optimizing-streamlit-performance

Reduce Streamlit app reruns with caching, fragments, and conditional rendering.

Updated Aug 27, 2026
One-click install
npx skills add https://github.com/fangshine01/AI_agent --skill optimizing-streamlit-performance-fangshine01
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-streamlit-performance
Source: https://github.com/fangshine01/AI_agent/tree/main/.github/skills/optimizing-streamlit-performance
Command: npx skills add https://github.com/fangshine01/AI_agent --skill optimizing-streamlit-performance-fangshine01

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Streamlit apps often become slow or waste resources because every interaction can trigger a full-app rerun and heavy computations reload on each user action; this Skill explains how to avoid unnecessary reruns, unbounded memory growth, and shared-resource mutation to keep apps responsive and stable.

Core Features & Use Cases

  • Caching patterns: Guidance on when and how to use @st.cache_data for computed data and @st.cache_resource for connections and large non-serializable objects, including TTL, max_entries, and on_release cleanup.
  • Fragment isolation & batching: Use @st.fragment and st.form to confine reruns to small UI pieces or batch inputs so typing and unrelated interactions do not retrigger expensive work.
  • Conditional rendering & large-data strategies: Techniques for rendering heavy content only when needed (segmented controls, toggles), pre-computing work in DB/jobs, sampling for exploration, and when to use resource caching for extremely large datasets.
  • Multithreading and safety: Patterns for background fetching without Streamlit calls in threads and warnings against mutating cached shared resources to prevent cross-user side effects.
  • Use Case: Convert a slow live dashboard that recomputes charts on every click into a responsive UI by caching raw data, isolating auto-refreshing metrics in fragments, and conditionally loading heavy charts only when requested.

Quick Start

Use this skill to identify and apply caching, fragments, conditional rendering, and form batching to reduce reruns and memory usage in your Streamlit app.

Frequently Asked Questions about optimizing-streamlit-performance

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

FAQPage Schema
Why does my Streamlit app run so slowly on every user interaction?

Streamlit apps run slowly because every widget interaction triggers a full-app rerun, reloading heavy computations. You can fix this by applying caching, fragment isolation, and conditional rendering to prevent unnecessary recomputations.

How do I stop Streamlit from recomputing expensive visualizations on every click?

To stop recomputing expensive visualizations, use @st.cache_data for computed data and @st.cache_resource for large objects. You can also isolate auto-refreshing metrics in @st.fragment to confine reruns to small UI pieces.

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

Use st.cache_data for serializable computed data like DataFrames, and st.cache_resource for non-serializable objects like database connections. Both support TTL and max_entries limits to manage cache lifecycle and memory.

Can I isolate a specific widget's rerun logic without refreshing the whole Streamlit dashboard?

Yes, you can isolate specific widget reruns by decorating functions with @st.fragment or wrapping inputs in st.form. This confines reruns to small UI pieces so typing or unrelated interactions do not retrigger expensive work.

What is the best way to handle large datasets in Streamlit without causing memory issues?

To handle large datasets safely, use resource caching with max_entries limits, pre-compute heavy work in database jobs, and apply data sampling for exploration. This prevents unbounded memory growth during interactive dashboard sessions.

Are there risks with multithreading and shared cached resources in Streamlit?

Yes, mutating shared cached resources risks cross-user side effects. When using multithreading for background fetching, ensure threads do not make Streamlit calls or mutate cached objects to keep your app stable and safe.