Creating Everyday Apps with Streamlit: A Data Scientist’s Guide
Introduction
As data scientists, we often dig deep into data to uncover insights. But sharing those insights in an engaging way can be a challenge. Enter Streamlit — a Python library that lets you turn your data scripts into interactive web apps without needing a background in web development. In this guide, we’ll explore how to use Streamlit to build practical apps, and I’ll walk you through a real-world example in healthcare.
Why Streamlit?
Streamlit is a game-changer for anyone who wants to quickly create interactive applications. Here’s why it’s so great:
- Easy to Use: You can build apps with just a few lines of code.
- Interactive Widgets: Add elements like sliders and buttons effortlessly.
- Live Updates: See changes in real time as you code.
- Python Integration: Works seamlessly with libraries like Pandas and Matplotlib.
Getting Started with Streamlit
First things first, make sure you have Python installed. Then, install Streamlit using pip:
pip install streamlit
Let’s start with a simple example. Here’s how you can create a basic app that shows a line chart:
import streamlit as st
import pandas as pd
import numpy as np
st.title("My super simple line chart with Streamlit")
# Create some sample data
data = pd.DataFrame(
np.random.randn(50, 3),
columns=['a', 'b', 'c']
)
st.line_chart(data)
Save this code as app.py
and run it with:
streamlit run app.py
This will launch your app in a web browser. It’s a quick way to make something interactive.
Building a Healthcare Dashboard
Now, let’s dive into a more practical example: a healthcare dashboard. Imagine you have a dataset with patient information — like age, gender, blood pressure, cholesterol levels, and diagnosis. We’ll build an app to explore and visualize this data.
Step 1: Setting Up
First, import the necessary libraries and load your dataset:
import streamlit as st
import pandas as pd
# Load the data
@st.cache
def load_data():
data = pd.read_csv('cool_healthcare_data.csv')
return data
data = load_data()
Assume cool_healthcare_data.csv
has columns for age, gender, blood pressure, and so on.
Step 2: Adding Filters
Let’s make it interactive by adding some filters. For example, users might want to filter the data by gender and age range:
# Sidebar filters
st.sidebar.header("Filter Options")
gender = st.sidebar.selectbox("Select Gender", options=data["gender"].unique())
age_range = st.sidebar.slider("Select Age Range", min_value=0, max_value=100, value=(20, 50))
# Filter the data based on user input
filtered_data = data[(data["gender"] == gender) &
(data["age"].between(age_range[0], age_range[1]))]
Step 3: Visualizing Data
With the filtered data, you can now create visualizations. For instance, let’s show a histogram of blood pressure values:
st.subheader("Blood Pressure Distribution")
st.bar_chart(filtered_data["blood_pressure"].value_counts())
Maybe you want to get fancy and display how age relates to cholesterol levels:
st.subheader("Age vs. Cholesterol")
st.scatter_chart(filtered_data[["age", "cholesterol"]])
Step 4: Deploying Your App
Once your app is ready, you can run it locally or deploy it on platforms like Streamlit Cloud to share it with others.
Conclusion
So there you have it — Streamlit is like the Swiss Army knife of interactive apps for data scientists. It’s easy, fast, and doesn’t require you to be a web developer to make something cool. Whether you’re showcasing a complex dataset or just want to impress your colleagues with a snazzy visualization, Streamlit has got your back.
In the end, it’s like having a magic wand for your data: a few lines of code, and voilà — your insights are now in the hands of anyone who needs them. So go ahead, build something awesome, and remember: with Streamlit, even your data can have its own little moment in the spotlight.