DS Streamlit Dashboards

Streamlit is a Python library that turns a script into an interactive web app. You write normal Python, and Streamlit adds buttons, sliders, tables, and charts to a web page. No web development skills are required. Data scientists use it to share results with teammates who never open a notebook.

A dashboard works like a car dashboard. A glance shows speed, fuel, and warnings, and a few controls let the driver adjust settings.

Install and Run

pip install streamlit
streamlit run app.py

The second command starts a local server and opens the app in your browser. Edit the file, save it, and the page offers a rerun button.

The Page Layout

+--------------------------------------------------------+
|  Title and short description                           |
+-------------+------------------------------------------+
|  SIDEBAR    |   Metric cards:  Sales | Orders | Growth |
|  Filters:   +------------------------------------------+
|  - Region   |   Chart: sales over time                 |
|  - Year     +------------------------------------------+
|  - Product  |   Table: top ten products                |
+-------------+------------------------------------------+

Your First App

import streamlit as st

st.title("Hello Dashboard")
st.write("Streamlit turns Python into a web page.")

name = st.text_input("Your name")
if name:
    st.success("Welcome, " + name)

Each st command draws one element on the page. Order in the script equals order on the screen.

Core Elements

CommandWhat It Shows
st.title, st.headerHeadings
st.writeText, numbers, tables, and charts
st.dataframeScrollable, sortable table
st.metricLarge number with a change arrow
st.line_chart, st.bar_chartQuick charts
st.plotly_chartPlotly charts

Add Controls

WidgetCommand
Dropdownst.selectbox
Multiple choicest.multiselect
Sliderst.slider
Checkboxst.checkbox
Buttonst.button
File uploadst.file_uploader

A Complete Sales Dashboard

import streamlit as st
import pandas as pd
import plotly.express as px

st.set_page_config(page_title="Sales Dashboard", layout="wide")

@st.cache_data
def load_data():
    return pd.read_csv("sales.csv", parse_dates=["date"])

df = load_data()

st.title("Sales Dashboard")

region = st.sidebar.multiselect(
    "Region", df["region"].unique(), default=list(df["region"].unique()))
filtered = df[df["region"].isin(region)]

col1, col2, col3 = st.columns(3)
col1.metric("Total Sales", int(filtered["amount"].sum()))
col2.metric("Orders", len(filtered))
col3.metric("Average Order", round(filtered["amount"].mean(), 1))

monthly = filtered.groupby(filtered["date"].dt.to_period("M").astype(str))["amount"].sum().reset_index()
fig = px.line(monthly, x="date", y="amount", title="Monthly Sales")
st.plotly_chart(fig, use_container_width=True)

st.dataframe(filtered.sort_values("amount", ascending=False).head(10))

How Streamlit Runs Your Script

 User moves a slider
        |
        v
 Streamlit reruns the script from top to bottom
        |
        v
 Page updates with new values

This rerun model keeps code simple, though slow steps repeat. The cache_data decorator stores results of expensive functions, such as file loading, so reruns finish fast.

Deploy the App

  • Push the code and a requirements.txt file to a code-sharing site.
  • Connect the repository to Streamlit Community Cloud, which hosts apps for free.
  • Share the generated link with your team.
  • Keep passwords and keys in the secrets settings and out of the code.

Dashboard Design Tips

  • Place the most important number at the top left.
  • Limit a page to four or five visuals.
  • Put filters in the sidebar and keep their names simple.
  • Show the data refresh date.
  • Protect personal data, and share only what viewers need.

Example: A Store Manager Dashboard

A store manager wants daily answers without writing code. The analyst builds a Streamlit app with a date slider, a product dropdown, and three metric cards. The manager opens one link each morning. Moving the slider updates every number and chart in seconds. Requests for custom reports drop sharply, because the manager explores the data directly.

Streamlit versus Notebooks

FeatureNotebookStreamlit App
AudienceAnalysts who read codeAnyone with a link
InteractionEdit and rerun cellsClick filters and sliders
Code visibilityAlways visibleHidden from viewers
Best useExploration and teachingSharing finished insights

Layout Tools

Streamlit offers simple layout helpers. Columns place elements side by side. Tabs split a page into named views, such as Overview and Details. Expanders hide long notes until a viewer clicks. The sidebar keeps filters apart from results. A tidy layout helps viewers find answers without scrolling through clutter.

Troubleshooting Tips

  • Apps that feel slow usually reload big files on every rerun, so wrap loaders in cache_data.
  • Missing library errors on the host mean the requirements.txt file lacks a package.
  • Widgets that reset unexpectedly need session_state to remember choices.
  • Pages with huge tables load faster when you show only the top rows.

Key Points

  • Streamlit converts Python scripts into web apps with a few commands.
  • Widgets let viewers filter data, and the script reruns after every change.
  • Caching and free cloud hosting make sharing quick.