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.
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.
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 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)