As Streamlit apps scale with more data, APIs, and visual elements, performance optimization becomes essential for a fluid user experience. In this guide, we’ll explore caching techniques, session state management, and rendering strategies that make your Streamlit apps load faster and feel smoother — without overcomplicating the code.
Caching Data And Computations
Streamlit provides built-in caching decorators like st.cache_data and st.cache_resource that store function results and avoid redundant computation.
import streamlit as st
import pandas as pd
import time
@st.cache_data
def load_data():
time.sleep(3) # simulate delay
return ("data.csv")
data = load_data()
st.dataframe(data)
Why it matters:
Reduces expensive operations like reading large files or hitting APIs.
Keeps your app responsive even under heavy load.
Automatically invalidates cache when inputs change.
Use st.cache_resource for objects that should persist across sessions (like ML models
or database connections).
Session State Management
User interactions often need persistence — think forms, filters, or toggles.
st.session_state allows you to store and update values dynamically.
if "count" not in st.session_state:
st.session_state.count = 0
increment = st.button("Increment")
if increment:
st.session_state.count += 1
st.write("Counter:", st.session_state.count)
Tip: Parallelize multiple predictions with concurrent.futures.
Efficient UI Rendering
Overloaded layouts or excessive re-renders can slow your Streamlit app. Here’s how to keep rendering efficient:
- Group widgets logically using
st.container()orst.columns(). - Avoid redundant
st.write()calls in loops. - Use placeholders with
st.empty()to update content dynamically.
Wrapping up
Optimizing Streamlit isn’t just about speed - it’s about consistency, responsiveness, and smooth UX.
By combining caching, session management, and efficient rendering, you can build apps that scale
elegantly and respond instantly.
At Hoopsiper, We Believe Performance Creates Happy Users.
Keep Your Streamlit Apps Fast, Use Smart Caching, And Let Users Focus On Insights
