optimizing-streamlit-performance

Optimize Streamlit app performance with caching, fragment isolation, and conditional rendering.

629|85|Updated May 4, 2020
One-click install
npx skills add https://github.com/andfanilo/streamlit-echarts --skill optimizing-streamlit-performance-andfanilo
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: optimizing-streamlit-performance
Source: https://github.com/andfanilo/streamlit-echarts/tree/main/.claude/skills/developing-with-streamlit/skills/optimizing-streamlit-performance
Command: npx skills add https://github.com/andfanilo/streamlit-echarts --skill optimizing-streamlit-performance-andfanilo

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Slow Streamlit apps that rerun too much, load heavy data on every interaction, or consume excessive memory are time-consuming for users and costly to operate; this Skill explains patterns to reduce unnecessary reruns and resource consumption.

Core Features & Use Cases

  • Caching patterns: Guidance on using @st.cache_data for computed data and @st.cache_resource for connections, models, and non-serializable objects while avoiding shared-mutation pitfalls.
  • Cache management: Recommendations for TTL, max_entries, and cleanup handlers to prevent unbounded growth and stale results.
  • Execution isolation: Use fragments to limit reruns to self-contained UI pieces and forms to batch inputs so typing or multiple controls don't trigger full reruns.
  • Conditional rendering & large-data strategies: Techniques to avoid rendering heavy content unless needed, sample large datasets for exploration, and prefer databases/materialized views for very large workloads.
  • Concurrency and threading guidance: Notes on safe multithreading patterns that avoid calling Streamlit APIs from background threads.
  • Use case: Convert a dashboard that reloads entire pages on each filter change into a responsive dashboard using cached data loaders, fragment-isolated metrics, and form-submitted filters.

Quick Start

Use the optimizing-streamlit-performance skill to add caching, fragment isolation, and form batching to a slow Streamlit dashboard that reloads on every interaction.

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 the entire script on every widget interaction?

To stop Streamlit from rerunning entirely on every interaction, use fragments to isolate UI pieces and forms to batch inputs so typing or adjusting controls only triggers localized updates instead of full app reruns.

What is the best way to cache heavy data loads in Streamlit to improve dashboard performance?

The best way to cache heavy data loads in Streamlit is applying the @st.cache_data decorator for computed datasets and @st.cache_resource for database connections or ML models, preventing expensive reloading during script reruns.

How do I manage cache size and stale results when using Streamlit caching decorators?

Manage Streamlit cache size and stale results by configuring TTL (time-to-live) to expire old entries, setting max_entries to cap memory growth, and using cleanup handlers for safe resource management when cached objects are evicted.

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

Use @st.cache_data for serializable computed data like DataFrames, and use @st.cache_resource for non-serializable objects such as database connections or ML models, avoiding shared-mutation pitfalls when handling global resources.

How do I handle very large datasets in Streamlit without slowing down the web app?

Handle very large datasets in Streamlit by applying conditional rendering to avoid loading heavy content unless needed, sampling data for exploration, and preferring databases or materialized views over loading entire datasets into memory.

Can I use multithreading in Streamlit to speed up background data processing without breaking the UI?

You can use multithreading in Streamlit for background processing by following safe concurrency patterns, but you must avoid calling Streamlit API commands directly from background threads to prevent application state conflicts.