Complete Streamlit Guide
Complete Streamlit Guide
Table of Contents
1. Streamlit and Its Importance
2. How to Install Streamlit
What is Streamlit?
Streamlit is an open-source Python library that makes it easy to create and share beautiful, custom
web applications for machine learning and data science. It transforms Python scripts into interactive
web apps with minimal code.
Key Features:
Rapid Development: Build apps in pure Python without HTML, CSS, or JavaScript
Interactive Widgets: Built-in widgets for user input and interaction
Importance of Streamlit:
Democratizes App Development: Enables data scientists to create web apps without web
development expertise
Installation Methods:
bash
bash
bash
Verify Installation:
bash
streamlit hello
System Requirements:
Python 3.8 or higher
Core Concepts:
Streamlit apps are Python scripts that run from top to bottom
Every time a user interacts with a widget, the entire script reruns
Use st. prefix for all Streamlit functions
Basic Structure:
python
import streamlit as st
# App title
[Link]("My First Streamlit App")
# Display text
[Link]("Hello, World!")
Key Principles:
1. Import streamlit: Always start with import streamlit as st
Step-by-Step Process:
python
# Save as: my_app.py
import streamlit as st
bash
cd C:\path\to\your\file
bash
Alternative Methods:
bash
Specifying port:
bash
python
python
[Link]("Section Header")
python
[Link]("Subsection Header")
python
python
[Link]("This is markdown text")
[Link]("**Bold text** and *italic text*")
[Link]("Here's a list:")
[Link]("- Item 1")
[Link]("- Item 2")
# Write variables
data = {"name": "John", "age": 30}
[Link](data)
# Write dataframes
import pandas as pd
df = [Link]({"A": [1, 2, 3], "B": [4, 5, 6]})
[Link](df)
Purpose: Most versatile function - displays text, markdown, dataframes, charts, etc.
python
Complete Example:
python
import streamlit as st
import pandas as pd
# Sample data
data = {
'Product': ['A', 'B', 'C', 'D'],
'Sales': [100, 150, 80, 200],
'Profit': [20, 30, 15, 40]
}
df = [Link](data)
[Link](df)
# Status messages
if [Link]("Process Data"):
[Link]("Data processed successfully!")
[Link]("Processing completed in 0.5 seconds")
6. Widgets
python
# Basic button
if [Link]("Click Me"):
[Link]("Button was clicked!")
python
# Basic checkbox
agree = [Link]("I agree to terms and conditions")
if agree:
[Link]("Thank you for agreeing!")
python
python
# Basic selectbox
option = [Link](
"Select a programming language:",
("Python", "Java", "C++", "JavaScript", "Go")
)
[Link](f"You selected: {option}")
python
# Number slider
age = [Link]("Select your age:", 0, 100, 25)
[Link](f"Your age: {age}")
# Range slider
values = [Link](
"Select a range of values:",
0.0, 100.0, (25.0, 75.0)
)
[Link](f"Range: {values[0]} to {values[1]}")
# Date slider
from datetime import datetime, date
start_date = [Link](
"Select date:",
value=datetime(2023, 1, 1),
min_value=datetime(2020, 1, 1),
max_value=datetime(2025, 12, 31),
format="MM/DD/YYYY"
)
python
# Integer input
number = st.number_input("Enter a number:", min_value=0, max_value=100, value=10, step=1)
# Float input
price = st.number_input("Enter price:", min_value=0.0, value=10.50, step=0.25, format="%.2f")
python
# Password input
password = st.text_input("Enter password:", type="password")
# Single date
birthday = st.date_input("Select your birthday:")
# Date range
from datetime import date
dates = st.date_input(
"Select date range:",
value=[date(2023, 1, 1), date(2023, 12, 31)],
min_value=date(2020, 1, 1),
max_value=date(2025, 12, 31)
)
python
import streamlit as st
from datetime import date
programming_languages = [Link](
"Programming Languages:",
["Python", "JavaScript", "Java", "C++", "Go", "Rust"]
)
submitted = st.form_submit_button("Submit")
if submitted:
[Link]("Form submitted successfully!")
[Link]({
"name": name,
"age": age,
"email": email,
"favorite_color": favorite_color,
"languages": programming_languages,
"experience": experience,
"subscribe": subscribe
})
7. Layouts
python
# Create columns
col1, col2, col3 = [Link](3)
with col1:
[Link]("Column 1")
[Link]("Content for column 1")
with col2:
[Link]("Column 2")
[Link]("Content for column 2")
with col3:
[Link]("Column 3")
[Link]("Content for column 3")
python
# Sidebar widgets
page = [Link]("Choose a page:", ["Home", "About", "Contact"])
[Link]("Adjust parameter:", 0, 100, 50)
# Main content
[Link]("Main Content Area")
[Link](f"Current page: {page}")
python
# Basic expander
with [Link]("Click to expand"):
[Link]("This content is hidden by default")
[Link]("[Link]
python
python
with tab1:
[Link]("Data Tab")
[Link]("Display your data here")
with tab2:
[Link]("Charts Tab")
[Link]("Display your charts here")
with tab3:
[Link]("Settings Tab")
[Link]("App settings here")
python
# Display image from URL
[Link]("[Link] caption="Sample Image")
7.7 Markdown
python
# Basic markdown
[Link]("# This is a header")
[Link]("**Bold text** and *italic text*")
[Link]("[Link to Google]([Link]
# Code blocks
[Link]("""
```python
def hello_world():
print("Hello, World!")
""")
### Complete Layout Example:
```python
import streamlit as st
import pandas as pd
import numpy as np
# Sidebar
[Link]("Dashboard Controls")
chart_type = [Link]("Chart Type:", ["Line", "Bar", "Area"])
data_points = [Link]("Data Points:", 10, 100, 50)
with tab1:
col1, col2 = [Link]([3, 1])
with col1:
if chart_type == "Line":
st.line_chart(data.set_index('x'))
elif chart_type == "Bar":
st.bar_chart(data.set_index('x'))
else:
st.area_chart(data.set_index('x'))
with col2:
[Link]("Data Points", data_points)
[Link]("Max Value", f"{data['y'].max():.2f}")
[Link]("Min Value", f"{data['y'].min():.2f}")
with tab2:
[Link](data, use_container_width=True)
with tab3:
[Link]("Application Settings")
theme = [Link]("Theme:", ["Light", "Dark", "Auto"])
auto_refresh = [Link]("Auto-refresh data")
refresh_interval = st.number_input("Refresh interval (seconds):", 1, 60, 5)
python
python
python
uploaded_files = st.file_uploader(
"Choose multiple files",
accept_multiple_files=True,
type=['csv', 'xlsx']
)
if uploaded_files:
for uploaded_file in uploaded_files:
[Link](f"Processing: {uploaded_file.name}")
# Process each file
if uploaded_file.[Link]('.csv'):
df = pd.read_csv(uploaded_file)
[Link]([Link]())
python
import streamlit as st
import pandas as pd
import json
from PIL import Image
import io
uploaded_file = st.file_uploader(
"Upload your file",
type=['csv', 'xlsx', 'json', 'txt', 'png', 'jpg', 'jpeg']
)
else:
# Text files
string_data = uploaded_file.read().decode("utf-8")
[Link]("File Content")
st.text_area("Content:", string_data, height=200)
python
import time
for i in range(100):
progress_bar.progress(i + 1)
status_text.text(f'Processing... {i+1}%')
[Link](0.01) # Simulate processing time
status_text.text('Processing complete!')
[Link]("File processed successfully!")
python
import streamlit as st
import requests
import json
python
try:
response = [Link](url, params=params)
if response.status_code == 200:
weather_data = [Link]()
[Link](f"Weather in {city}")
[Link](f"Temperature: {weather_data['main']['temp']}°C")
[Link](f"Description: {weather_data['weather'][0]['description']}")
else:
[Link]("City not found or API error")
except Exception as e:
[Link](f"Error: {str(e)}")
9.3 REST API with Different Methods
python
import streamlit as st
import requests
import json
# GET Request
[Link]("GET Request - Fetch Posts")
if [Link]("Fetch Posts"):
response = [Link](f"{base_url}/posts")
if response.status_code == 200:
posts = [Link]()[:5] # Show first 5 posts
for post in posts:
with [Link](f"Post {post['id']}: {post['title'][:50]}..."):
[Link](f"**User ID:** {post['userId']}")
[Link](f"**Title:** {post['title']}")
[Link](f"**Body:** {post['body']}")
# POST Request
[Link]("POST Request - Create Post")
with [Link]("create_post"):
user_id = st.number_input("User ID:", min_value=1, value=1)
title = st.text_input("Post Title:")
body = st.text_area("Post Body:")
if st.form_submit_button("Create Post"):
post_data = {
"userId": user_id,
"title": title,
"body": body
}
python
import streamlit as st
import requests
import time
python
import streamlit as st
import requests
import pandas as pd
import [Link] as px
if api_choice == "JSONPlaceholder":
resource = [Link]("Select resource:", ["posts", "users", "comments"])
if [Link](f"Fetch {[Link]()}"):
with [Link]("Loading..."):
response = [Link](f"[Link]
if response.status_code == 200:
data = [Link]()
df = [Link](data)
# Download option
csv = df.to_csv(index=False)
st.download_button(
label=f"Download {resource} CSV",
data=csv,
file_name=f"{resource}.csv",
mime="text/csv"
)
Use Caching
python
@st.cache_data
def load_data():
# Expensive data loading operation
return pd.read_csv("large_dataset.csv")
@st.cache_resource
def init_model():
# Load ML model (singleton resource)
return [Link]("[Link]")
python
# Good: Use session state for expensive operations
if 'processed_data' not in st.session_state:
st.session_state.processed_data = expensive_processing()
if submitted:
process_form_data(name, age)
Modular Structure
python
# [Link]
def load_data():
return pd.read_csv("[Link]")
def preprocess_data(df):
# Data preprocessing logic
return df
# main_app.py
import streamlit as st
from utils import load_data, preprocess_data
def main():
[Link]("My App")
data = load_data()
processed_data = preprocess_data(data)
[Link](processed_data)
if __name__ == "__main__":
main()
Configuration Management
python
# [Link]
class Config:
APP_TITLE = "My Streamlit App"
DEFAULT_THEME = "light"
API_BASE_URL = "[Link]
# [Link]
from config import Config
[Link](Config.APP_TITLE)
Loading States
python
Error Handling
python
try:
data = load_external_data()
[Link]("Data loaded successfully!")
except FileNotFoundError:
[Link]("Data file not found. Please upload a file.")
except Exception as e:
[Link](f"An unexpected error occurred: {str(e)}")
[Link]("Please try again or contact support.")
Input Validation
python
email = st.text_input("Enter your email:")
if email:
if "@" not in email:
[Link]("Please enter a valid email address")
else:
[Link]("Email format is valid")
Environment Variables
python
import os
import streamlit as st
Input Sanitization
python
import re
[Link](f"Counter: {st.session_state.counter}")
[Link]
txt
streamlit>=1.28.0
pandas>=1.5.0
plotly>=5.0.0
requests>=2.28.0
pillow>=9.0.0
Configuration Files
toml
# .streamlit/[Link]
[theme]
primaryColor = "#FF6B6B"
backgroundColor = "#FFFFFF"
secondaryBackgroundColor = "#F0F2F6"
textColor = "#262730"
[server]
port = 8501
enableCORS = false
enableXsrfProtection = true
Unit Testing
python
# test_app.py
import pytest
from app import process_data, validate_input
def test_process_data():
sample_data = [1, 2, 3, 4, 5]
result = process_data(sample_data)
assert len(result) == 5
assert all(isinstance(x, int) for x in result)
def test_validate_input():
assert validate_input("valid@[Link]") == True
assert validate_input("invalid-email") == False
python
import streamlit as st
import pandas as pd
import logging
import os
from typing import Optional
import time
# Configure logging
[Link](level=[Link])
logger = [Link](__name__)
# Configuration
class AppConfig:
TITLE = "📊 Professional Streamlit App"
MAX_FILE_SIZE = 200 * 1024 * 1024 # 200MB
ALLOWED_EXTENSIONS = ['csv', 'xlsx', 'json']
CACHE_TTL = 3600 # 1 hour
@st.cache_data(ttl=AppConfig.CACHE_TTL)
def load_data(file_path: str) -> Optional[[Link]]:
"""Load data with caching and error handling"""
try:
if file_path.endswith('.csv'):
return pd.read_csv(file_path)
elif file_path.endswith('.xlsx'):
return pd.read_excel(file_path)
else:
raise ValueError("Unsupported file format")
except Exception as e:
[Link](f"Error loading data: {str(e)}")
return None
def main():
# Page configuration
st.set_page_config(
page_title=[Link],
page_icon="📊",
layout="wide",
initial_sidebar_state="expanded"
)
# App header
[Link]([Link])
[Link]("---")
# Sidebar
with [Link]:
[Link]("🔧 Configuration")
if uploaded_file:
if uploaded_file.size > AppConfig.MAX_FILE_SIZE:
[Link]("File size exceeds maximum limit")
return
# Processing options
[Link]("Processing Options")
remove_nulls = [Link]("Remove null values", value=True)
normalize_data = [Link]("Normalize data")
if normalize_data:
numeric_columns = df.select_dtypes(include=['number']).columns
df[numeric_columns] = (df[numeric_columns] - df[numeric_columns].mean()) / df[numeric_co
st.session_state.processed_data = df
st.session_state.processing_complete = True
[Link]("Data processed successfully!")
else:
[Link]("Invalid data format or structure")
except Exception as e:
[Link](f"Processing failed: {str(e)}")
[Link](f"Data processing error: {str(e)}")
# Display metrics
col1, col2, col3, col4 = [Link](4)
with col1:
[Link]("Total Rows", len(df))
with col2:
[Link]("Total Columns", len([Link]))
with col3:
[Link]("Memory Usage", f"{df.memory_usage(deep=True).sum() / 1024**2:.2f} MB")
with col4:
[Link]("Null Values", [Link]().sum().sum())
# Data preview
[Link]("📋 Data Preview")
[Link]([Link](100), use_container_width=True)
with tab1:
[Link]("Statistical Summary")
[Link]([Link]())
# Data types
[Link]("Data Types")
dtype_df = [Link]({
'Column': [Link],
'Data Type': [Link],
'Non-Null Count': [Link](),
'Null Count': [Link]().sum()
})
[Link](dtype_df)
with tab2:
if len(df.select_dtypes(include=['number']).columns) > 0:
numeric_columns = df.select_dtypes(include=['number']).[Link]()
with col2:
[Link]("Box Plot")
[Link]("Box plot data:")
[Link](df[selected_column].describe())
else:
[Link]("No numeric columns available for visualization")
with tab3:
[Link]("Export Processed Data")
with col1:
csv = df.to_csv(index=False)
st.download_button(
label="📥 Download as CSV",
data=csv,
file_name="processed_data.csv",
mime="text/csv"
)
with col2:
# Convert to Excel
from io import BytesIO
buffer = BytesIO()
with [Link](buffer, engine='openpyxl') as writer:
df.to_excel(writer, sheet_name='Processed Data', index=False)
st.download_button(
label="📥 Download as Excel",
data=[Link](),
file_name="processed_data.xlsx",
mime="application/[Link]"
)
else:
# Welcome message
[Link]("👆 Please upload a data file using the sidebar to get started")
# Feature showcase
[Link]("🚀 Features")
feature_cols = [Link](3)
with feature_cols[0]:
[Link]("**📊 Data Processing**")
[Link]("- Remove null values")
[Link]("- Normalize data")
[Link]("- Statistical analysis")
with feature_cols[1]:
[Link]("**📈 Visualizations**")
[Link]("- Interactive charts")
[Link]("- Data distributions")
[Link]("- Summary statistics")
with feature_cols[2]:
[Link]("**⬇️ Export Options**")
[Link]("- CSV format")
[Link]("- Excel format")
[Link]("- Processed data")
if __name__ == "__main__":
main()
11. Summary
Key Takeaways
Streamlit is a powerful, user-friendly framework for creating data applications with minimal code.
Here are the essential points covered in this guide:
Core Concepts
Essential Components
Display Functions:
Interactive Widgets:
Layout Options:
Advanced Features
1. Performance
Use caching for expensive operations
Implement proper state management
2. Code Organization
Structure code modularly
Use configuration files
3. User Experience
Provide loading indicators
4. Security
Use environment variables for sensitive data
5. Deployment
Create proper [Link]
Configure app settings
Streamlit democratizes web application development for data scientists and Python developers,
enabling rapid creation of interactive, professional applications without extensive web development
knowledge. The framework's simplicity, combined with its powerful features, makes it an excellent
choice for data-driven applications, prototypes, and production deployments.
Next Steps
Explore Streamlit's extensive component library
Learn about custom components development
This guide provides a comprehensive foundation for Streamlit development. Continue practicing with
different datasets and use cases to master the framework and create impressive data applications.