Skip to content

Run Python code

In Serenytics, you can write and execute Python code directly within the platform. The main use case is to use Python to get data from a custom datasource or to execute an advanced data processing.

All you have to do it to create a Job of type Python script, type your Python code and in the Execution tab, click on Run, or schedule it as any other job (e.g. each night).

We provide a Python client to make it as simple as possible to interact with your data.

The Serenytics Python client allows you to:

  • create a new data source
  • load data in a source
  • extract data from a source
  • generate PDF reports from a dashboards
  • send emails
  • refresh cache of a dashboard

and much more...

See Here for the full list of functions in our Python client.

Basically, our Python client is a wrapper around our REST API. We strongly advise you to use this client. You should only use the REST API if you are working with another language or for advanced needs.

Common Python script example

Here is the most common example of a Python code that you can schedule in the platform. It gets data from a datasource, does some data processing and loads the result in a new datasource (it must be a storage, i.e. a table in the internal datawarehouse - see here for details about storages).

import serenytics  # the Serenytics Python client
import pandas as pd

INPUT_DATASOURCE_UUID = '181018f9-a18a-4cf0-993f-9e3fa45d3f61'
OUTPUT_STORAGE_UUID = '04835197-d8a5-485a-927f-b7f65314149d'

client = serenytics.Client()

# Get a datasource
# It's better to get it from its uuid than by its name because the name can change.
# The UUID of a datasource is its unique number, it is shown in the config page of the datasource
# and you can also see it in the datasource URL.
input_source = client.get_data_source_by_uuid(uuid=INPUT_DATASOURCE_UUID)

# get all the data from this source, in a pandas dataframe
df = input_source.get_data().get_as_dataframe()

# use pandas for some transformations
df_clean = pd.DataFrame(df.name.str.split(' ',1).tolist(),
                        columns = ['first name','last name'])
df_clean['country'] = df['country']
df_clean['quantity'] = df['quantity']

# load the DataFrame in a new source
output_source = client.get_data_source_by_uuid(uuid=OUTPUT_STORAGE_UUID)
output_source.reload_data_from_dataframe(df_clean)