legout/pi-config/optimizing-streamlit-performance
Claude Code and agent skill by legout/pi-config
- community
Checking that the SKILL.md file is still in the repository.
Optimizing Streamlit app performance. Use when apps are slow, rerunning too often, or loading heavy content. Covers caching, fragments, and static vs dynamic widget choices.
Install
npx vskill@latest install legout/pi-config/optimizing-streamlit-performanceSKILL.md
The same SKILL.md was published earlier as majiayu000/claude-skill-registry/optimizing-streamlit-performance.
Streamlit performance
Performance is the biggest win. Without caching and fragments, your app reruns everything on every interaction.
Caching
@st.cache_data for data
Use for any function that loads or computes data.
@st.cache_resource for connections
Use for connections, API clients, ML models—objects that can't be serialized.
Critical warning: Never mutate @st.cache_resource returns—changes affect all users:
Cleanup with `on_release`: Clean up resources when evicted from cache:
TTL for fresh data
Guidelines:
- Real-time dashboards →
ttl="1m"or less - Metrics/reports →
ttl="5m"tottl="15m" - Reference data →
ttl="1h"or more - Static data → No TTL
Prevent unbounded cache growth
Important: Caches without ttl or max_entries can grow indefinitely and cause memory issues. For any cached function that stores changing objects (user-specific data, parameterized queries), set limits:
Use ttl for time-based expiration OR max_entries for size-based limits. You usually don't need both.
Caching anti-patterns
Don't cache functions that read widgets:
Cache at the right granularity:
Fragments
Use @st.fragment to isolate reruns for self-contained UI pieces.
For auto-refreshing metrics, use run_every:
Use for: live metrics, refresh buttons, interactive charts that don't affect global state.
Forms to batch interactions
By default, every widget interaction triggers a full rerun. Use st.form to batch multiple inputs and only rerun on submit.
Use border=False for seamless inline forms that don't look like forms:
When to use forms:
- Multiple related inputs (signup, filters, settings)
- Text inputs where typing triggers expensive operations
- Any UI where "submit" semantics make sense
When NOT to use forms: If inputs depend on each other (e.g., selecting a country should update available cities), forms won't work since there's no rerun until submit.
Conditional rendering
This is critical and often missed.
Layout containers like st.tabs, st.expander, and st.popover always render all their content, even when hidden or collapsed.
To render content only when needed, use elements like st.segmented_control, st.toggle, or @st.dialog with conditional logic:
Pre-computation
Move expensive work outside the main flow:
- Compute aggregations in SQL/dbt, not Python
- Pre-compute metrics in scheduled jobs
- Use materialized views for complex queries
Large data handling
For datasets under ~100M rows
For very large datasets (over ~100M rows)
Note: This is only an escape hatch when serialization becomes too slow. In most cases, data this large shouldn't be loaded entirely into memory—prefer using a database that queries and loads data on demand.
@st.cache_data uses pickle which slows with huge data. Use @st.cache_resource instead:
Sampling for exploration
When exploring large datasets, load a random sample instead of the full data:
Multithreading
Custom threads cannot call Streamlit commands (no session context).
Prefer alternatives when possible:
@st.cache_datafor expensive computations@st.fragment(run_every="5s")for periodic updates
References
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckinginstalled-skills/developing-with-streamlit/skills/optimizing-streamlit-performance/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/legout/pi-config/optimizing-streamlit-performance)