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_datafor data: DataFrames, query results, API responses. Each call gets a copy, which protects against accidental mutation.@st.cache_resourcefor 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=3600andmax_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_resourceobject changes it for every user; the docs' example fails with aKeyErrorafter a column is dropped in place. Usecache_datafor anything you transform. cache_datapickles 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_entriesandpersist.
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_datagives each call a copy andst.cache_resourceshares one object across sessions without copying, and that object must be thread-safe (the developer's job);ttlandmax_entries; an underscore prefix leaves an argument out of the cache key; mutating acache_resourceobject affects all sessions;cache_datauses pickle, so cache only trusted values; a fragment reruns only its function,run_everyrefreshes 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.rerunwith a fragment key in a callback;run_everyreruns continue even when the user is not interacting with the app;parallel=Truechanges 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