Streamlit
📚

Streamlit

Created
Sep 1, 2025 10:05 PM
Tags

Streamlit Study Notes

Streamlit is a Python framework designed for creating interactive web applications for data analysis, visualization, and machine learning.

Step-by-Step Guide

1. Installation and Setup

  • Recommended: Use a virtual environment to keep your project dependencies isolated.

Creating and activating a virtual environment (Python ≥ 3.8 recommended):


python -m venv streamlit_env

source streamlit_env/bin/activate  # macOS/Linux
  • Install Streamlit:
pip install streamlit

Check your installation by typing:

streamlit hello

This opens a demo app in your browser.

2. Creating Your First Streamlit App

Create a new Python file, e.g., app.py:

# Simple Streamlit App

import streamlit as st

st.title("My First Streamlit App")
st.write("Hello, Streamlit! 🎉")
  • Run the app:
streamlit run app.py

3. Building an Interactive App: A Professional Example

Here is a more advanced app demonstrating common functionalities educators or analysts might use:

Example: Interactive Data Exploration App (app.py):


import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt

# Load dataset
@st.cache_data
def load_data():
    df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
    return df

df = load_data()

st.title("Interactive Iris Dataset Explorer")

st.header("Dataset Preview")
st.write(df.head())

# Sidebar widgets
st.sidebar.header("Filter Data")

species = st.sidebar.multiselect(
    "Select Species",
    options=df["species"].unique(),
    default=df["species"].unique()
)

filtered_df = df[df["species"].isin(species)]

st.header("Filtered Data")
st.write(filtered_df)

# Visualization
st.header("Data Visualization")

fig, ax = plt.subplots()
ax.scatter(filtered_df["sepal_length"], filtered_df["sepal_width"], c='blue', alpha=0.5)
ax.set_xlabel("Sepal Length")
ax.set_ylabel("Sepal Width")
ax.set_title("Sepal Length vs. Sepal Width")

st.pyplot(fig)

  • Run the app again:
streamlit run app.py

This provides an interactive dashboard for exploring Iris dataset features, filtering, and visualization.

4. Streamlit Cloud Deployment

Streamlit apps can be easily deployed using Streamlit Cloud:

Step-by-step deployment process:

  • Prerequisites:
    • Create a GitHub account.
    • Push your project code (app.py) to GitHub.
  • Directory structure for your project:
CopyEdit
streamlit-iris-app/
├── app.py
├── requirements.txt

  • Create requirements.txt:

pip freeze > requirements.txt

This captures dependencies for deployment.

5. Deploying to Streamlit Cloud

  1. Visit https://streamlit.io/cloud, sign in, and select "New app".
  2. Connect your GitHub account and select your repo.
  3. Choose the branch (usually main) and specify your Python file (default: app.py).
  4. Click "Deploy".

Your app will be live in minutes!

Key Streamlit Concepts

  • Widgets: Create interactive UI elements like sliders, dropdowns, buttons, checkboxes, and multiselect.
  • Layouts: Use st.sidebarst.columns, and st.tabs to organize UI elements.
  • Caching: Optimize app performance with @st.cache_data decorator to avoid repeated computations or data reloads.

Streamlit Widgets Quick Reference

Widget
Example Usage
Description
Slider
st.slider()
Numeric range slider
Dropdown
st.selectbox()
Dropdown list
Checkbox
st.checkbox()
Checkbox
Multiselect
st.multiselect()
Multiple-choice dropdown
Text input
st.text_input()
Single-line text
File uploader
st.file_uploader()
File upload control

Common Deployment Issues

  • Dependency management: Always use requirements.txt for dependencies.
  • Large files or datasets: Use external storage solutions (e.g., GitHub raw URL, AWS S3).