Stakeholders commonly need to provide some configuration to scripts to enable self-service report generation. Datapane allows you to add parameters to a script, which are presented to end users as web forms. This means that other people who have accounts on your instance can generate reports without worrying about code, notebooks, or setting up a Python environment.
Input parameters can be defined in your
datapane.yaml configuration file, where you can enter a schema and configure the inputs. In your Python code, the parameters which you define in this file are accessible in the
Params dictionary. You can get an item from the dictionary with
Following the previous example, the dataset we are pulling includes a few other useful parameters which people may want to graph. Let's add the ability for the end-user to choose from
gdp_per_capita . Additionally, let's allow them to choose a subset of continents for the graph.
Based on this, we are going to add two parameters:
continents to the
parameters section of our
datapane.yaml. To configure what the end-user's form looks like, we can choose the type of widget. For the above, we're choosing a
enum (which provides a dropdown select menu where the user must select one option), and a
list (which allows the user to choose or more choices from a predefined list). We can also set the default parameters for each input and a description.
datapane.yamlname: covid_scriptscript: simple_script.py # this could also be ipynb if it was a notebookparameters:- name: field_to_plotdescription: Field to plottype: enumchoices:- new_cases_smoothed_per_million- new_deaths_smoothed_per_million- median_age- gdp_per_capita- name: continentsdescription: Field to plottype: listchoices:- Africa- Asia- Europe- Oceania- North America- South Americadefault:- Asia- North America
You can then use the
Params object as you would when running on your Datapane instance, and we can customise our data and graph based on those inputs.
simple_script.pyimport pandas as pdimport altair as altimport datapane as dpdataset = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv')# Get input parameterscontinents = dp.Params.get('continents', ['Asia', 'North America'])field_to_plot = dp.Params.get('field_to_plot', 'new_cases_smoothed_per_million')df = dataset[dataset.continent.isin(continents)].groupby(['continent', 'date'])[field_to_plot].mean().reset_index()plot = alt.Chart(df).mark_area(opacity=0.4, stroke='black').encode(x='date:T',y=alt.Y(field_to_plot, stack=None),color=alt.Color('continent:N', scale=alt.Scale(scheme='set1')),tooltip='continent:N').interactive().properties(width='container')dp.Report(dp.Plot(plot),dp.Table(df)).publish(name='covid_report', open=True)
When we run
script deploy, Datapane will deploy a new version of our script, and use our parameters definition to generate the following form:
Stakeholders can enter parameters and generate custom reports themselves, based on these fields.