This is the 3rd chapter of the Dash Tutorial.
The previous chapter covered the Dash app <code>layout<code>
and the next chapter covers interactive graphing.
Just getting started? Make sure to install the necessary dependencies.
In the previous chapter we learned that the
app.layout describes what the app
looks like and is a hierarchical tree of components.
dash_html_components library provides classes for all of the HTML
tags, and the keyword arguments describe the HTML attributes like
generates higher-level components like controls and graphs.
This chapter describes how to make your
Dash apps using callback functions: Python functions that are automatically called by Dash whenever an input component’s property changes.
Let’s get started with a simple example of an interactive Dash app.
Let’s break down this example:
a. By writing this decorator, we’re telling Dash to call this function for us whenever the value of the “input” component (the text box) changes in order to update the children of the “output” component on the page (the HTML div).
b. You can use any name for the function that is wrapped by the
@app.callback decorator. The convention is that the name describes the callback output(s).
c. You can use any name for the function arguments, but you must use the same names inside the callback function as you do in its definition, just like in a regular Python function. The arguments are positional: first the
Input items and then any
State items are given in the same order as in the decorator.
d. You must use the same
id you gave a Dash component in the
app.layout when referring to it as either an input or output of the
@app.callback decorator needs to be directly above the callback function declaration. If there is a blank line between the decorator and the function definition, the callback registration will not be successful.
f. If you’re curious about what the decorator syntax means under the hood, you can read this StackOverflow answer and learn more about decorators by reading PEP 318 – Decorators for Functions and Methods.
value” property of the component that has the ID
my-input”. Our output is the “
children” property of the
component_propertykeywords are optional
dash.dependencies.Inputobject and the
dash_core_components.Inputobject. The former is just used in these
childrenproperty of the
my-outputcomponent in the
layout. When the Dash app starts, it
html.Div(id='my-output', children='Hello world'), it would get
It’s sort of like programming with Microsoft Excel:
whenever an input cell changes, all of the cells that depend on that cell
will get updated automatically. This is called “Reactive Programming”.
Remember how every component was described entirely through its set of
keyword arguments? Those properties are important now.
With Dash interactivity, we can dynamically update any of those properties
through a callback function. Frequently we’ll update the
children of a
component to display new text or the
figure of a
to display new data, but we could also update the
style of a component or
even the available
options of a
Let’s take a look at another example where a
dcc.Slider updates a
In this example, the
"value" property of the
Slider is the input of the
app and the output of the app is the
"figure" property of the
value of the
Slider changes, Dash calls the callback
update_figure with the new value. The function filters the
dataframe with this new value, constructs a
and returns it to the Dash application.
There are a few nice patterns in this example:
df = pd.read_csv('...').
dfis in the global state of the app and can be
layout.transitionto give an idea
In Dash, any “
Output” can have multiple “
Here’s a simple example that binds five Inputs
value property of 2
RadioItems components, and 1
to 1 Output component (the
figure property of the
Notice how the
app.callback lists all five
inside a list in the second argument.
In this example, the
update_graph function gets called whenever the
value property of the
The input arguments of the
update_graph function are the new or current
value of each of the
Input properties, in the order that they were
Even though only a single
Input changes at a time (a user can only change
the value of a single Dropdown in a given moment), Dash collects the
current state of all of the specified
Input properties and passes them
into your function for you. Your callback functions are always guaranteed
to be passed the representative state of the app.
Let’s extend our example to include multiple outputs.
So far all the callbacks we’ve written only update a
Output property. We can also update several at once: put all the
properties you want to update as a list in the decorator, and return that
many items from the callback. This is particularly nice if two outputs
depend on the same computationally intense intermediate result, such as a
slow database query.
A word of caution: it’s not always a good idea to combine Outputs, even if
You can also chain outputs and inputs together: the output of one callback
function could be the input of another callback function.
This pattern can be used to create dynamic UIs where one input component
updates the available options of the next input component.
Here’s a simple example.
The first callback updates the available options in the second
component based off of the selected value in the first
The second callback sets an initial value when the
changes: it sets it to the first value in that
The final callback displays the selected
value of each component.
If you change the
value of the countries
RadioItems component, Dash
will wait until the
value of the cities component is updated
before calling the final callback. This prevents your callbacks from being
called with inconsistent state like with
In some cases, you might have a “form”-type pattern in your
application. In such a situation, you might want to read the value
of the input component, but only when the user is finished
entering all of his or her information in the form.
Attaching a callback to the input values directly can look like
In this example, the callback function is fired whenever any of the
attributes described by the
Try it for yourself by entering data in the inputs above.
dash.dependencies.State allows you to pass along extra values without
firing the callbacks. Here’s the same example as above but with the
dash.dependencies.State and a button as
In this example, changing text in the
dcc.Input boxes won’t fire
the callback but clicking on the button will. The current values of
dcc.Input values are still passed into the callback even though
they don’t trigger the callback function itself.
Note that we’re triggering the callback by listening to the
n_clicks property of the
n_clicks is a
property that gets incremented every time the component has been
clicked on. It is available in every component in the
We’ve covered the fundamentals of callbacks in Dash.
Dash apps are built off of a set
of simple but powerful principles: declarative UIs that are customizable
through reactive and functional Python callbacks.
Every element attribute of the declarative components can be updated
through a callback and a subset of the attributes, like the
properties of the
dcc.Dropdown, are editable by the user in the
The next part of the Dash tutorial covers interactive graphing.