Lesson 36 · Data visualisation in python
4 - Advanced Dash - Layouts, Callbacks, Hosting
Welcome to the world of interactive web apps with Python! Today, you will dive deep into Dash: Build complex layouts easily Connect components with…
- CourseData visualisation in python
- Lesson36 of 34
- Video15 min
- FormatJupyter notebook · 15 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbAdvanced Dash: Layouts, Callbacks, and Hosting#
Welcome to the world of interactive web apps with Python!
Today, you will dive deep into Dash:
- Build complex layouts easily
- Connect components with interactive callbacks
- Display time series data from real datasets
- Prep apps for sharing and secure hosting
Even if you are brand new to Dash, just follow along.
Let us create some magic with just Python and a web browser!
# Import core tools for Dash app building
import warnings; warnings.filterwarnings("ignore")
import dash
from dash import dcc, html, Input, Output
import plotly.express as px
import pandas as pd
What is a Dash App?#
A Dash app is a Python program that runs in your browser.
It lets you:
- Display data
- Make beautiful interactive charts
- Respond to clicks or text input in real time
Let us see how simple the first step can be!
# Let's start and preview a blank Dash app
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1('Hello, Dash!'),
html.P('Welcome to your first app.')
])
# This will not run here, but locally use:
# app.run_server(debug=True)
# Data setup: Load a real time series dataset for live Dash experience
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv"
df = pd.read_csv(url)
print(df.shape)
print(df.head())
# Quick line chart of airline passengers over time
fig = px.line(df, x='Month', y='Passengers', title='Airline Passengers Over Time')
fig.show()
Building Layouts the Easy Way#
Dash uses 'layout' to decide what you see and where.
Simple layouts use html.Div, html.H1, html.P, and more.
For dashboards, you nest, stack, and style with these building blocks.
Let us create a layout with a chart and instructions.
# Layout with chart and helper text
app.layout = html.Div([
html.H2('Line chart example'),
dcc.Graph(figure=fig),
html.P('Try adding your own numbers below!')
])
# Adding input box for interactive dashboard
app.layout = html.Div([
html.H2('Try typing a note:'),
dcc.Input(id='input-text', type='text', value=''),
html.Div(id='output-text')
])
# Making things react: a simple callback
@app.callback(
Output('output-text', 'children'),
Input('input-text', 'value')
)
def update_output(value):
return f'You typed: {value}'
More Layout Tricks!#
Dash layouts can get complex fast.
You can put charts side by side with html.Div(style={'display': 'flex'}).
Let us build a side-by-side layout for comparisons.
# Side by side charts using flex layout
layout_row = html.Div([
html.Div([dcc.Graph(figure=fig)], style={'width': '48%', 'display': 'inline-block'}),
html.Div([html.P('Second chart or info goes here.')], style={'width': '48%', 'display': 'inline-block'})
], style={'display': 'flex', 'justify-content': 'space-between'})
app.layout = layout_row
Making Apps Respond to More Than One Input#
Many apps need dropdowns, sliders, or multiple controls.
You can connect as many as you need with Dash's callback system.
Let us make a callback that responds to both dropdown and text input.
# Layout with dropdown and text input
app.layout = html.Div([
dcc.Dropdown(
options=[
{'label': 'Passengers', 'value': 'Passengers'},
{'label': 'Month (as text)', 'value': 'Month'}
],
value='Passengers',
id='column-dropdown'
),
dcc.Input(id='input-text', type='text', value=''),
html.Div(id='multi-output')
])
)
])
# Callback with multiple inputs: combines dropdown and input box
@app.callback(
Output('multi-output', 'children'),
Input('column-dropdown', 'value'),
Input('input-text', 'value')
)
def update_multi(selected_column, user_text):
if selected_column == 'Passengers':
col_preview = df[selected_column].head().to_list()
else:
col_preview = df[selected_column].astype(str).head().to_list()
return f'Column preview: {col_preview}, You typed: {user_text}'
# Showing errors safely: what if a column is missing?
try:
df['FakeColumn']
except KeyError as e:
print("Column not found! Error was:", e)
Deploying Your Dash App Online#
You made something cool!
Do you want to share it with friends or colleagues?
You can host on Dash's free platform, Heroku, or similar services.
Just make sure your app uses:
- app.layout
- app.run_server()
and push to a safe public GitHub.
# Example: include this at the bottom of your script for cloud hosting
if __name__ == "__main__":
app.run_server(debug=False, host='0.0.0.0', port=8050)
# Challenge: let the user choose a date range and see that part of the data
from dash import dash_table
import datetime
app.layout = html.Div([
dcc.DatePickerRange(
id='date-range',
start_date=df['Month'].iloc[0],
end_date=df['Month'].iloc[-1]
),
dash_table.DataTable(id='table-display'),
])
])
# Callback: update the table to match user-selected date range
@app.callback(
Output('table-display', 'data'),
Output('table-display', 'columns'),
Input('date-range', 'start_date'),
Input('date-range', 'end_date')
)
def update_table(start, end):
dff = df[(df['Month'] >= start) & (df['Month'] <= end)]
columns = [{'name': col, 'id': col} for col in dff.columns]
data = dff.to_dict('records')
return data, columns
Troubleshooting Tips#
- If your app does not load: Check for missing commas or mismatched brackets.
- If nothing updates: Make sure callback ids match your layout's.
- For blank charts: Try printing your data before the charting step.
- To see your app on the web: Use the host='0.0.0.0' setting.
You will get more comfortable as errors pop up fixing them is real learning.
# Extra tip: Add a theme for better looks
import dash_bootstrap_components as dbc
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.CYBORG])
You Did It! Let Us Review#
- Built flexible Dash layouts
- Connected widgets with powerful callbacks
- Loaded and displayed real time series data
- Let users filter and interact all in the browser
You can now expand or remix everything you have learned.
Thanks for Learning Dash!#
Try more challenges, remix these apps, and share your dashboards.
Subscribe for more beginner-friendly Python and web apps.
See you in the next fun lesson!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



