
Hello everyone, today I want to share a magical Python library – Streamlit.
Github Address: https://github.com/streamlit/streamlit
Streamlit is a revolutionary Python library that allows data scientists and developers to quickly create and share custom web applications. Unlike traditional web development frameworks, Streamlit focuses on developing data applications, simplifying the complex web development process into writing Python scripts. It adopts an innovative application architecture that supports real-time updates and interactive components, making it exceptionally easy to build data visualization dashboards, machine learning applications, and more. For teams that need to quickly showcase data analysis results to stakeholders, Streamlit is an ideal choice.
Installation
Installing Streamlit is very simple and can be done using the pip package manager:
# Install Streamlit
pip install streamlit
# Install commonly used data processing libraries
pip install pandas numpy matplotlib
# Verify installation
streamlit --version
# Run the sample application
streamlit hello
Features
-
Simple and Intuitive API: Uses an intuitive Python API, allowing you to create web applications without needing to understand HTML, CSS, or JavaScript. -
Real-time Reload Functionality: Automatically reruns the application after code modifications, greatly improving development efficiency. -
Rich Component Library: Built-in interactive components including charts, tables, input controls, etc., to meet various data display needs. -
Cache Mechanism: Provides an intelligent caching system to optimize data processing performance and enhance application response speed. -
Session State Management: Supports user session state management, making it easy to develop multi-user applications.
Basic Functions
1. Basic Page Layout
Streamlit provides a series of simple yet powerful layout functions to help developers build clear page structures. With these functions, you can easily create titles, paragraphs, sidebars, and other elements to achieve basic page layouts.
The following example demonstrates how to create a basic page containing multiple layout elements.
import streamlit as st
def create_basic_layout():
# Set page title
st.set_page_config(
page_title="Data Analysis Dashboard",
layout="wide"
)
# Add page title and description
st.title("Sales Data Analysis Dashboard")
st.markdown("### Real-time Monitoring of Sales Performance")
# Create sidebar
with st.sidebar:
st.header("Filter Criteria")
date_range = st.date_input("Select Date Range")
category = st.selectbox("Select Product Category", ["All", "Electronics", "Clothing", "Food"])
# Create multi-column layout
col1, col2 = st.columns(2)
with col1:
st.subheader("Sales Trend")
st.line_chart({"Data": [1, 5, 2, 6, 2, 1]})
with col2:
st.subheader("Category Distribution")
st.bar_chart({"Data": [3, 2, 4, 2]})
2. Data Display and Visualization
Data display and visualization is one of the core functions of Streamlit. With built-in chart components and support for mainstream visualization libraries, you can easily create various data visualization effects.
The following example demonstrates how to use Streamlit to display a data table and create interactive charts.
import pandas as pd
import numpy as np
import plotly.express as px
def create_data_visualization():
# Create sample data
df = pd.DataFrame(
np.random.randn(50, 3),
columns=["Sales", "Profit", "Cost"]
)
# Display data table
st.dataframe(
df,
width=600,
height=400
)
# Create interactive chart
fig = px.scatter(
df,
x='Sales',
y='Profit',
size='Cost',
title='Sales-Profit Relationship Analysis'
)
st.plotly_chart(fig)
# Add data filtering function
selected_columns = st.multiselect(
"Select Metrics to Display",
df.columns
)
if selected_columns:
st.line_chart(df[selected_columns])
3. User Input Handling
Streamlit provides a rich set of input components, making it easy to create interactive applications. These components can receive user input and update the page content in real-time.
The following example demonstrates how to use various input components to collect and process user data.
def handle_user_input():
# Text input
name = st.text_input("Please enter your name")
# Numeric input
age = st.number_input("Please enter your age", min_value=0, max_value=120)
# Selection box
category = st.selectbox(
"Select your interested category",
["Data Analysis", "Machine Learning", "Deep Learning"]
)
# Slider
experience = st.slider(
"Years of Experience",
min_value=0,
max_value=20
)
# Submit button
if st.button("Submit"):
# Process user input
st.success(f"Submission Successful!")
st.json({
"Name": name,
"Age": age,
"Interest": category,
"Experience": experience
})
Advanced Features
1. Using the Cache Mechanism
The caching mechanism of Streamlit can significantly improve application performance, especially when handling large amounts of data. By using caching decorators, you can avoid repeated calculations and enhance application response speed.
The following example demonstrates how to effectively use the caching mechanism to handle large-scale data.
import time
@st.cache_data
def load_large_dataset():
"""Example function to load a large dataset"""
# Simulate time-consuming operation
time.sleep(2)
return pd.DataFrame(
np.random.randn(10000, 4),
columns=['A', 'B', 'C', 'D']
)
def demonstrate_caching():
# Display loading progress
with st.spinner("Loading data..."):
df = load_large_dataset()
st.success("Data loaded successfully!")
# Data analysis operations
st.write("Data Overview:")
st.dataframe(df.head())
# Calculate statistical information
st.write("Basic Statistical Information:")
st.write(df.describe())
2. Session State Management
When developing multi-page applications or applications that need to maintain state, session state management is very important. Streamlit provides a simple API to manage user session states.
The following example demonstrates how to use session state to achieve user authentication and data persistence.
def manage_session_state():
# Initialize session state
if 'login_status' not in st.session_state:
st.session_state.login_status = False
if not st.session_state.login_status:
# Login form
with st.form("login_form"):
username = st.text_input("Username")
password = st.text_input("Password", type="password")
if st.form_submit_button("Login"):
if username == "admin" and password == "password":
st.session_state.login_status = True
st.rerun()
else:
st.error("Username or password is incorrect")
else:
# Display application content
st.success("Welcome back!")
if st.button("Logout"):
st.session_state.login_status = False
st.rerun()
Real-World Application Scenarios
1. Data Analysis Dashboard
In a corporate environment, a data analysis dashboard is a common requirement. With Streamlit, you can quickly build a fully functional data analysis dashboard to achieve real-time data display and interactive analysis.
The following example demonstrates how to create a sales data analysis dashboard.
def create_sales_dashboard():
# Page configuration
st.set_page_config(
page_title="Sales Analysis Dashboard",
layout="wide"
)
# Load data
@st.cache_data
def load_sales_data():
df = pd.DataFrame({
'Date': pd.date_range('2024-01-01', periods=100),
'Sales': np.random.randint(1000, 5000, 100),
'Region': np.random.choice(['East China', 'South China', 'North China', 'Central China'], 100),
'Product': np.random.choice(['Product A', 'Product B', 'Product C'], 100)
})
return df
df = load_sales_data()
# Create filters
with st.sidebar:
st.header("Filter Criteria")
selected_region = st.multiselect("Select Region", df['Region'].unique())
selected_product = st.multiselect("Select Product", df['Product'].unique())
# Apply filters
if selected_region:
df = df[df['Region'].isin(selected_region)]
if selected_product:
df = df[df['Product'].isin(selected_product)]
# Display key metrics
st.title("Sales Performance Analysis")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Total Sales", f"¥{df['Sales'].sum():,.0f}")
with col2:
st.metric("Average Daily Sales", f"¥{df['Sales'].mean():,.0f}")
with col3:
st.metric("Sales Days", f"{len(df)}")
# Create charts
st.subheader("Sales Trend")
st.line_chart(df.set_index('Date')['Sales'])
# Display detailed data
st.subheader("Sales Details")
st.dataframe(df)
2. Machine Learning Model Demonstration
Streamlit is very suitable for creating demonstration applications for machine learning models. With an intuitive interface, users can upload data, adjust parameters, and view prediction results.
The following example demonstrates how to create a simple machine learning model demonstration application.
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
def create_ml_demo():
st.title("Machine Learning Model Demonstration")
# Generate sample data
@st.cache_data
def generate_data():
X, y = make_classification(
n_samples=1000,
n_features=4,
n_informative=2,
n_redundant=0,
random_state=42
)
return X, y
X, y = generate_data()
# Model parameter settings
st.sidebar.header("Model Parameters")
n_estimators = st.sidebar.slider("Number of Trees", 10, 100, 50)
max_depth = st.sidebar.slider("Max Depth", 1, 10, 5)
# Train model
@st.cache_data
def train_model(_n_estimators, _max_depth):
model = RandomForestClassifier(
n_estimators=_n_estimators,
max_depth=_max_depth,
random_state=42
)
model.fit(X, y)
return model
model = train_model(n_estimators, max_depth)
# Show model performance
y_pred = model.predict(X)
accuracy = (y_pred == y).mean()
st.metric("Model Accuracy", f"{accuracy:.2%}")
# Feature importance visualization
st.subheader("Feature Importance")
importance_df = pd.DataFrame({
'Feature': [f'Feature {i+1}' for i in range(4)],
'Importance': model.feature_importances_
})
st.bar_chart(importance_df.set_index('Feature'))
Conclusion
As a Python library focused on data application development, Streamlit greatly simplifies the process of creating web applications. With its intuitive API and rich components, developers can quickly build interactive data visualization applications and machine learning demonstration platforms. Especially with its real-time reload functionality and caching mechanism, the development process becomes efficient and smooth.
For data scientists and analysts, Streamlit provides an ideal tool that allows them to easily convert data analysis results into interactive web applications. It is recommended to start learning from basic components and gradually explore advanced features to fully leverage the various capabilities offered by Streamlit to create professional data applications.
If you think this article is good, please like, share, and comment, as this will be the strongest motivation for me to continue producing more quality articles!
We have also prepared Python materials for everyone. Interested friends, come and find me to receive them and learn together!
Previous Recommendations
A complete PDF of the Python Web Scraping Learning Manual compiled over a month (free download)
Quick Start Guide to Beautiful Soup, from beginner to expert (PDF download)
100 common questions in basic Python learning (with answers).pdf
124 Python cases, complete source code!
30 practical Python web scraping projects (with source code)
From beginner to expert, 100 practical Python projects for practice (with answers)!
80 essential practical cases for Python data analysis.pdf (with code), completely open for download