optimizing-streamlit-performance

Optimize Streamlit apps by reducing reruns with caching and fragments.

1|1|Updated Mar 20, 2026
One-click install
npx skills add https://github.com/Shamrock2245/shamrock-trading-bot --skill optimizing-streamlit-performance-shamrock2245
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-streamlit-performance
Source: https://github.com/Shamrock2245/shamrock-trading-bot/tree/main/.agent/skills/developing-with-streamlit/skills/optimizing-streamlit-performance
Command: npx skills add https://github.com/Shamrock2245/shamrock-trading-bot --skill optimizing-streamlit-performance-shamrock2245

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Streamlit apps often rerun entire scripts on every interaction and can become slow or memory-heavy when loading large datasets, models, or expensive computations. This Skill helps developers eliminate unnecessary recomputation, control cache growth, and isolate UI pieces so apps remain responsive under real-world usage.

Core Features & Use Cases

  • Caching best practices: Guidance for using st.cache_data for serializable data and st.cache_resource for connections and models, including TTLs, max_entries, and anti-patterns to avoid shared-mutable state.
  • Fragmentation and batching: Use @st.fragment, run_every, and st.form to isolate reruns, auto-refresh metrics safely, and batch user inputs to prevent full-app reruns.
  • Conditional rendering and large data handling: Techniques to avoid loading heavy content in hidden containers, sample or stream very large datasets, and prefer pre-computation or DB-backed queries for massive workloads.
  • Use Case: Convert a slow analytics dashboard that reloads on every widget change into a responsive app by caching the expensive data loads, isolating live metrics in fragments, and batching filter inputs in forms.

Quick Start

Analyze my Streamlit app and recommend specific places to apply st.cache_data, st.cache_resource, fragments, forms, conditional rendering, and cache TTL/max_entries settings to reduce reruns and memory growth.

Frequently Asked Questions about optimizing-streamlit-performance

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I stop my Streamlit app from rerunning and recomputing on every widget interaction?

To stop unnecessary Streamlit reruns, you can isolate UI pieces using @st.fragment and batch user inputs with st.form to prevent full-app script reruns on every widget change.

What is the best way to cache heavy data and models in Streamlit?

The best way to cache heavy data in Streamlit is using st.cache_data for serializable data and st.cache_resource for models and connections, applying TTL and max_entries settings to control memory growth.

When should I use st.cache_data versus st.cache_resource in Streamlit?

Use st.cache_data when loading serializable data like dataframes, and use st.cache_resource for global resources like database connections or ML models to avoid shared-mutable state anti-patterns in Streamlit.

How do I handle large datasets in Streamlit without making the app slow?

To handle large datasets in Streamlit without lag, avoid loading heavy content in hidden containers, sample or stream the data, and prefer pre-computation or DB-backed queries for massive workloads.

Can I auto-refresh live metrics in Streamlit without triggering a full app rerun?

You can auto-refresh live metrics in Streamlit without full app reruns by using the run_every parameter within an @st.fragment to safely isolate and update only the specific metrics UI.

What are the limitations of using caching and fragments for Streamlit performance?

Limitations include managing cache memory growth via TTL and max_entries, avoiding shared-mutable state in st.cache_resource, and ensuring thread-safe patterns to maintain app responsiveness under heavy use.