Python Friday #198: Multi-Component Dashboards with Dash

After our first steps with Dash last week, we create in this post a more complex dashboard with multiple components that we can update simultaneously.

This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.

 

Install the dash-bootstrap-components

I like working with Bootstrap and the package dash-bootstrap-components allows us to use it directly in Dash. While this is optional and you can use Dash with many other components, I suggest you give it a try. We can install the package with this command:

All the components are well explained in the official documentation. Since it is easy to overlook, make sure that you set a stylesheet in the constructor for Dash – otherwise the user interface will not know what to do with rows and other Bootstrap-specific components:

 

Extending the dashboard

In the last post we created a simple dashboard, that we now extend with more functionality. We keep the dropdown list for the days to filter the tips data set and the scatter plot.

We add a pie chart, a box plot, and a Markdown area to our dashboard. All those elements should automatically update as soon as we select a different value in the dropdown list.

To get everything working, we need a few additional imports and we set the default template for Plotly to seaborn:

 

Creating the layout

In the layout method we start with a H1 element for the title, add a horizontal rule as a separator. Then we use the rows from Bootstrap to place the dropdown list and the Markdown area on the same row. The scatter plot gets its own row, while the pie chart and the box plot share the same row:

 

Gluing everything together

The update logic and the definition of the plots is handled in the method update_graphs(). We use the callback to map one input on an output for each of our 4 components. We need to specify the Id of the element and the property name we want to put the return value into.

The method returns multiple values as a tuple and Dash puts them into the different outputs of the callback:

At the end of our Python script, we add the well-known lines to start the application:

 

The result

If we put all the code together and run our application, we get a dashboard that looks like this:

Our dashboard has a title, a dropdown list to select the day, 3 graphs and a Markdown area.

For all the plots we can use the interactive features of Plotly and zoom to the interesting parts.

 

Next

We created an interactive dashboard with multiple components that are updated in one go. I hope this shows you the possibilities of Dash and how we can create complex dashboards to meet our requirements.

Next week we add Dash to an existing Flask application and fight through the obstacles we find on the way.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.