How to add a button in a dashboard to run a Python script

Svg background
Overview

In this tutorial, you will learn how to add a button in a dashboard that runs a Python script. The button can be added:

  • in a table (one button per row). In this case, you can pass values from the table columns to the script as parameters.
  • in a custom HTML widget.

Run a script from a table widget

As a very first step, create a Python script using the code below. We will use it in all our examples.

import serenytics
client = serenytics.Client()

print('Hello, Serenytics!')
print(client.script_args)

Then create a simple table in a dashboard:

Table simple
Then, create a new "Computed Measure" in this table and use the function "btn_run_automation" (this function generates a button to run an automation, see its doc here):
Table simple

Here, we use this formula:

btn_run_automation("My button", "028f8585-07e9-4d48-b295-2cc67dcd2f41")
where "028f8585-07e9-4d48-b295-2cc67dcd2f41" is the unique identifier of the automation to run (in the script editor, you can get it either from the "script id" field or from the URL).

Note that when creating the formula in the table, the input area is very small. It's easier to open the formatting option for the newly created column and edit the formula there.

You can test the process: click on the button and then open the Execution tab of your Python script. You will see it's been executed a few seconds ago:

Script log

In the Python script used here, we printed the values of client.script_args. This value contains the parameters passed to the Python script. When executed from a dashboard, it contains the key "triggered_by_user". In the next paragraph, we show you how to add data from the table into these parameters.

Pass row values as parameters to the triggered script
To pass parameters to the script, add them as a list of key-values at the end of the parameters list of the btn_run_automation function:
btn_run_automation("My button", "028f8585-07e9-4d48-b295-2cc67dcd2f41",
"country", line[1],
"nb_of_purchases", line[2])
With this code, the execution log of the script shows (if I click on the "France" row of the table):
Script log
Tip: in some cases, you need to pass an identifier that you don't want to show in the table. To do that, in the table configuration, select the measure corresponding to the identifier in a column, and then hide this column (in the formatting option of the column). You can use it in the btn_run_automation function, but the user won't see the column with the identifier.
Using a custom widget
To run a script from a button outside a table, add a custom widget in a dashboard, using the icon:
Custom widget icon
In the HTML code of the widget, enter this code:
<button  class="btn btn-primary"
onclick="srnRunAutomation('028f8585-07e9-4d48-b295-2cc67dcd2f41',{'content':'my parameter'})">
My button
</button>
This creates a simple button:
Custom button to run a script
In a very similar manner, this button will trigger the script and pass it the parameters specified in the second argument in the HTML code.
Allow Viewers to run a Python script

By default, the script will use the credentials of the user who clicked on the button. But in many cases, this user will be a Viewer and a Viewer cannot have credentials on a Python script (to be more precise, "on the folder containing a script").

To allow a given script to be executed by Viewers, you need to enable an option on the automation called "Allow all users to run this script":

barchart icon
When this option is enabled, a logged Viewer is allowed to run the script. And the script is executed with the credentials defined in the run-as-user field.
Conclusion
With this feature, you can now create dashboards that not only let the Viewer view data but also let him trigger actions.
×