Skip to content

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!

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...')

# 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'}]

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 = ['']
html = 'Hello,<br><br> US quantity is: %d' % output_data.rows[0][1]
html += '<br><br> <a href=></a>'

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