Python Tutorial
Dash Controls
Dropdown, slider, range slider, and date picker are the usual filters for a dashboard.
Common dcc Inputs
from dash import dcc, html
layout = html.Div([
dcc.Dropdown(id="metric", options=["tip", "total_bill"], value="tip"),
dcc.Slider(id="bins", min=5, max=40, step=5, value=20),
dcc.RangeSlider(id="bill", min=0, max=60, value=[10, 40]),
dcc.DatePickerRange(id="dates"),
dcc.Checklist(
id="days",
options=["Thur", "Fri", "Sat", "Sun"],
value=["Sat", "Sun"],
),
])Read each control's value (or start_date / end_date) from Input in a callback and filter your DataFrame before you draw the graph.
📘 Real-World Deep Dive
Knowing <strong>Dash Controls (Dash)</strong> well is what turns Dash from a curiosity into a daily tool — you'll reach for it in nearly every real project.
Real-Life Scenario
An end-to-end usage of Dash Controls that you'd actually see in a data pipeline or analytics notebook.
Real-Life Example
from dash import dcc
controls = html.Div([
dcc.Dropdown(id="region",
options=[{"label":"NA","value":"na"},
{"label":"EU","value":"eu"}],
value="na"),
dcc.RangeSlider(0, 100, 5, value=[20, 80], id="range"),
])Expected Output
(no output)Common mistakes
- Callbacks must declare
Output/Input/Statein the right order; mismatched component IDs silently produce empty updates. @app.callbackbody cannot read HTML — return only data, render in the layout.- Each request lifecycle can fire callbacks many times — debounce heavy computation with
dcc.Intervalor caching. - Treating Dash Controls as a black box without reading the docs — the API has subtle defaults that bite when you scale.
🚀 Performance & Best Practices
- Use
functools.lru_cacheon expensive data-loaders prefixed by their inputs. - Switch large tables to
dash_table.DataTablewithvirtualization=True. - Run in production with
gunicorn --workers 4 --threads 8and proxy through nginx. - When working with Dash, prefer vectorised / batched operations over Python loops.
🧪 Try It Yourself
- Reproduce the snippet on a representative slice of your own data.
- Profile the snippet with
cProfileortimeitand find the single biggest improvement. - Generalise the snippet into a small, reusable function you can drop into future projects.