Streamlit Tutorial: Turn a Python Script Into a Dashboard

Quick answer
Streamlit turns a plain Python script into a web app: you write st.title, st.dataframe, and st.selectbox in a .py file, run streamlit run app.py, and the dashboard opens in the browser, with no HTML, no JavaScript, and no separate front end to build. This post builds a shop orders dashboard from six real orders and shows the one column-name error that stops almost every beginner.
A shop owner in Karachi keeps orders in a spreadsheet and asks every morning: how much did each city bring in, and which orders came from Karachi? The spreadsheet answers it, but only after filtering, scrolling, and adding columns by hand. A Flask app would answer it properly, but that means routes, templates, and a front end for one table and one filter. Streamlit sits between those two options: the whole dashboard is a short Python script, the table and the filter are two function calls, and the owner opens it in a browser. That is the whole pitch; the demo below tests it with real orders.
What I would check before using it
My first check is who the dashboard is for. Streamlit fits an internal tool: a team, a shop owner, a colleague who needs to see numbers. It is a weaker fit for a public site, because the layout is Streamlit's, not yours. My judgement: pick it when the audience is small and known, and skip it when the page itself is the product.
My second check is what the data looks like. Streamlit is happiest with a table: a pandas DataFrame in, a sortable table out. The Pandas Explained: Tables in Python, Without the Spreadsheet post covers the table side, and the Polars Python Tutorial: Fast DataFrames Without Pandas post covers a faster frame if the data grows. A live feed or heavy joins need plumbing first; Streamlit is only the last mile.
My third check is Streamlit vs Flask, the comparison most Python developers face. Flask gives you an API, routes, and full control over the front end, and the FastAPI Explained: Python APIs at Full Speed post covers the API side. Streamlit gives you a dashboard in an afternoon and takes the front end off your hands. My judgement: reach for Flask or FastAPI when other programs call your service, and Streamlit when a human looks at numbers.
My fourth check is deployment and sharing. A Streamlit app is a Python process that must keep running somewhere, so sharing means a server, Streamlit Community Cloud, or a machine that stays on. A static page can live anywhere; this cannot. My verdict, as judgement: use Streamlit for internal dashboards built from Python data, and Flask or FastAPI for APIs and public sites. The recommendation flips if the audience goes public, the design must be custom, or the app needs user accounts.
Six orders, one dashboard, and the column that did not exist
The demo is a shop orders dashboard: six orders, a table, city totals, and a filter. The orders list is plain Python, in the style of the Python List Comprehensions Explained With Real Examples post, and the whole app is one file, app.py:
import pandas as pd
import streamlit as st
orders = [
{"order": 201, "customer": "Ayesha", "city": "Karachi", "item": "kurta", "total": 4200},
{"order": 202, "customer": "Bilal", "city": "Lahore", "item": "sneakers", "total": 8900},
{"order": 203, "customer": "Fatima", "city": "Islamabad", "item": "handbag", "total": 6400},
{"order": 204, "customer": "Usman", "city": "Karachi", "item": "jacket", "total": 7300},
{"order": 205, "customer": "Zara", "city": "Lahore", "item": "shawl", "total": 3100},
{"order": 206, "customer": "Hassan", "city": "Islamabad", "item": "wallet", "total": 2800},
]
df = pd.DataFrame(orders)
st.title("Shop Orders Dashboard")
st.write("Six recent orders from Karachi, Lahore, and Islamabad.")
city = st.selectbox("Filter by city", ["All"] + sorted(df["city"].unique()))
shown = df if city == "All" else df[df["city"] == city]
st.dataframe(shown)
st.subheader("Totals by city")
st.write(df.groupby("city")["total"].sum())
st.metric("Grand total (Rs)", int(df["total"].sum()))
Streamlit 1.67.0 went into a fresh virtual environment with pip install streamlit, on Python 3.12.3, and the server started headless on port 8501:
$ streamlit run app.py --server.headless true --server.port 8501
Uvicorn server started on :::8501
Local URL: http://localhost:8501
$ curl -s http://localhost:8501/_stcore/health
ok
Then the thing that went wrong. The first filter line read df["City"], capital C, while the column is "city", lowercase. Streamlit stopped at the selectbox and showed this error:
KeyError: 'City'
File "app.py", line 18, in <module>
city = st.selectbox("Filter by city", ["All"] + sorted(df["City"].unique()))
File "pandas/core/frame.py", line 4330, in __getitem__
indexer = self.columns.get_loc(key)
File "pandas/core/indexes/base.py", line 3823, in get_loc
raise KeyError(key) from err

Nothing else rendered past that line: Streamlit runs the script top to bottom, and an exception stops it cold. The fix is one character of case, df["city"]. After the fix, every element rendered and the run finished cleanly. Totals: Karachi Rs 11,500, Lahore Rs 12,000, Islamabad Rs 9,200, grand total Rs 32,700. Picking Karachi leaves two rows: order 201, Ayesha, Rs 4,200, and order 204, Usman, Rs 7,300. The Matplotlib: Charts in Python Without Opening a Spreadsheet post covers the next step: drawing those totals as a chart.
Common mistakes and how to fix them
First, the wrong column name. The symptom is KeyError: 'City' and a page that stops at the widget, because pandas column names are case sensitive and the frame holds "city", not "City". The fix is to match the case exactly, or print df.columns first.
Second, running the app with plain Python. The symptom is a run that exits with code 0 and no page, plus warnings about a missing ScriptRunContext and a hint to run streamlit run app.py. The fix is to always start the app with streamlit run app.py, never python app.py.
Third, installing Streamlit in the wrong Python. The symptom is ModuleNotFoundError: No module named 'streamlit' when the app starts, because the package went into one environment and the command ran in another. The fix is one virtual environment for the project, activated before both the install and the run.
Fourth, expecting a widget choice to survive on its own. The symptom is a filter that resets to "All" after another widget changes, because Streamlit reruns the whole script on every interaction. The fix is st.session_state for anything that must persist across reruns.
Frequently Asked Questions
Is Streamlit free? Yes. Streamlit is open source, and Streamlit Community Cloud hosts small apps free. Bigger ones run on your own server, where you pay for the machine, not the framework.
Can Streamlit replace Flask? Not for the same job. Flask builds APIs that other programs call. Streamlit builds dashboards that humans look at. Many teams run both: FastAPI serves the data, Streamlit shows it.
Do I need to know HTML or JavaScript? No. The whole dashboard here is Python: st.title for the heading, st.dataframe for the table, st.selectbox for the filter. HTML and CSS only matter if you want styling beyond the defaults.
How do I share the dashboard with someone else? The app must keep running on a machine others can reach, a server you run or Streamlit Community Cloud. Send the link, not the file: the .py file alone opens nothing in a browser.
What to remember
- Streamlit turns a Python script into a web app: st.title, st.dataframe, and st.selectbox are the whole front end.
- Start it with streamlit run app.py, never python app.py, and install it in the same venv you run it from.
- Column names are case sensitive: df["City"] raises KeyError when the column is "city".
- Every interaction reruns the whole script, so persist state in st.session_state.
- Use Streamlit for dashboards humans look at, and Flask or FastAPI for APIs other programs call.
Where this leaves you
Streamlit kept the promise the demo tested: one short script, one command, and a shop owner can filter six real orders by city and see the totals. The error was useful too: a KeyError on a column name teaches the mental model, that the script runs top to bottom and the page is whatever it built before it stopped. Try it on a table you already have, a CSV of your own, and see how far one file takes you before you need anything heavier.
Further reading
- Streamlit documentation
- Streamlit API reference
- Streamlit: Deploy your app
- Pandas Explained: Tables in Python, Without the Spreadsheet
- Polars Python Tutorial: Fast DataFrames Without Pandas
- Matplotlib: Charts in Python Without Opening a Spreadsheet
- Python List Comprehensions Explained With Real Examples
- FastAPI Explained: Python APIs at Full Speed
Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.
Comments
Join the discussionNo comments yet. Be the first to comment.





