Dash Overview
Create data apps in Python.
Dash: The Basics
Dash is a Python-based dashboarding library that makes it easy to build detailed, Enterprise-grade dashboards and data apps.
To understand Dash, there are three basic concepts to understand:
A Dash App instance: a single running instance of your data app.
The app layout: how to configure what is actually displayed in your dash app.
Callbacks: how to make your dash app interactive.
To see how each of these come together, consider the following simplest example of an interactive Dash application:
(1) The Dash App Instance
To create a new Dash app instance, all you need is the following code (in a file app.py
Running this code with python app.py
will start a server that serves a blank webpage. Congrats, that's your first data app! But because you havent set a layout, or configured any callbacks, it's not super interesting.
(2) The App Layout
Ok, so you actually want something to be displayed. Let's add in more code - to set the layout.
If you run this code, a webpage with some content will be served! It will have two things:
A title - set on line 12. The title is set to
Title of Dash App
and some additional styling is added to center the text.A dropdown - set on line 13. The possible options of this dropdown are the various countries in the dataframe defined on line 5, and the starting value is
Canada
app.layout
is how you actually tell the Dash app what to display. You can nest components like div's (which are just containers), or H1
(which is a large header) or Dropdown
(which lets you select from a variety of options). See the Dash documentation for all components.
However, nothing happens when you change the dropdown item that is selected! That's no good -- we want to update the graph component defined on line 14...
(3) A Callback
Callbacks are how you add interactivity to your application. By registering a series of Outputs that change when a different set of Inputs change, you can make pieces of your application respond changes in other pieces of your application.
Here's what's happening above:
On line 18, a callback is registered. The callback is the function
update_graph
The Input to the callback is the
value
of thedropdown-selection
component - defined on line 13.The Output from the callback is the
figure
of thegraph-content
component, defined on line 14.
Here's what this accomplishes:
When the
value
of the component with iddropdown-selection
changes because the user selects something different from the dropdown, the functionupdate-graph
changes.When this function runs, the
value
that is passed to the function as it's singular argument is the new value of the Dropdown the user has selected on the frontend.This function runs, creates a new Plotly line chart, and then returns it.
This returned line chart is written to the
graph-content
component, specifically for it'sfigure
value, which then updates on the frontend.
By reading the documentation for a component, you can see what the correct props are to write to with outputs!
Understanding More About Dash
New to writing Dash applications and looking to understand more? Check out the Dash docs to learn more core concepts.
Last updated