Mathew K Analytics

Lesson 15 · Data analytics zero to hero

Build a Streamlit Dashboard in Python | Data Analytics #15

Video fifteen of the 30-part series: turning a real dataset into a genuine interactive web dashboard, entirely in Python. We're using the real Sample…

What you'll learn

Datasets used in this lesson

Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.

📓 Full notebook

Download .ipynb

Data Analytics Zero to Hero, Video 15: Building a Streamlit Dashboard#

  • Video fifteen of the 30-part series: turning a real dataset into a genuine interactive web dashboard, entirely in Python.
  • We're using the real Sample Superstore dataset, a well-known real US retail sales dataset with almost 10,000 real orders.
  • Let's jump straight in.

Before You Start#

  • Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
  • Install Streamlit if you haven't already: pip install streamlit.
  • Place superstore_sales.csv in the same folder as this notebook.

Part 1: Streamlit Apps Are Scripts, Not Cells#

with open('app.py', 'w') as f:
    f.write('# Superstore Sales Dashboard\n')
    f.write('# Built step by step from a real retail dataset\n\n')
print('app.py created')
app.py created

Part 2: Page Setup and Loading Real Data#

page_setup = '''import streamlit as st
import pandas as pd

st.set_page_config(page_title='Superstore Dashboard', layout='wide')

st.title('Superstore Sales Dashboard')
st.write('An interactive view of a real US retail sales dataset, nearly 10,000 genuine orders.')

@st.cache_data
def load_data():
    df = pd.read_csv('superstore_sales.csv')
    df['Order Date'] = pd.to_datetime(df['Order Date'])
    return df

sales = load_data()
'''

with open('app.py', 'a') as f:
    f.write(page_setup)
print('Page setup and data loading written')
Page setup and data loading written

Part 3: Widgets#

widgets_code = '''
st.sidebar.header('Filters')
region_choice = st.sidebar.multiselect('Region', sorted(sales['Region'].unique()), default=sorted(sales['Region'].unique()))
category_choice = st.sidebar.selectbox('Category', ['All'] + sorted(sales['Category'].unique()))
min_sales = st.sidebar.slider('Minimum order sales ($)', 0, 500, 0)
show_raw = st.sidebar.checkbox('Show raw data table')
'''

with open('app.py', 'a') as f:
    f.write(widgets_code)
print('Widgets written')
Widgets written

Part 4: Filtering and Metrics#

filter_code = '''
filtered = sales[sales['Region'].isin(region_choice) & (sales['Sales'] >= min_sales)]
if category_choice != 'All':
    filtered = filtered[filtered['Category'] == category_choice]

col1, col2, col3 = st.columns(3)
col1.metric('Orders shown', len(filtered))
col2.metric('Total sales', f"${filtered['Sales'].sum():,.0f}")
col3.metric('Total profit', f"${filtered['Profit'].sum():,.0f}")

if show_raw:
    st.dataframe(filtered)
'''

with open('app.py', 'a') as f:
    f.write(filter_code)
print('Filtering and metrics written')
Filtering and metrics written

Part 5: Charts#

charts_code = '''
st.subheader('Sales by Sub-Category')
subcat_totals = filtered.groupby('Sub-Category')['Sales'].sum().sort_values(ascending=False)
st.bar_chart(subcat_totals)

st.subheader('Monthly Sales Trend')
monthly = filtered.set_index('Order Date')['Sales'].resample('ME').sum()
st.line_chart(monthly)
'''

with open('app.py', 'a') as f:
    f.write(charts_code)
print('Charts written')
Charts written

Part 6: Layout with Tabs#

tabs_code = '''
tab1, tab2 = st.tabs(['By Region', 'About This Data'])

with tab1:
    region_totals = filtered.groupby('Region')['Profit'].sum().sort_values(ascending=False)
    st.bar_chart(region_totals)

with tab2:
    st.write('This dashboard uses the real Sample Superstore dataset, a well-known US retail sales dataset.')
    st.write(f"Date range in the full dataset: {sales['Order Date'].min().date()} to {sales['Order Date'].max().date()}.")
'''

with open('app.py', 'a') as f:
    f.write(tabs_code)
print('Tabs written')
Tabs written

Part 7: Download Button and Validation#

download_code = '''
st.sidebar.divider()
csv_data = filtered.to_csv(index=False).encode('utf-8')
st.sidebar.download_button(
    label='Download filtered data as CSV',
    data=csv_data,
    file_name='filtered_superstore_sales.csv',
    mime='text/csv',
)

st.divider()
st.caption('Built with Streamlit and pandas, on the real Sample Superstore dataset.')
'''

with open('app.py', 'a') as f:
    f.write(download_code)
print('Download button and footer written')
Download button and footer written
import py_compile
py_compile.compile('app.py', doraise=True)
with open('app.py') as f:
    final_line_count = len(f.readlines())
print(f'app.py is valid and complete: {final_line_count} lines')
app.py is valid and complete: 66 lines

Launching It For Real#

  • Open a terminal in VS Code, in the same folder as app.py and superstore_sales.csv.
  • Run: streamlit run app.py
  • Streamlit opens a browser tab automatically; the sidebar filters, metrics, charts, tabs, and download button all become fully interactive over the real data.
  • Press Ctrl+C in the terminal to stop the app when you're done.

Wrap-Up: What You Learned#

  • Why Streamlit apps are scripts, not notebook cells, and building app.py progressively as a real file.
  • Loading and caching a real dataset with @st.cache_data.
  • Interactive widgets: multiselect, selectbox, slider, and checkbox, populated from the real data itself.
  • Metrics, columns, and dataframe for displaying real filtered results.
  • Charts with bar_chart and line_chart, including a real resampled monthly trend.
  • Layout with tabs, and a live download button tied to the real filtered data.
  • All built on the real, nearly 10,000-row Sample Superstore dataset. Video sixteen starts a SQL block: fundamentals for data analysts, using Python's own sqlite3, no separate database software required. Subscribe so it lands automatically see you there.

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.