Python Guide
Streamlit Python Tutorial — Build Data Apps in Minutes
Transform Python scripts into interactive web apps. No HTML, no CSS, no JavaScript — just Python.
Quick answer: Install with pip install streamlit, create an app.py file with import streamlit as st, add widgets like st.slider() and st.button(), then run streamlit run app.py. Streamlit handles the entire web UI automatically.
Install Streamlit
pip install streamlit
Verify the installation:
streamlit hello
This opens a demo app in your browser at localhost:8501.
💡 Tip: Use a virtual environment to keep dependencies clean: python -m venv venv then activate it before installing.
Create Your First App
Create a file called app.py:
import streamlit as st
st.title("My First Streamlit App")
st.write("Hello, world! This is my first data app.")
name = st.text_input("What's your name?")
if name:
st.write(f"Nice to meet you, {name}!")
Run it:
streamlit run app.py
Your browser opens automatically. Every time you save the file, Streamlit detects changes and asks if you want to rerun.
Add Interactive Widgets
Streamlit widgets let users interact with your app:
import streamlit as st
import pandas as pd
import numpy as np
st.title("Interactive Data Explorer")
# Slider
value = st.slider("Pick a number", 0, 100, 50)
st.write(f"You picked: {value}")
# Selectbox
option = st.selectbox("Choose a dataset", ["Sales", "Users", "Revenue"])
# Button
if st.button("Generate data"):
df = pd.DataFrame(
np.random.randn(10, 3),
columns=["A", "B", "C"]
)
st.dataframe(df)
Key widgets:
st.slider()— numeric range selectorst.selectbox()— dropdown menust.button()— clickable actionst.checkbox()— boolean togglest.file_uploader()— file upload
Display Data and Charts
Streamlit has built-in support for Pandas DataFrames and charts:
import streamlit as st
import pandas as pd
import numpy as np
df = pd.DataFrame(
np.random.randn(20, 3),
columns=["Sales", "Users", "Revenue"]
)
# DataFrame
st.subheader("Raw Data")
st.dataframe(df)
# Line chart
st.subheader("Trends")
st.line_chart(df)
# Bar chart
st.subheader("Comparison")
st.bar_chart(df)
# Metrics
col1, col2, col3 = st.columns(3)
col1.metric("Sales", "$12k", "+5%")
col2.metric("Users", "1,200", "+12%")
col3.metric("Revenue", "$8k", "-3%")
💡 Pro tip: Use st.columns() to create side-by-side layouts. It's the easiest way to build dashboards.
Add Caching for Performance
Streamlit reruns your entire script on every interaction. Use caching for expensive operations:
import streamlit as st
import pandas as pd
import time
@st.cache_data
def load_data():
time.sleep(3) # Simulate slow API call
return pd.DataFrame({
"Product": ["A", "B", "C"],
"Sales": [100, 200, 150]
})
df = load_data()
st.dataframe(df)
Use @st.cache_data for DataFrames and @st.cache_resource for ML models and database connections.
Deploy for Free
The fastest way to deploy is Streamlit Community Cloud:
- Push your code to GitHub
- Go to
share.streamlit.io - Connect your GitHub repo
- Select
app.pyas the main file - Click Deploy
Your app gets a public URL instantly. Free tier includes unlimited public apps.
Requirements file: Add a requirements.txt with streamlit and any other dependencies (pandas, numpy, etc.) so the cloud platform installs them automatically.
🎯 Streamlit vs Gradio — Quick Comparison
| Feature | Streamlit | Gradio |
|---|---|---|
| Best for | Data dashboards, multi-page apps | ML model demos, Hugging Face Spaces |
| Learning curve | Very easy | Extremely easy |
| Layout control | Columns, tabs, sidebar | Limited |
| Caching | Built-in (@st.cache_data) |
Manual |
👉 Full breakdown: Streamlit vs Gradio
🛡️ Best Practices
- Always use
@st.cache_datafor data loading and@st.cache_resourcefor models - Break large apps into multiple pages using the
pages/directory - Use
st.session_stateto persist variables across reruns - Keep the main script focused — put reusable functions in separate modules
- Add
st.set_page_config()at the top for title, icon, and layout - Use
st.secretsfor API keys and sensitive config
Important: Streamlit apps are stateless by default. Every interaction reruns the entire script. Use st.session_state to store user-specific data.
❓ Frequently Asked Questions
What is Streamlit used for?
Streamlit is a Python framework for building interactive data apps, dashboards, and machine learning demos. It transforms Python scripts into shareable web apps with minimal code.
Is Streamlit free?
Yes. Streamlit is open-source and free. Streamlit Community Cloud offers free hosting for public apps.
Do I need to know HTML or JavaScript?
No. Streamlit handles all the web rendering. You write pure Python, and Streamlit generates the UI automatically.
Streamlit vs Gradio — which should I use?
Gradio is faster for ML model demos and Hugging Face Spaces. Streamlit is better for full data dashboards and multi-page apps.
Can I deploy Streamlit for free?
Yes. Streamlit Community Cloud lets you deploy unlimited public apps for free. You can also deploy on Heroku, AWS, or any server.