To get the most out of this page, make sure you’ve read about Basic Callbacks in the Dash Fundamentals.
There are some aspects of how Dash works that can be tricky, especially when it comes to callbacks. This page
outlines some common gotchas that you might encounter as you start building more complex Dash apps.
Outputto be present in the layout
By default, Dash applies validation to your callbacks, which performs checks
such as validating the types of callback arguments and checking to see
whether the specified
Output components actually have the
specified properties. For full validation, all components within your
callback must exist in the layout when your app starts, and you
will see an error if they do not.
However, in the case of more complex Dash apps that involve dynamic
modification of the layout (such as multi-page apps), not every component
appearing in your callbacks will be included in the initial layout. You can
remove this restriction by disabling callback validation like this:
app.config.suppress_callback_exceptions = True
Statesto be rendered on the page
If you have disabled callback validation in order to support dynamic
layouts, then you won’t be automatically alerted to the situation where a
component within a callback is not found within a layout. In this situation,
where a component registered with a callback is missing from the layout, the
callback will fail to fire. For example, if you define a callback with only
a subset of the specified
Inputs present in the current page layout, the
callback will simply not fire at all.
All your callbacks must be defined before your Dash app’s server starts
running, which is to say, before you call
app.run(debug=True). This means
that while you can assemble changed layout fragments dynamically during the
handling of a callback, you can’t define dynamic callbacks in response to
user input during the handling of a callback. If you have a dynamic
interface, where a callback changes the layout to include a different set of
input controls, then you must have already defined the callbacks required to
service these new controls in advance.
For example, a common scenario is a
Dropdown component that updates the
current layout to replace a dashboard with another logically distinct
dashboard that has a different set of controls (the number and type of which
might which might depend on other user input) and different logic for
generating the underlying data. A sensible organization would be for each of
these dashboards to have separate callbacks. In this scenario, each of these
callbacks much then be defined before the app starts running.
Generally speaking, if a feature of your Dash app is that the number of
States is determined by a user’s input, then you must
predefine every permutation of callback that a user can
potentially trigger. For an example of how this can be done programmatically
callback decorator, see this Dash Community forum
As of Dash 1.15.0,
State in callback definitions don’t need to be in lists. You still need to provide
Output items first, and the list form is still supported. In particular, if you want to return a single output item wrapped in a length-1 list, you should still wrap the
Output in a list. This can be useful for procedurally-generated callbacks.
Note: This section is present for legacy purposes. Prior to v0.40.0, setProps was only defined if the component was connected to a
callback. This required complex state management within the component like this.
Now, setProps is always defined which should simplify your component’s state
management. Learn more in this community forum topic.
If a Dash Core Component is present in the layout but not registered with a
callback (either as an
Output) then any changes to its
value by the user will be reset to the original value when any callback
updates the page.