Dash
Dash is a Python framework for building analytical web applications. Developed by Plotly, it allows users to create interactive dashboards and data visualization tools entirely in Python, without requiring JavaScript or web development expertise. It is currently at version 4.1.0 and typically releases minor versions and release candidates regularly, leading up to major version updates.
Common errors
-
ModuleNotFoundError: No module named 'dash'
cause The Dash library or its essential components are not installed in the active Python environment, or the environment is not correctly activated.fixRun `pip install dash` in your terminal to install the Dash library and its core components. -
ImportError: cannot import name 'dcc' from 'dash'
cause This error often occurs due to version discrepancies (older Dash versions required separate imports for `dash_core_components` and `dash_html_components`) or a circular import if a local file is named `dash.py`.fixFor Dash 2.0 and newer, ensure imports are `from dash import dcc, html`. For older versions (pre-2.0), use `import dash_core_components as dcc` and `import dash_html_components as html`. Also, rename any local file named `dash.py`. -
AttributeError: 'Dash' object has no attribute 'run'
cause The method `app.run()` is not the correct way to start a Dash application; the correct method is `app.run_server()`.fixChange `app.run()` to `app.run_server()` to properly launch your Dash application. -
AttributeError: module 'dash' has no attribute 'Dash'
cause A local Python file named `dash.py` exists in your project or current working directory, which Python attempts to import instead of the actual Dash library, leading to a naming conflict.fixRename any file named `dash.py` (e.g., to `app.py` or `my_dash_app.py`) to avoid conflicting with the installed `dash` package. -
Error loading layout
cause This is a generic client-side error indicating that the `app.layout` structure is malformed, contains invalid components, or has incorrect property assignments, which prevents Dash from rendering the page.fixEnable debug mode (`app.run_server(debug=True)`) to get detailed server-side tracebacks and carefully inspect your `app.layout` definition for syntax errors, invalid component properties, or improper nesting.
Warnings
- deprecated The `dash.dependencies` module for importing `Input`, `Output`, `State` is deprecated. These symbols, along with the `callback` decorator, should now be imported directly from the `dash` package.
- breaking Dash Core Components (DCC) underwent a significant redesign in Dash v4.0.0. This might introduce breaking changes to component properties, styling, or internal structure for applications upgrading from older versions.
- gotcha By default, Dash callbacks fire immediately upon application load. This can lead to errors if input components have not yet been rendered or populated with initial values (e.g., `None` where a string or number is expected).
- gotcha With the introduction of multi-backend support in Dash 4.1.0, there are two distinct ways to specify a backend server: `Dash(backend="flask"| "fastapi" | "quart")` or `Dash(server=existing_server_instance)`. Using `backend` creates a new server instance, while `server` attaches Dash to an *already existing* Flask/FastAPI/Quart application instance.
- breaking The `app.run_server()` method has been deprecated and subsequently removed. It has been replaced by `app.run()`.
- breaking The `app.run_server()` method for running a Dash application has been removed and replaced by `app.run()` starting from Dash v4.0.0. Calling `app.run_server()` will raise an `ObsoleteAttributeException`.
Install
-
pip install dash -
pip install dash[fastapi] -
pip install dash[quart]
Imports
- Dash
from dash import Dash
- html
from dash import html
- dcc
from dash import dcc
- Input, Output, State, callback
from dash.dependencies import Input, Output, State
from dash import Input, Output, State, callback
Quickstart
import os
from dash import Dash, html, dcc, Input, Output, State, callback
# Initialize the Dash app
# For deployment, consider setting up a robust WSGI server (e.g., Gunicorn)
# and handling config via environment variables.
app = Dash(__name__)
app.layout = html.Div([
html.H1("Dash Quickstart App"),
html.Button("Click Me", id="my-button", n_clicks=0),
html.Div(id="my-output", children="No clicks yet.")
])
# Define a callback to update the output based on button clicks
@callback(
Output("my-output", "children"),
Input("my-button", "n_clicks"),
State("my-output", "children"),
prevent_initial_call=True # Prevents the callback from firing on initial load
)
def update_output(n_clicks, current_text):
if n_clicks is None:
# This branch might be reached if prevent_initial_call=False
return "No clicks yet."
return f"Button has been clicked {n_clicks} times!"
# Run the app
if __name__ == "__main__":
# In production, set debug=False and use a production-ready WSGI server
app.run_server(debug=True)