Menu

Building Dashboards with Dash

Building Dashboards with Dash

A dashboard brings many charts and controls together on one page. Dash is a Python framework from the Plotly team for building web dashboards without writing JavaScript. This topic shows the basic parts and how to make them interactive.

a. Introduction to Dash

Dash lets you build a full web application with Python code only. It uses Plotly for charts and standard components for dropdowns, sliders, and tables. The app runs in a browser, and others can access it once it is hosted.

Install it with pip install dash. A Dash app has two main parts. The layout describes what the page looks like. The callbacks describe how the page reacts to user actions.

from dash import Dash, dcc, html
import plotly.express as px
app = Dash(__name__)
fig = px.bar(df, x="region", y="sales")
app.layout = html.Div([
html.H1("Sales Dashboard"),
dcc.Graph(figure=fig)
])
if __name__ == "__main__":
app.run(debug=True)

b. Creating Layouts and Components

The layout is built from components arranged in a tree. The html module gives standard elements such as headings, paragraphs, and containers. The dcc module gives interactive parts such as graphs, dropdowns, and sliders.

app.layout = html.Div([
html.H1("Sales Dashboard"),
dcc.Dropdown(
id="region-dropdown",
options=[{"label": r, "value": r} for r in df["region"].unique()],
value="North"
),
dcc.Graph(id="sales-chart")
])

Each component can have an id, which lets callbacks refer to it. Use CSS styles or a theme library such as Dash Bootstrap Components to arrange the page in neat rows and columns.

c. Adding Interactivity with Callbacks

A callback is a Python function that runs when an input changes. It takes the new input value and returns a new output, such as an updated chart.

from dash import Input, Output
@app.callback(
Output("sales-chart", "figure"),
Input("region-dropdown", "value")
)
def update_chart(region):
filtered = df[df["region"] == region]
return px.line(filtered, x="date", y="sales", title=f"Sales in {region}")

When the user picks a region, Dash calls this function and updates the graph. You can connect many inputs and outputs, so that one filter updates several charts.

Keep callbacks fast. If they do heavy work, load and prepare the data once outside the function and only filter it inside.

d. Deploying Your Dash App

To share your app, you need to host it on a server. Options include Render, Railway, Heroku-style platforms, and cloud services such as AWS, Azure, and Google Cloud. Many offer free or low-cost starting plans.

For production, use a proper web server such as Gunicorn rather than the built-in development server. Expose the Flask server from your app with server = app.server, and run it with a command such as gunicorn app:server.

Store keys and passwords as environment variables, not in code. Add a requirements.txt file so the host installs the right libraries. Test the app after each deployment.


Data Visualization Tutorial

WR

Wesline Rinisha



Learn via Video Course

Data Visualization Using Python Logo

Data Visualization Using Python

19 hrs



Get in Touch For Details