Periodic server-driven data refresh shared across sessions (vs. per-session run_every)
Summary
Provide a way to refresh a data source on a server-side schedule — computed
once for the whole app and pushed to every session currently viewing it —
instead of each session running its own run_every timer and recomputing the
same query independently. This is the shared "the dashboard refreshes every
minute and everyone sees the latest" pattern that live/ops dashboards need.
Why?
@st.fragment(run_every=…) already gives time-driven updates, but the timer and
the computation are per session: with N viewers on the same dashboard you
get N timers and N executions of the same query every interval. A refresh
that is cheap for one user becomes N× load on the warehouse/API for a shared
internal tool.
@st.cache_data dedupes the computation across sessions, but nothing drives
a periodic refresh or wakes the sessions when a new value lands — today
run_every is the driver, and it's per session. The missing piece is a refresh
that runs once for the whole app and updates every session viewing it.
How?
Mark a cached data source to refresh on a fixed interval, centrally for the whole app. Any fragment that reads it shows the current value and re-renders when a new one lands.
Sketch (names illustrative):
# The value refreshes centrally every 60s; sessions read the current value and
# rerun when a new one lands — no per-session timer, one shared computation.
@st.cache_data(refresh_every="60s")
def latest_metrics():
return run_warehouse_query()
@st.fragment
def kpis():
df = latest_metrics() # returns the current cached value immediately
st.metric("Orders", len(df))
st.line_chart(df, x="ts", y="orders")
kpis()Additional Context
- Split out from #12980
("real-time chart updates without rerunning the entire script"), which is being
closed now that fragments (
run_every),st.rerun(scope=…), and element diffing cover the original per-session/live-update and flicker concerns. - Related in-flight work: the non-blocking background execution spec (#17067) covers shared, off-thread cached computation; scheduled/recurring refresh is called out there as not-yet-built.
- Companion gap: a freshness signal
(#17141) — a cheap "did
the source change?" probe (e.g. Snowflake change-tracking /
LAST_ALTERED/MAX(updated_at)) to drive event-driven invalidation instead of a blind timer. That's the push counterpart to this timer-based one.
Source: streamlit/streamlit