Streamlit для аналитика
Карьерник — квиз-тренажёр в Telegram с 1500+ вопросами для собесов аналитика. SQL, Python, A/B, метрики. Бесплатно.
Зачем это знать
Streamlit позволяет аналитику build interactive web apps без фронтенд-знаний. 30 минут — от Python-скрипта к working dashboard. В startups и data-teams — популярный tool для quick prototypes, internal tools, demo моделей.
Что такое Streamlit
Python library для web apps:
- Declarative API
- No HTML/CSS/JS нужно
- Real-time reactive
- Deploy easy
pip install streamlit
streamlit run app.pyHello world
# app.py
import streamlit as st
import pandas as pd
st.title("Мой первый dashboard")
st.write("Здесь будет аналитика")
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
st.dataframe(df)
st.line_chart(df)streamlit run app.py → opens browser с app.
Widgets
Interactive inputs:
# Slider
age = st.slider("Your age", 0, 100, 25)
# Selectbox
color = st.selectbox("Pick color", ["red", "green", "blue"])
# Date input
date = st.date_input("Select date")
# File upload
file = st.file_uploader("Upload CSV")
# Multi-select
countries = st.multiselect("Countries", ["RU", "US", "KZ"])
# Button
if st.button("Run analysis"):
st.write("Analyzing...")Layout
Columns
col1, col2 = st.columns(2)
with col1:
st.metric("Revenue", "$1M", "+10%")
with col2:
st.metric("Users", "5k", "+5%")Sidebar
with st.sidebar:
filter_country = st.selectbox("Country", options)Filters в sidebar — classic.
Tabs
tab1, tab2 = st.tabs(["Metrics", "Details"])
with tab1:
st.write("Metrics here")
with tab2:
st.write("Details here")Caching
Для slow queries — @st.cache_data:
@st.cache_data
def load_data():
return pd.read_csv("huge_file.csv")
df = load_data() # первый запуск — загружает
# Subsequent runs — cachedCritical для performance.
Typical dashboard
import streamlit as st
import pandas as pd
import plotly.express as px
st.set_page_config(page_title="Sales Dashboard", layout="wide")
st.title("📊 Sales Dashboard")
# Sidebar filters
country = st.sidebar.selectbox("Country", ["All", "RU", "US", "KZ"])
# Load data (cached)
@st.cache_data
def load_data():
return pd.read_csv("sales.csv")
df = load_data()
if country != "All":
df = df[df['country'] == country]
# Metrics row
col1, col2, col3 = st.columns(3)
col1.metric("Revenue", f"${df['revenue'].sum():,.0f}")
col2.metric("Orders", f"{df['orders'].sum():,}")
col3.metric("AOV", f"${df['revenue'].sum() / df['orders'].sum():.0f}")
# Chart
fig = px.line(df.groupby('date')['revenue'].sum().reset_index(),
x='date', y='revenue')
st.plotly_chart(fig, use_container_width=True)
# Table
st.dataframe(df.head(100))30 строк = работающий dashboard.
ML model interface
Для demo models:
import streamlit as st
import pickle
model = pickle.load(open("model.pkl", "rb"))
st.title("Churn prediction")
age = st.slider("Age", 18, 100, 30)
tenure = st.number_input("Tenure (months)", 0, 100, 12)
payments = st.number_input("Payments count", 0, 50, 5)
if st.button("Predict"):
features = [[age, tenure, payments]]
prob = model.predict_proba(features)[0][1]
st.write(f"Churn probability: {prob:.2%}")Stakeholders играются, understand model.
State management
# Session state
if 'count' not in st.session_state:
st.session_state.count = 0
if st.button("Increment"):
st.session_state.count += 1
st.write(f"Count: {st.session_state.count}")Persist data across reruns.
Deploy
Streamlit Cloud (free)
Connect к GitHub repo → auto-deploy. 1 public app free.
Internal
Docker + cloud:
FROM python:3.10
RUN pip install streamlit pandas
COPY app.py .
CMD ["streamlit", "run", "app.py"]Use cases
1. Internal tools
Ad-hoc queries с filters. Non-technical users.
2. ML demo
Show model to stakeholders.
3. Prototype
Before full BI investment.
4. Portfolio
Deploy your project — shareable.
5. Experiments
A/B analysis with custom logic.
Streamlit vs альтернативы
Streamlit
- Simplest Python
- Quick prototyping
- Single file
Dash (Plotly)
- More flexible
- Callback-based
- Heavier
Gradio
- ML model-focused
- Simpler than Streamlit для ML
- Less general-purpose
Panel
- Holoviews ecosystem
- Flexible
- Steeper curve
Streamlit — best starting point.
Частые ошибки
Heavy load без caching
Query runs на каждый widget change → slow.
Always @st.cache_data для heavy queries.
Too many widgets
10+ filters → confusion. Prioritize.
Production claims
Streamlit — prototype. Для production SaaS — proper frontend.
No error handling
User uploads wrong file format → app crashes.
try:
df = pd.read_csv(file)
except:
st.error("Invalid file format")Limitations
- Single user: не multi-tenant
- No auth built-in: use wrapper (Streamlit Cloud has)
- Performance: limited на huge datasets
- Fancy UI: not as polished as dedicated frontend
Но для internal analytics — хватает.
На собесе
«Как поделились бы anal?»
Options:
- Jupyter notebook
- Streamlit app
- BI tool (Tableau, Metabase)
- Email report
Streamlit — когда нужен interactivity without BI invest.
Связанные темы
FAQ
Free?
Open-source + Streamlit Cloud free tier.
Production?
Internal tools — ok. Customer-facing — consider proper frontend.
Mobile?
Works, но не mobile-optimized.
Тренируйте навыки — откройте тренажёр с 1500+ вопросами для собесов.