Sharing your work · lesson 3 of 8
Parts in this lesson
Ast.sidebar.multiselect(label, options, default=...)a filter the user changes
Bst.metric(label, value)one big number
C@st.cache_dataload the data once, not on every click

Dashboards

Last updated

A report answers this month's question. A dashboard lets someone ask the same questions again, any day, for the store or the year they choose. In this lesson you'll decide when a dashboard is worth building, plan one around a question, and build it in Streamlit: two filters, two numbers and a chart, from one Python file.

By the end of this lesson you can

  • Decide whether a dashboard or a report fits the need
  • Build a Streamlit app with filters, metrics and a chart
  • Cache the data so the app stays fast

Report or dashboard?

A dashboard is a page of numbers and charts that updates, with controls the reader can change. It's the right tool less often than people think.

reportdashboard
AnswersOne question, once, with a conclusionThe same questions, again and again
The readerReads it throughLooks up what they need
ExplainsYes: story, caveats, recommendationBarely: it shows, the reader interprets
Example"Why is Rosario shrinking?""How did my store do last month?"

The owners want both: the review (9.P) once a year, and a way for each store manager to check their store's takings any day. The second is a dashboard.

Wrong way: fourteen charts and no question

The first dashboard an analyst builds often shows everything: takings, receipts, average basket, rice, sugar, oil, by store, by day, by weekday, a map, a pie of payment methods. Fourteen charts. The store managers opened it once, couldn't find what they needed, and went back to asking the analyst by text message.

A dashboard with no question is a data dump with filters. Before building, write down:

  • Who looks at it: the four store managers.
  • How often: every Monday.
  • What they ask: "How is my store doing this year, against last year?"
  • What they'd do: order more or less stock, and tell the owners early when something's wrong.

That's two filters (store, year), two numbers (takings, change on last year) and one chart (takings by month). Sketch it on paper first. Here's the built one; try it:

The Tindahan dashboardlocalhost:8501
Takings, 2024₱3,403,023
On 2023+5.7%
0200kJFMAMJJASOND

The check: for every chart on a dashboard, name the question it answers and who asks it. If you can't, take it off.

Streamlit

Streamlit turns a Python script into a web app. No HTML, no JavaScript: each st. call adds something to the page. Here's the whole dashboard above, as dashboard.py in the project folder:

import pandas as pd
import streamlit as st


@st.cache_data
def load():
    books = pd.read_csv("data/clean/books.csv", parse_dates=["month"])
    books["year"] = books["month"].dt.year
    return books


books = load()
st.title("The Tindahan dashboard")

all_stores = sorted(books["store"].unique())
stores = st.sidebar.multiselect("Stores", all_stores, default=all_stores)
year = st.sidebar.selectbox("Year", [2024, 2023])

if not stores:
    st.warning("Choose at least one store.")
    st.stop()

chosen = books[books["store"].isin(stores)]
now = chosen.loc[chosen["year"] == year, "pesos"].sum()
before = chosen.loc[chosen["year"] == year - 1, "pesos"].sum()

left, right = st.columns(2)
left.metric(f"Takings, {year}", f"₱{now:,.0f}")
right.metric(f"On {year - 1}", f"{100 * (now / before - 1):+.1f}%")

monthly = chosen[chosen["year"] == year].groupby("month")["pesos"].sum()
st.line_chart(monthly)
  • st.sidebar.multiselect(...) draws a filter in the sidebar and returns what's chosen: a list of store names. default=all_stores starts with every store ticked.
  • st.sidebar.selectbox("Year", [2024, 2023]) returns one year.
  • st.stop() ends the run early, after a warning, if no store is chosen.
  • st.columns(2) makes two side-by-side columns, and .metric(label, value) puts a big number in each.
  • st.line_chart(monthly) draws a Series as a line, with the index along the bottom.

Run it on your computer, with the environment active:

Terminal, in the tindahan folder
(.venv) $ python -m pip install streamlit
(.venv) $ streamlit run dashboard.py

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501

Your browser opens the app. Keep the terminal open: it's the server. Ctrl+C stops it. Add streamlit to requirements.txt (10.4).

How it runs

Streamlit's model is simple, and it explains everything it does: every time the user changes a filter, the whole script runs again from the top, with the filter's new value. There are no callbacks to write.

That would mean reading the CSV on every click. @st.cache_data above load() saves what the function returns the first time it runs; after that, it hands back the saved copy. Use it on anything slow: reading files, querying a database, fitting a model. Change the data file, and use the app's menu to clear the cache (or restart it).

A dashboard on localhost is only on your computer. To share it, the usual routes are a computer on the stores' own network running streamlit run, or a hosting service. Before putting any dashboard on the internet, check what data it shows (10.6): this one shows only store totals, which is fine; one that showed card holders would not be.

Check what you learned

The owners ask: why did Rosario shrink in 2024? Report or dashboard?
Predict: a user changes the Year filter in a Streamlit app. What runs?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis