optimizing-streamlit-performance

Reduce unnecessary Streamlit reruns with caching, fragments, and forms.

Updated Apr 20, 2025
One-click install
npx skills add https://github.com/cathayrisk/Anya --skill optimizing-streamlit-performance-cathayrisk
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-streamlit-performance
Source: https://github.com/cathayrisk/Anya/tree/main/skills/developing-with-streamlit/skills/optimizing-streamlit-performance
Command: npx skills add https://github.com/cathayrisk/Anya --skill optimizing-streamlit-performance-cathayrisk

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Streamlit apps often rerun on every interaction, leading to slow dashboards. This skill teaches caching, fragmenting reruns, and batch interaction patterns to dramatically cut latency.

Core Features & Use Cases

  • Caching: use st.cache_data for data loading and st.cache_resource for connections or models to avoid recomputation.
  • Fragments and forms: isolate reruns with st.fragment and batch UI actions with st.form to improve responsiveness.
  • Conditional rendering and pre-computation: render heavy content lazily and pre-compute expensive operations for faster UX.

Quick Start

Apply the caching and fragment guidance to optimize a Streamlit dashboard.

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 a slow Streamlit dashboard that reruns on every interaction?

Streamlit apps rerun on every interaction, so speeding up a slow dashboard requires caching heavy computations with st.cache_data and st.cache_resource. You can also isolate reruns with st.fragment and batch UI actions with st.form to reduce latency.

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

Use st.cache_data when loading data like DataFrames to avoid recomputing data transformations, and use st.cache_resource for caching connections or loaded models. This distinction prevents unnecessary heavy computations during Streamlit app reruns.

What is the best way to isolate expensive UI components from Streamlit reruns?

The best way to isolate expensive UI components from Streamlit reruns is using st.fragment. This isolates specific reruns so only the targeted part of the app updates, preventing unnecessary recomputation across the entire dashboard.

How do I batch form interactions in Streamlit to improve responsiveness?

To batch form interactions in Streamlit and improve responsiveness, use st.form. This groups multiple UI inputs into a single submission, preventing the app from rerunning on every individual widget change and reducing overall latency.

Can I use conditional rendering to optimize data-heavy Streamlit apps?

Yes, you can use conditional rendering to optimize data-heavy Streamlit apps by rendering heavy content lazily. Pre-computing expensive operations alongside this strategy ensures faster UX by only loading content when specifically needed.