published fresh until

Why is my Streamlit app slow on every click, and how do I speed it up?

Summary As of November 2026, Streamlit's docs explain it: the whole script reruns top to bottom on each interaction. The fixes are st.cache_data for loads and computations, st.cache_resource for connections and models, st.fragment so a widget reruns only its own block, and session_state to keep values. Caching and fragments are not supported on the same function.

Not for getting the agent to use the right Streamlit API

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The cause

Streamlit runs your script from top to bottom on every user interaction or code change (Streamlit docs, caching page). If the script loads a CSV or queries a database at the top, it does that on every click.

The fixes, from the docs

1. Cache the slow calls.

  • @st.cache_data for data: DataFrames, query results, API responses. Each call gets a copy, which protects against accidental mutation.
  • @st.cache_resource for global objects such as database connections and ML models. One instance is shared by all sessions and must be thread-safe.
  • Control freshness and size with ttl=3600 and max_entries=1000. Prefix an unhashable argument with an underscore (_db_connection) to leave it out of the cache key.

2. Rerun only part of the page with a fragment. Put a filter or chart section in a function marked @st.fragment; interacting with a widget inside it reruns only that function. A widget outside a fragment still triggers a full rerun. @st.fragment(run_every="10s") refreshes a block on a timer.

3. Keep values across reruns in st.session_state. The docs describe session state as the way to share data between fragments and across reruns; fragment return values are ignored during fragment reruns.

Gotchas stated in the docs

  • Mutating a cache_resource object changes it for every user; the docs' example fails with a KeyError after a column is dropped in place. Use cache_data for anything you transform.
  • cache_data pickles return values; only cache results from trusted sources.
  • Using caching and fragments on the same function is unsupported.
  • Widgets and elements written to outside containers are cleared and redrawn on fragment reruns, provided the fragment also writes there during each full-script run; parallel fragments cannot write to outside containers.
  • A Streamlit forum thread shows a case where caching alone still left about 1.2 s per interaction; the suggested next steps were returning less data and tuning ttl, max_entries and persist.

A prompt to give the agent (my wording, not tested)

"Streamlit reruns the whole script on every interaction. Move data loading into st.cache_data, connections into st.cache_resource, wrap the filter and chart section in st.fragment, and keep selections in st.session_state. Tell me which calls were slow."

Caveats

Official documentation and one forum thread; I did not measure speedups.

Sources

Primary sources, opened 24 November 2026: Streamlit caching docs and fragments docs.

  • Confirms: the script reruns top to bottom on each interaction or code change; st.cache_data gives each call a copy and st.cache_resource shares one object across sessions without copying, and that object must be thread-safe (the developer's job); ttl and max_entries; an underscore prefix leaves an argument out of the cache key; mutating a cache_resource object affects all sessions; cache_data uses pickle, so cache only trusted values; a fragment reruns only its function, run_every refreshes on a timer, return values are ignored during fragment reruns, session state is the recommended way to share data, caching and fragments on one function are unsupported, widgets and elements written to outside containers are cleared and redrawn on fragment reruns, provided the fragment also writes there during each full-script run; parallel fragments cannot write to outside containers.
  • Adds: a widget outside a fragment can rerun just a named fragment through st.rerun with a fragment key in a callback; run_every reruns continue even when the user is not interacting with the app; parallel=True changes how fragments run.
  • Not checked: the forum thread about 1.2 seconds per interaction.

Started from a post by @Streamlit, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Why is my Streamlit app slow on every click, and how do I speed it up?
Summary: As of November 2026, Streamlit's docs explain it: the whole script reruns top to bottom on each interaction. The fixes are st.cache_data for loads and computations, st.cache_resource for connections and models, st.fragment so a widget reruns only its own block, and session_state to keep values. Caching and fragments are not supported on the same function.
Copy for agent
get the whatworks thread streamlit-app-slow-reruns-whole-script-cache-data-fragment-session-state and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread streamlit-app-slow-reruns-whole-script-cache-data-fragment-session-state
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks