Day 35 of 50 Days of Python: Streamlit for Building Simple Data Apps
Part of Week 5: Data Analysis and Visualisation
Welcome to Day 35 and the final stop of this week’s journey!
Yesterday, we explored how to communicate insights using Jupyter Notebooks. Today, we’re taking it a step further by transforming our static notebooks into interactive web applications using Streamlit.
Streamlit is a Python-based framework that lets you build data dashboards and interactive tools with minimal code. No need to be a front-end engineer. If you know Python and Pandas, you’re already most of the way there in all fairness.
Getting Started with Streamlit
Installing Streamlit is easy within your Python environment:
pip install streamlit
Running Your First “App”
Create a Python file, for example app.py and then run the following code:
streamlit run app.py
What this is launch your app in a brower window automatically, none of that madness with trying to figure out how to launch flask apps.
Streamlit Basics
Streamlit works by chaining Python scripts with UI widgets, think sliders, dropdowns, text inputs and every time you change something, the app reruns from top to bottom (Full refresh).
Core Functionality
import streamlit as st
st.title("My First App")
st.write("Hello, Streamlit!")
Adding Interactivity
name = st.text_input("Enter your name")
st.write(f"Hello, {name}!")
Visualise Data
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'x': [1, 2, 3],
'y': [10, 20, 30]
})
fig, ax = plt.subplots()
ax.plot(df['x'], df['y'])
st.pyplot(fig)
Real-World Example: Interactive Sales Dashboard
A simple, “sales” dashboard.
Sample Dataset
import pandas as pd
data = {
'Region': ['North', 'South', 'East', 'West'],
'Sales': [30500, 35600, 28300, 33900]
}
df = pd.DataFrame(data)
App Code
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
st.title("
Regional Sales Dashboard")
# Data
data = {
'Region': ['North', 'South', 'East', 'West'],
'Sales': [30500, 35600, 28300, 33900]
}
df = pd.DataFrame(data)
# Selectbox
selected_region = st.selectbox("Choose a region:", df['Region'])
# Filtered View
filtered_sales = df[df['Region'] == selected_region]
st.write(f"Sales in {selected_region}: ${int(filtered_sales['Sales'])}")
# Visualisation
fig, ax = plt.subplots()
ax.bar(df['Region'], df['Sales'], color='skyblue')
ax.set_title("Sales by Region")
st.pyplot(fig)
Now you’ve built a mini-dashboard where users can filter and view data dynamically, all with less than 30 lines of Python! Granted it’s not “real” data but they’re real metrics that BI and Insight analysts report on.
Next Up: Week 6 - Advanced Topics
We begin with Day 36: Introduction to Machine Learning Concepts, where we’ll demystify how machines learn from data, explore key concepts like features, labels, overfitting, and model evaluation, and get our hands clean with some real-world ML examples. As well as, plug some pieces I’ve already covered over at: Zero to Neural: Exploring AI | Jonathon Kindred
So see you for the next one and… Happy coding.