Overview
This article covers how to use Jinja templating to read URL parameters and use them in datasets, charts, saved queries and dashboards. For example, a link ending in ?quarter=2 can show the same chart filtered to the second quarter, without creating a separate chart.
.gif)
To set the value of a dashboard's native filters from the URL, you don't need Jinja. See Filtering dashboards with URL parameters. Use
url_param()when you need a custom parameter that your SQL reads directly.
How url_param() works
url_param('name') returns the value of the URL parameter name. With the URL ...?country=France, {{ url_param('country') }} renders as France.
The value is inserted without quotes. For text values, add the quotes yourself:
country = '{{ url_param('country') }}'.A missing parameter renders as
None. Either wrap the condition in{% if url_param('country') %} ... {% endif %}, or set a default value:url_param('country', default='USA').Quotes in the value are escaped for your database. For example,
O'BrienbecomesO''Brien, so the value can't close the quotes around it.Where the value comes from: the URL of the chart (Explore), of the dashboard the chart is on, or of SQL Lab. In embedded dashboards, pass parameters with the
urlParamsoption of the Embedded SDK (see Step 2: Deployment).Each parameter value is cached separately, so changing the parameter returns fresh results.
The full signature is url_param(param, default=None, add_to_cache_keys=True, escape_result=True).
Configuring the asset to retrieve the URL parameter
URL parameters can be used in dataset definitions, chart configurations and saved queries. In all cases, the syntax is the same: the url_param() Jinja macro.
The following sections cover each scenario. They use the Vehicle Sales table from the examples database connection.
1. Fetching URL parameters in the dataset query
The url_param() macro can be used in the SQL query of a virtual dataset, and in metric and calculated column definitions. The following example uses this SQL query:
select order_date, order_number, product_line, price_each, country, quarter
from "Vehicle Sales"
where 1=1
{% if url_param('quarter') %}
and quarter = {{ url_param('quarter') | int }}
{% endif %}quarter is a number, so the value isn't quoted. The | int filter converts it to a whole number, so only numbers can reach the query (see Security and good practices).
To do so:
Access your Workspace.
Navigate to SQL > SQL Lab.
Make sure that the
examplesdatabase connection is selected in the Database dropdown.Paste the SQL query and click Run to confirm it works. Without the parameter, the query returns all quarters.
Click the arrow next to Save and select Save dataset.
Enter a name for your dataset and click Save & Explore.
The page opens a Table chart. Set the query mode to Aggregate and select
quarteras the dimension.Set
COUNT(*)as the metric and click Update chart.Click Save in the top right corner, and fill in the dialog to create the chart.
After the page reloads, click in the browser's address bar, add
&quarter=1at the end of the URL, and press Enter.
The chart now shows only the first quarter. The same approach works in calculated columns and metrics defined in the dataset.
2. Fetching URL parameters in the chart configuration
You can also use URL parameters directly in the chart builder, in custom SQL dimensions, metrics and filters. To do so:
Create a chart using the
Vehicle Salesdataset from theexamplesdatabase connection.Set any column as the dimension.
Add a metric, open the Custom SQL tab and enter:
{% if url_param('metric') == 'sum' %} SUM(price_each) {% elif url_param('metric') == 'avg' %} AVG(price_each) {% else %} COUNT(*) {% endif %}Here the value is only compared inside Jinja and never inserted into the SQL, so no quotes are needed. Only the three listed expressions can reach the query.
Give the metric a name and save it.
Click Update chart.
Click the ellipsis menu (...) next to Save and select View query. Note that
COUNT(*)is used.Click in the browser's address bar, add
&metric=sumat the end of the URL, and press Enter.Open View query again and confirm that
SUM(price_each)is used.
If you add this chart to a dashboard, you can also add the parameter to the dashboard URL (see Using this feature on dashboards).
3. Fetching URL parameters in a saved query
You can also save a SQL query that uses URL parameters. To do so:
Navigate to SQL > SQL Lab.
Make sure the
examplesdatabase connection is selected in the Database dropdown, and paste the following SQL query:select country, SUM(price_each) as revenue from "Vehicle Sales" where 1=1 {% if url_param('country') %} and country = '{{ url_param('country') }}' {% endif %} group by 1countryis text, so the value is wrapped in single quotes.Click Run. Without the parameter, the query returns all countries.
Click Save.
Enter a name for the saved query and click Save.
Navigate to SQL > Saved Queries.
Hover over the query and click the Copy query URL button in the Actions column.
Paste the URL in the browser's address bar, add
&country=USAat the end, and press Enter.Click Run.
The filter is applied and only data for USA is returned.
Using this feature on dashboards
URL parameters also work in dashboard links. Every chart on the dashboard receives the dashboard's URL parameters. Keep in mind that:
Charts must be configured to use the URL parameter, either in the dataset or in the chart itself. Charts that don't use it are not affected.
Native filters still apply on top of the URL parameter. For example, with
?country=Franceand a Country native filter set to USA, a chart filtered by both shows no results.If the dashboard URL has no
?yet, start with?instead of&, for example/superset/dashboard/12/?quarter=2. Preset may add anative_filters_keyparameter to the URL after the dashboard loads. This is expected, and your own parameters are kept.
For this example, create a sample dashboard that includes the charts created in sections 1 and 2:
Create a dashboard and add the charts from sections 1 and 2.
Save the changes and reload the dashboard.
In the browser's address bar, add
&quarter=2&metric=sumat the end of the URL and press Enter.Each chart updates based on the parameter it uses: the quarter chart shows only the second quarter, and the metric chart shows sums instead of counts.
(1).gif)
To share the result, copy the URL from the address bar. Permalinks created with ... > Share > Copy permalink to clipboard also keep the URL parameters.
Security and good practices
Anyone who opens the link can change URL parameters, so treat their values as user input:
Quote text values:
country = '{{ url_param('country') }}'. Escaping protects values that are inside quotes.Convert numbers with
| int(or| float):quarter = {{ url_param('quarter') | int }}. Without quotes or a conversion, a value such as1 OR 1=1is inserted into the SQL as is. With| int, anything that isn't a number becomes0.Prefer a fixed list of options when the parameter selects SQL (columns, functions, sort order). Compare the value in Jinja and write the SQL yourself, as in section 2.
Don't set
escape_result=False. It turns off escaping. Even legitimate values such asO'Brienthen break the query.Don't use URL parameters to restrict data access. Users can remove or change them. Use Row Level Security or dataset permissions instead.