Streamlit Core Features Beginner
Master the essential Streamlit commands for displaying text, building layouts, and optimizing performance with caching. These core features are the foundation of every Streamlit application.
Text & Display
Python
import streamlit as st # The magic command - renders almost anything st.write("Hello, world!") st.write(42) st.write({"key": "value"}) st.write(my_dataframe) # Specific text elements st.title("Page Title") st.header("Section Header") st.subheader("Subsection") st.markdown("**Bold** and *italic* text with [links](https://streamlit.io)") st.caption("Small caption text") st.code("print('Hello')", language="python") st.latex(r"e = mc^2") st.divider() # Horizontal rule
Status Elements
Python
# Info boxes st.info("This is informational.") st.success("Operation completed!") st.warning("Be careful with this.") st.error("Something went wrong.") st.exception(ValueError("Invalid input")) # Progress and spinners with st.spinner("Loading model..."): model = load_model() st.success("Model loaded!") progress = st.progress(0) for i in range(100): progress.progress(i + 1)
Layout
Columns
Python
# Equal columns col1, col2, col3 = st.columns(3) col1.metric("Temperature", "72 F", "1.2 F") col2.metric("Wind", "9 mph", "-8%") col3.metric("Humidity", "86%", "4%") # Unequal columns left, right = st.columns([2, 1]) with left: st.write("This is the wider column.") with right: st.write("Narrow column.")
Tabs
Python
tab1, tab2, tab3 = st.tabs(["Data", "Charts", "Settings"]) with tab1: st.dataframe(df) with tab2: st.line_chart(df["values"]) with tab3: st.write("Configuration options here.")
Sidebar
Python
# Add widgets to the sidebar with st.sidebar: st.title("Settings") model = st.selectbox("Model", ["GPT-4", "GPT-3.5", "Claude"]) temperature = st.slider("Temperature", 0.0, 2.0, 0.7) st.divider() st.caption("Configure the model parameters.")
Expander & Container
Python
# Collapsible section with st.expander("See details"): st.write("Hidden content revealed on click.") st.code("print('hello')") # Empty container for dynamic content placeholder = st.empty() placeholder.text("Loading...") # Later: placeholder.write("Done!")
Caching
Caching is critical for performance. Streamlit provides two caching decorators:
Python
# Cache data (DataFrames, strings, numbers) @st.cache_data def load_data(url): return pd.read_csv(url) # Cache resources (ML models, database connections) @st.cache_resource def load_model(): return pipeline("sentiment-analysis") # Cache with TTL (time-to-live) @st.cache_data(ttl=3600) # Expires after 1 hour def fetch_api_data(): return requests.get("https://api.example.com/data").json()
| Decorator | Use For | Returns |
|---|---|---|
@st.cache_data |
DataFrames, API responses, computations | New copy each call (safe to mutate) |
@st.cache_resource |
ML models, DB connections, shared objects | Same object (shared across users) |
Rule of Thumb: Use
@st.cache_data for anything serializable (data). Use @st.cache_resource for anything that should not be serialized (models, connections). Never use the deprecated @st.cache.
Core Features Mastered!
You know the essentials. Next, learn how to display data with DataFrames, charts, and maps.
Next: Data Display →
Lilly Tech Systems