optimizing-streamlit-performance

Reduce Streamlit reruns with caching, fragments, and forms.

Updated Jan 27, 2026
One-click install
npx skills add https://github.com/erickfmm/transformer-encoder-frankestein --skill optimizing-streamlit-performance-erickfmm
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-streamlit-performance
Source: https://github.com/erickfmm/transformer-encoder-frankestein/tree/main/.agents/skills/developing-with-streamlit/skills/optimizing-streamlit-performance
Command: npx skills add https://github.com/erickfmm/transformer-encoder-frankestein --skill optimizing-streamlit-performance-erickfmm

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Streamlit apps often rerun heavy computations and reload large data on every interaction, leading to slow user experiences.

Core Features & Use Cases

  • Caching strategies: use @st.cache_data for data and @st.cache_resource for connections or models to persist expensive objects across reruns.
  • Fragments and forms: isolate reruns with st.fragment and batch inputs with st.form to reduce unnecessary recomputation.
  • Pre-computation and lazy loading: move costly computations out of the main flow and load data only as needed for responsiveness.

Quick Start

Identify heavy UI components and apply caching using st.cache_data and st.cache_resource to minimize reruns.

Frequently Asked Questions about optimizing-streamlit-performance

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

FAQPage Schema
How do I speed up Streamlit apps that reload large data on every interaction?

To speed up Streamlit apps, apply caching decorators like @st.cache_data for datasets and @st.cache_resource for models. This persists expensive objects across reruns, preventing heavy computations from executing on every user interaction.

What's the best way to choose between st.cache_data and st.cache_resource for caching?

Choose st.cache_data for caching data structures like DataFrames, and st.cache_resource for global resources like database connections or models. Using the correct decorator ensures heavy objects persist efficiently across reruns without memory duplication.

How do Streamlit fragments and forms reduce unnecessary recomputation?

Streamlit fragments isolate reruns to specific UI components, while forms batch user inputs to delay execution. Combining st.fragment and st.form prevents the main script from rerunning entirely, minimizing unnecessary recomputation for interactive dashboards.

Can I optimize Streamlit widgets using pre-computation and lazy loading?

Yes, you can optimize Streamlit widgets by moving costly computations out of the main flow and loading data only as needed. Pre-computation and lazy loading improve responsiveness for interactive tools rendering heavy UI components.

Why does my Streamlit dashboard run slow despite caching heavy computations?

A Streamlit dashboard runs slow if caching is misapplied or heavy UI components trigger full reruns. Isolate widget reruns with fragments, batch inputs with forms, and ensure pre-computation moves heavy logic out of the main execution flow.

When should I not use st.cache_data for caching in Streamlit?

Avoid st.cache_data for mutable global resources like database connections or machine learning models. Use st.cache_resource instead to persist these objects, preventing the overhead of re-initializing expensive connections on every app rerun.