📱
Persona
Samuel — Analyst, M-KOPA Solar Kisumu

You're the only analyst at M-KOPA Solar, a fast-growing fintech doing solar-powered loans across Kenya. Every Monday, your manager asks: "How's the portfolio?—default rate, disbursals, which counties are struggling?" You've built a Jupyter notebook that reads 4,000 loan records and spits out tables and charts. But rerunning it manually takes 20 minutes, and sharing results means emailing screenshots. You need a dashboard the leadership team can bookmark and check anytime, from any device.

4,000
Loan Records
10
Counties
190M KSh
Portfolio Size

Why Streamlit? Speed over scaffolding

Streamlit lets you build web dashboards using Python alone. No HTML, no CSS, no JavaScript. Write st.title(), st.metric(), st.plotly_chart()—and the browser renders interactivity for free.

Tool Code needed Best for Deployment
Jupyter Notebook Python + Markdown Exploratory analysis (not dashboards) Manual share (cells don't auto-update)
Tableau / Power BI No code (drag-drop) Enterprise reports with shared data sources Expensive licenses + server costs
Flask / Django Python + HTML + CSS + JavaScript Full-featured web apps with auth, databases Requires DevOps (AWS, Heroku, etc.)
Streamlit Python only (no web framework) Data dashboards, rapid prototyping, internal tools Free tier on Streamlit Cloud (1 click)

The pattern: Use Streamlit when you have a notebook and a deadline. You'll deploy a dashboard in hours, not weeks.

Install & hello world

Streamlit is a pip package. In 3 lines, you'll have an app running locally.

Install & create
pip install streamlit pandas
Then save this as app.py and run streamlit run app.py. Streamlit opens a browser tab automatically.
Python · app.py (hello world)

That's it. Streamlit reloads the app in the browser every time you save the file. No server restart needed.

Layout building blocks: KPIs, headers, and metrics

A dashboard needs hierarchy. Streamlit provides st.title(), st.header(), st.metric(), and st.divider() for structure:

Python · kpis.py
Output: 4 metric cards displaying portfolio KPIs
Mockup of KPI cards in Streamlit
How it works: st.columns(4) creates a row of 4 equal-width containers. with col1: switches context—everything after it appears in that column. st.metric() renders a big, bold number perfect for dashboards.

Embedding charts: Seaborn, Plotly, and st.pyplot

Streamlit handles matplotlib (Seaborn) via st.pyplot() and Plotly via st.plotly_chart(). Both are one-liners:

Python · charts_in_app.py
Python · embedded_chart_example.py (from notebook)
Two-chart layout: Seaborn (left) and Plotly (right) side-by-side
Bar chart of default rate by county
Why Plotly here? st.plotly_chart() is interactive—users can hover for values, click legends to toggle series, and zoom. Seaborn (matplotlib) charts are static, rendered as PNG images.

Caching: Stop reloading the CSV on every keystroke

By default, Streamlit reruns your entire script every time a user interacts with a widget. If you're loading a 100MB CSV, that's slow. Decorate your data-loading function with @st.cache_data to memoize it:

Python · caching.py
How it works: @st.cache_data stores the DataFrame in memory. Next run, Streamlit skips the load_data() call and returns the cached result instantly. If the file changes on disk, Streamlit auto-detects and recaches.

Advanced layout with st.columns and st.tabs

For richer layouts, use st.columns() to create grids, or st.tabs() to organize content into tabs. This is how you mimic a professional dashboard:

Python · advanced_layout.py
Full dashboard wireframe: sidebar, KPI row, and 2-column chart layout
Wireframe of complete dashboard

Deploy to Streamlit Community Cloud: One-click sharing

Once your app works locally, deploy it free on Streamlit Community Cloud. Leadership gets a URL they can bookmark.

Step 1: Prepare your repo

Push your code to GitHub:

Git setup
git init
git add app.py mkopa_portfolio.csv requirements.txt
git commit -m "Initial dashboard"
git remote add origin [your GitHub repo]
git push -u origin main

Step 2: Create requirements.txt

Streamlit Cloud reads this to install dependencies:

Text · requirements.txt

Step 3: Connect Streamlit Cloud

Go to share.streamlit.io, sign in with GitHub, and click "New app". Select your repo, branch, and app.py. Streamlit deploys automatically and gives you a live URL.

Pro tip: Streamlit Community Cloud is free for public repos. For private repos or production use, upgrade to a paid tier. Redeploys happen automatically when you push to GitHub—no Docker, no DevOps.

Common pitfalls & how to avoid them

Pitfall 1: Widget state resets on rerun

When a user selects a filter, Streamlit reruns the script. If you have count = 0 at the top, it resets to 0. Use st.session_state to persist values:

Python · session_state.py

Pitfall 2: Widget keys colliding

If you loop over items and create widgets without unique keys, Streamlit will error. Always use a key:

Python · widget_keys.py

Pitfall 3: Loading large CSV every time

Use @st.cache_data (covered earlier). Without it, a 100MB CSV reloads on every keystroke, causing lag.

Streamlit API cheatsheet

Function Use case Example
st.title() Page title (h1) st.title('Dashboard')
st.header() Section heading (h2) st.header('Metrics')
st.subheader() Sub-heading (h3) st.subheader('By County')
st.write() Generic output (text, df, chart) st.write(df)
st.metric() KPI card (big number + label) st.metric('Active', '3,680')
st.dataframe() Interactive table (sortable, searchable) st.dataframe(df)
st.matplotlib() Seaborn / matplotlib charts st.pyplot(fig)
st.plotly_chart() Plotly interactive charts st.plotly_chart(fig)
st.columns(n) n-column layout col1, col2 = st.columns(2)
st.tabs() Tabbed interface tab1, tab2 = st.tabs(['A', 'B'])
st.sidebar.selectbox() Dropdown in sidebar st.sidebar.selectbox('County', df['county'].unique())
st.sidebar.multiselect() Multi-select in sidebar st.sidebar.multiselect('Products', products)
st.sidebar.date_input() Date picker in sidebar st.sidebar.date_input('Start')
@st.cache_data Memoize function (cache between reruns) @st.cache_data
def load_data(): ...
🧪
Quiz — 7 questions
Q1: What does @st.cache_data do?
Memoizes function output so data loads only once, speeding up reruns
Saves data to disk for offline access
Encrypts sensitive dataframes
Converts pandas DataFrames to Parquet
Q2: Which Streamlit function renders a big number ideal for dashboards?
st.write()
st.metric()
st.number_input()
st.dataframe()
Q3: How do you create a 3-column layout in Streamlit?
st.layout('columns=3')
col1, col2, col3 = st.columns(3)
st.grid(3, 1)
st.sidebar.columns(3)
Q4: When does Streamlit rerun the entire script?
Every 5 seconds automatically
When a user interacts with a widget (button, selectbox, etc.)
Only when you manually refresh the browser
When you push to GitHub
Q5: Which chart library gives you interactive hover/zoom in Streamlit?
Matplotlib
Seaborn
Plotly
Both Matplotlib and Seaborn equally
Q6: How do you deploy a Streamlit app for free?
Push to Docker Hub and rent a server
Push to GitHub and connect via Streamlit Community Cloud
Email the .py file to users
Use only Jupyter Notebook (no web app needed)
Q7: What goes in requirements.txt for a Streamlit app?
Only your app's filename (app.py)
Python package names and versions (streamlit, pandas, plotly)
GitHub secrets and API keys
CSS and HTML files
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You've mastered dashboards.
Now deploy to production.

Join the Best AI Program: The AI-Era Analyst — 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Analyst →