Data Visualization
Plotly + Dash
What is Dash?
Plotly creates interactive charts.
Dash connects those charts together and turns them into an interactive website / dashboard.
Plotly → Creates charts
Dash → Combines charts + filters + KPIs into a full web app
Think of Plotly as individual furniture pieces and Dash as the complete furnished room.
Building a House Analogy
- Python → Land
- Pandas → Workers handling data
- Plotly → Interior designer creating graphs
- Dash → Architect combining everything into one dashboard
You first learn Plotly to make good charts. Then you learn Dash to put those charts into a live, interactive website that users can filter and explore.
Install and Import
Python
# Install the libraries (quiet mode)
# !pip install plotly dash -q
import pandas as pd # work with tabular data
import numpy as np # numerical operations
import plotly.express as px # quick interactive charts
import plotly.graph_objects as go # more control over charts
from dash import Dash, dcc, html, Input, Output, callback
Dash → Creates the applicationdcc → Dash Core Components (Graph, Dropdown, Slider…)html → Create webpage elements (H1, Div, P…)Input → Tells Dash: “Watch this component for changes”Output → Tells Dash: “Update this component after something changes”
Important Dash Components
| Component | Purpose |
|---|---|
html.H1() | Main heading |
html.P() | Paragraph text |
html.Div() | Container (like a box) |
dcc.Graph() | Displays a Plotly figure |
dcc.Dropdown() | Dropdown filter |
dcc.RangeSlider() | Slider for numbers |
Almost every interactive element you see in a Dash dashboard is either an
html element or a dcc component.
Basic Dashboard Layout
Python
app = Dash()
app.layout = html.Div([
html.H1("Bank Marketing Dashboard"),
html.P("Select a Job"),
dcc.Dropdown(
id="job-dropdown",
options=["Manager", "Technician", "Admin"],
value="Manager"
),
dcc.Graph(id="balance-chart")
])
id is extremely important.The
id is how the callback knows which component to watch and which component to update.
How Callbacks Work (Line by Line)
Python
@callback(
Output("balance-chart", "figure"), # What should be updated?
Input("job-dropdown", "value") # What should we watch?
)
def update_chart(selected_job):
# 1. Filter the data based on the selected job
filtered_df = df[df["Job"] == selected_job]
# 2. Create a new chart with the filtered data
fig = px.bar(
filtered_df,
x="Job",
y="Balance",
title=f"Balance Analysis - {selected_job}"
)
# 3. Return the figure so Dash can display it
return fig
Very Important:
The
If you write
The
id used in Input() and Output() must exactly match the id you gave in the layout.If you write
"job-dropdown" in one place and "job_dropdown" in another, the callback will not work.
Complete Working Example (Corrected)
Python
from dash import Dash, html, dcc, Input, Output, callback
import pandas as pd
import plotly.express as px
# Sample data
df = pd.DataFrame({
"Job": ["Manager", "Technician", "Admin",
"Manager", "Technician", "Admin"],
"Age": [45, 32, 29, 51, 40, 35],
"Balance": [50000, 25000, 18000,
72000, 38000, 30000]
})
# Create the app
app = Dash()
# Layout
app.layout = html.Div([
html.H1("Bank Customer Dashboard"),
html.P("Select Job"),
dcc.Dropdown(
id="job-dropdown", # ← This ID must match Input
options=df["Job"].unique(),
value="Manager"
),
dcc.Graph(id="balance-chart") # ← This ID must match Output
])
# Callback
@callback(
Output("balance-chart", "figure"),
Input("job-dropdown", "value")
)
def update_chart(selected_job):
filtered_df = df[df["Job"] == selected_job]
fig = px.bar(
filtered_df,
x="Job",
y="Balance",
title=f"Balance Analysis - {selected_job}"
)
return fig
# Run the app
if __name__ == "__main__":
app.run(debug=True)
What the dashboard looks like
When you change the dropdown, the bar chart updates automatically
Errors Found in the Original Notebook & Fixes
1. ID Mismatch (Most Common Error)
Wrong
Layout:
Callback:
Layout:
id="job_dropdown"Callback:
Input("job-dropdown", "value")
Fix: Both places must use exactly the same ID (either both with underscore or both with hyphen).
2. Column Name Case Sensitivity
Using
df["job"] when the real column is "Job" (or vice-versa).
Always check exact column names with
df.columns before writing filters.
3. Incomplete Callback
Some callbacks were written but never connected to a proper layout. Always make sure every Input and Output has a matching component in the layout.
Summary
- Plotly → Creates interactive charts
- Dash → Turns charts into a full interactive dashboard
- Use
htmlfor page structure - Use
dccfor interactive components - Use
@callbackto make the dashboard respond to user actions - Always keep the
idnames consistent between layout and callback
Once you understand the pattern (Layout → Callback → Filter → Update Chart), you can build almost any interactive dashboard.
Plotly + Dash
Turn interactive charts into complete web dashboards with filters and live updates.
Select any topic from the left sidebar to begin.
Start Learning →
