Quick start - basic script

Here is an example to illustrate several core features of Serenytics scripts:

  • creating the Serenytics python client
  • filling an internal storage with some data
  • querying this storage (could be any other data source)
  • sending an email

In the Serenytics interface, create a new script, copy/paste the code below and execute it. Congratulations, you're done with your first data reporting process with Serenytics!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import serenytics

# create the serenytics client
client = serenytics.Client()

# get or create a storage
# Note: open the Data menu to see this new data source
# (you need to reload the page to refresh the list of sources)
source = client.get_or_create_storage_data_source_by_name(name='example 1 storage')

# fill it with 4 rows of data
input_data = [
    {'country': 'FR', 'name': 'John', 'quantity': 10},
    {'country': 'US', 'name': 'Mark', 'quantity': 2},
    {'country': 'UK', 'name': 'John', 'quantity': 1},
    {'country': 'US', 'name': 'David', 'quantity': 8}
]

print('loading data in the storage...')
source.reload_data(new_data=input_data)

# run a query against the storage we've just created above
options = {
    'format': 'simple_array',
    'order': 'row_by_row',
    'data_processing_pipeline': [{
        'group_by': ['country'],
        'select': [{'name': 'country'},
                   {'name': 'quantity', 'agg_function': 'sum'}],
        'where': [{'name': 'country', 'op': '==', 'value': 'US'}]
    }]
}

print('querying...')
output_data = source.get_data(options=options)

# send an email with the resut of the query in the body of the email
# Note: for a more graphical result, use our studio to design a pretty dashboard
# and use the send_email function to send it as a pdf file
# (using parameter web_app_uuid)
subject = 'Sales US'
recipients = ['contact@serenytics.com']
html = 'Hello,<br><br> US quantity is: %d' % output_data.rows[0][1]
html += '<br><br> <a href=http://www.serenytics.com>www.serenytics.com</a>'

print('sending email...')
client.send_email(subject=subject, recipients=recipients, html=html)

print('done')