Menu

Building Dashboards with Streamlit

Building Dashboards with Streamlit

Streamlit is another popular way to turn Python scripts into web apps. It is known for being very simple, since you write a normal script and Streamlit handles the page. This topic shows how to build and share a dashboard quickly.

a. Introduction to Streamlit

Streamlit turns a Python script into an interactive web app with almost no extra code. You do not need to learn HTML or callbacks. Each line of your script that uses a Streamlit command adds an element to the page.

Install it with pip install streamlit, save your code in a file such as app.py, and run it from the terminal.

streamlit run app.py
A basic app looks like this.
import streamlit as st
import pandas as pd
st.title("Sales Dashboard")
df = pd.read_csv("sales.csv")
st.write("Here is the data")
st.dataframe(df)

Streamlit reruns your script from top to bottom whenever the user changes something, which keeps the logic simple.

b. Adding Widgets and Filters

Widgets are the controls users interact with. Streamlit offers sliders, select boxes, checkboxes, date pickers, and more. Each widget returns its current value, which you can use directly in your code.

region = st.selectbox("Choose a region", df["region"].unique())
min_sales = st.slider("Minimum sales", 0, 1000, 100)
filtered = df[(df["region"] == region) & (df["sales"] >= min_sales)]

Place filters in a sidebar so the main area stays clean. Use st.sidebar.selectbox() for this.

Because the script reruns on each change, the filtered data and charts update right away. This makes a responsive dashboard with very little code.

c. Displaying Charts and Data Tables

Streamlit works with the main plotting libraries. Use st.plotly_chart() for Plotly figures and st.pyplot() for Matplotlib figures. It also has simple built-in charts, like st.line_chart().

import plotly.express as px
fig = px.line(filtered, x="date", y="sales", title="Sales Over Time")
st.plotly_chart(fig, use_container_width=True)
st.metric("Total Sales", f"{filtered['sales'].sum():,.0f}")

The st.metric() function shows a key number in a clean card. You can arrange content in columns with st.columns() and in tabs with st.tabs().

Use caching for slow steps, such as loading data. The @st.cache_data decorator stores the result so it is not recomputed on every rerun.

d. Sharing Your Streamlit App

The easiest way to share an app is Streamlit Community Cloud. You push your code and a requirements.txt file to a GitHub repository, connect it, and the app gets a public link. It is free for public projects.

You can also deploy with Docker on cloud platforms, or host it inside your company network. Choose based on who should see the app and how sensitive the data is.

Never put secrets such as passwords in your code. Use Streamlit's secrets feature or environment variables. Also check that the data you share is safe to make public.


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