Interactive Dashboards in Apache Superset

Aleksandr Gordienko
Jul 21 · 3 min read

What if you want to make your dashboard more interactive? What if you have a complex SQL query for your dataset and you have to implement an algorithm that will depend on filters you set in the dashboard or even based on parameters from a link for that particular dashboard? The answer to all of the questions is Jinja templates.

Photo by Kelly Sikkema on Unsplash

First of all, you need to activate Jinja templating if you have not done this before. To do this, you should add another element to the FEATURE_FLAGS dictionary in your superset_config.py. Element’s key will be ENABLE_TEMPLATE_PROCESSING, and the value will be True. Here is how it will look like:

FEATURE_FLAGS = {
... # your existing flags
"ENABLE_TEMPLATE_PROCESSING": True,
}

After adding the parameter you should rebuild your Docker image if you use Superset in a container. Here are two of my articles that could help you Run Apache Superset Locally in 10 Minutes and change settings via superset_config.py.

Now, it’s time to use Jinja macros in our SQL query! You may start with from_dttm or to_dttm standard Superset macros, for example. If you go to edit your virtual dataset, you may use {{to_dttm}} placeholder for the Jinja processor. Jinja will replace that placeholder with the end bound of the time range filter you set. Let’s start with an example query for MariaDB:

SELECT DATE_SUB(NOW(), INTERVAL 1 DAY) AS XDATE

We will use it to create a new virtual dataset in our system by clicking EXPLORE button and save. Then, we will save a table chart with the name test_table_chart. Now, we see the result of the query like this:

Result of the query execution

Now let’s change our query in the virtual dataset by replacing NOW() function with {{to_dttm}} Jinja macro this way and save:

Changed virtual dataset with Jinja macro

If you try to run your chart now, it will return N/A, because the template processor does not see any time range filter associated with this chart. To fix this, you have to put together your chart with a filter on a new dashboard. If you open your dashboard now, you will see the result of the query as we expected — the date that represents one day before the end bound of the time range filter:

Chart based on a dataset that uses to_dttm macro

Wow, it is cool, isn’t it? Play with the filter to check how it affects test_table_chart.

There are many built-in macros available for templating. You know that HTML is rendered in the Table visualization type, right? For example, you could use {{ url_param('your_custom_parameter') }} for using custom GET request parameters in your dashboards and make them really interactive. You could construct links via <a> tags in your SQL like this:

SELECT <a href="http://superset?page=777">Page #777</a> AS MY_LINK

That link will lead a user to the detailed dashboard where the dataset is based on {{ url_param('page') }} which filled via GET parameter from your link!

Additional information on other built-in Jinja macros could be found in the jinja_context.py source code. Moreover, you could define your own macro and make it available for templating in the dataset via TEMPLATE PARAMETERS in dataset properties. Fantastic!

I hope, you have enjoyed the article and this piece of information will help you to build charming useful visualizations. If so, please, follow me on Medium, GitHub, Twitter, and LinkedIn.

Geek Culture

Proud to geek out. Follow to join our 1M monthly readers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Start a blog

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store