In this tutorial, you’ll learn how to create Python interactive dashboards with plotly Dash.
Web-based dashboards are a great way to display and share information with others. But it often involves complicated processes that only a team of web developers can achieve. As Python developers in data science, how can we build an interactive web application with data visualizations?
Plotly Dash is the go-to library. It empowers us to build beautiful looking, interactive, and easy to share dashboards, all in Python.
Following this tutorial, you’ll learn:
- What is Dash
- How to build the Dash app layout
- How to include data visualization
- How to add interactive features (callbacks)
- How to run and display the dashboard
If you want a quick example of building dashboards with Python Dash, this tutorial will walk you through the process step-by-step.
Let’s get started!
Further learning: to learn more details and depth about Dash, please take our video course: Python Interactive Dashboards with Plotly Dash. It includes step-by-step explanations, more advanced functions, all with real-world dataset examples.
This tutorial assumes you have basic Python knowledge. If not, please take our FREE Python crash course for data science.
It also helps to have some knowledge of the pandas library. Check out Learn Python Pandas for Data Science: Quick Tutorial.
What is Dash?
Each Dash app has two main parts:
- layout: determines the visual components displayed on the Dash app.
- callback function: the function that connects the Dash components and defines their interactive features.
Now let’s go through an example to make an interactive data visualization using Dash.
Step #1: Exploring the dataset
Before building the Dash app, we need to explore the dataset. We recommend doing this in Jupyter Notebook. Since it has an interactive interface, we can code and examine the results easily.
First, we’ll import two libraries:
pandas: for loading and manipulating datasets.
plotly.express: for generating data visualizations.
Dash is built on top of plotly, so it’s easy to put plotly figures into Dash apps. This is why we are using plotly, instead of other Python data visualization libraries.
In this tutorial, we’ll use the Avocado Prices dataset to build our example dashboard. So let’s load it and take a look at its summary.
As you can see, the dataset contains information about avocado prices.
<class 'pandas.core.frame.DataFrame'> RangeIndex: 30021 entries, 0 to 30020 Data columns (total 13 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 date 30021 non-null object 1 average_price 30021 non-null float64 2 total_volume 30021 non-null float64 3 4046 30021 non-null float64 4 4225 30021 non-null float64 5 4770 30021 non-null float64 6 total_bags 30021 non-null float64 7 small_bags 30021 non-null float64 8 large_bags 30021 non-null float64 9 xlarge_bags 30021 non-null float64 10 type 30021 non-null object 11 year 30021 non-null int64 12 geography 30021 non-null object dtypes: float64(9), int64(1), object(3) memory usage: 3.0+ MB
Suppose we want to present the average prices of different types of avocados for various geographies across time, i.e., we want to focus on presenting the information of the features
What are the different
geography of avocados? Let’s take a look at the categories using the
value_counts method with
dropna = False. This will show us the unique categories for these variables and if there are any missing values.
There are two categories of
type, and many different categories for
geography. And both variables don’t have missing values.
conventional 15012 organic 15009 Name: type, dtype: int64
Phoenix/Tucson 556 Northeast 556 Las Vegas 556 Sacramento 556 Tampa 556 Spokane 556 Southeast 556 New York 556 Raleigh/Greensboro 556 Syracuse 556 Plains 556 California 556 Orlando 556 Albany 556 Boise 556 Boston 556 Houston 556 West 556 Portland 556 Harrisburg/Scranton 556 Cincinnati/Dayton 556 Miami/Ft. Lauderdale 556 Dallas/Ft. Worth 556 Hartford/Springfield 556 Great Lakes 556 Louisville 556 Philadelphia 556 Pittsburgh 556 Baltimore/Washington 556 Roanoke 556 Jacksonville 556 Midsouth 556 Chicago 556 San Francisco 556 South Central 556 San Diego 556 Detroit 556 Grand Rapids 556 Nashville 556 Charlotte 556 Seattle 556 Los Angeles 556 Northern New England 556 Indianapolis 556 Buffalo/Rochester 556 Total U.S. 556 Richmond/Norfolk 556 New Orleans/Mobile 556 Denver 556 St. Louis 556 Atlanta 556 South Carolina 556 Columbus 556 West Tex/New Mexico 553 Name: geography, dtype: int64
Since there are only two avocados types, we can plot their
average_price time series on the same line chart.
Let’s try to plot such a figure when
geography is ‘Los Angeles’.
Further Learning: If you are not familiar with plotly, please look at our tutorial Plotly Python Tutorial: How to create interactive graphs.
If you are not familiar with pandas filtering, please take a look at Learn Python Pandas for Data Science: Quick Tutorial.
This is a nice chart, but it’s only for one
How can we make it easy for users to explore this information for different
If we have a dropdown with options of
geography, the users would be able to choose among them. Then according to the
geography selected by the users, we can display the above line plot to them for that specific
While this is not possible with plotly, it’s something Dash can help!
It’s time to use Dash.
Step #2: Setting up the Python environment
After exploring the dataset in Jupyter Notebook, we recommend using one of the Python editors to implement Dash apps. When working on Dash apps, we want to focus on building and running the dashboards as a whole script. So it’s easier to test in Python editors such as PyCharm.
We’re using the PyCharm Editor – Community Edition. It’s free and has many useful features for writing code in Python. But you’ll also be good to choose your preferred Python editor.
It’s also necessary to use the
pip install dash command in your terminal to install Dash before using it.
Step #3: Preparing to build the Dash app
Now we can head over to the Python editor such as PyCharm to start writing the Dash app. The code snippets below need to be combined and run as a single Python script. We are breaking them down into pieces so that it’s easier to explain. You can either type them into your Python script or copy and paste the complete version, which will be provided at the end.
First, we need to import the libraries. The necessary libraries for our dashboard are:
dash: the main Dash library.
dash.dependencies, so that we can use them without referring to
dash_html_components: for building the layout, which contains components for every HTML tag, such as the H1 heading.
dash_core_components: for building the layout, which contains various higher-level components such as dropdown, graph.
pandas: for loading and manipulating datasets.
plotly.express: for creating figures.
Then we can load the dataset as a pandas DataFrame, which is the same as earlier. Please make sure you save the Python script and the dataset
avocado-updated-2020.csv in the same directory to avoid setting the path in the
We’ll also create a Dash app object called
app as below. This
app is what we’ll be focusing on for the rest of the tutorial.
Step #4: Building the layout of the dashboard
The app-building process always starts from the layout. We need to design the look of the dashboard first.
The layout has the structure of a tree of components. We use the keyword
layout of the
app to specify its layout. Then, using the two libraries:
dcc), we can display three components on our dashboard:
- an H1 heading (
html.H1) as the title of the dashboard. We specify its children property to be the text ‘Avocado Prices Dashboard’.
- a dropdown menu (
dcc.Dropdown) based on the
We give it an id ‘geo-dropdown’.
optionsproperty specifies the options of unique geographies the dropdown has.
valueproperty is the selected geography when we first launch the app. We made it as ‘New York’.
- a graph component (
dcc.Graph) with id ‘price-graph’.
Below is the code to set up the layout.
As you noticed, we are using an
html.Div component to hold our three Dash components. The
html.Div is a container component, which is always used when we have multiple Dash components in the layout. We put the other Dash components as a list inside its
After setting up the dashboard’s look, it’s time to add a callback function to make it interactive.
Step #5: Adding interactivity to the dashboard
The callback functions are Python functions. But they get automatically called by Dash whenever its input changes. As a result, the function runs and updates its output.
The two main sections of the callback function are:
- the decorator which starts with
- the function itself starts with
Below is the code of our callback function to make the plotly figure dependent on the dropdown.
Within the decorator
@app.callback, we specify the
Output and the
Input objects of the callback function. They are both the properties of Dash components. In our example, the output is the
figure property of the Dash component with ID = ‘price-graph’, which is the
dcc.Graph component set in the layout. While the input is the
value property of the Dash component with ID = ‘geo-dropdown’, which is the
dcc.Dropdown component set in the layout.
After specifying them, we use them within the function below. Within the parenthesis after the
def update_graph, we named the input as
selected_geography, this corresponds to the
Input(component_id='geo-dropdown', component_property='value'). Then within the body of the function, we ask the function to:
- generate a filtered dataset
- create a plotly line figure called
line_figbased on this filtered dataset
The function returns this
line_fig as the output, which corresponds to
For example, when the user selects ‘Los Angeles’ in the dropdown component, its
value property will become ‘Los Angeles’, which means the input of the function
selected_geography='Los Angeles'. This change will trigger the callback function, and update the output, as the line figure only for Los Angeles.
That’s all the work needed for the callback function!
We are ready to run the dashboard.
Step #6: Running the dashboard
To complete the script, we need to add code to run the server. By default, the Dash app runs on our local computers. We can add these two lines of code after the callback function.
The complete script we need to save into the Python editor is below:
As mentioned earlier, we need to run it as a whole script. So we can save this whole block of code as a Python script (named
avocado_example) under the same directory as our dataset
avocado-updated-2020.csv. Then we can go to the terminal to run it by typing in the command
After running successfully, you should see the below messages in the terminal window.
Dash is running on http://127.0.0.1:8050/ * Serving Flask app "avocado_example" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on
Remember that Dash creates web applications? So this URL
http://127.0.0.1:8050/ is the default address to access the app. You can click this link and see your first Python interactive dashboard opening in the browser!
It should look like this:
If you haven’t got the chance to run your app, take a look here. We have deployed this app on Heroku so that you can interact with it as a user. Try to select different geographies within the dropdown and see the updated graph.
In this tutorial, you’ve learned how to create your first Python interactive dashboard with Dash!
Again, to learn about how to:
- set up more Dash components such as range slider, radio items, datatable
- customize the look of the dashboards
- create a grid layout dashboard
- more dynamic interactive features
please take our video-format course: Python Interactive Dashboards with Plotly Dash. You’ll also get an overview of HTML, CSS within the course, so you’ll have a better understanding of Dash.
Leave a comment for any questions you may have or anything else.