liveBook · Manning

St.cache_data

As explained in Build Python Web Apps with Streamlit

st.cache_data is a Streamlit decorator or caching mechanism that stores function results and reuses them across reruns when inputs remain unchanged.

st.cache_data stores the result of a slow function call. When the function is called again with the same parameters, Streamlit reuses the stored result instead of running the function again. This keeps data-heavy dashboards responsive across reruns and page refreshes. section 6.2.3

Use it as a decorator by placing @st.cache_data above a function such as load_data. Streamlit then caches the returned pandas dataframe. The first run may show a spinner; later calls can return the cached dataframe. section 6.2.3

@st.cache_data
def load_data():
  return pd.read_csv(SALES_DATA_PATH)

A minimal pattern for caching data loaded from a CSV file. section 6.2.3

Adjust the cache for practical dashboard behavior:

  • Set ttl="1d" to make the cached result valid for one day. section 6.2.3
  • Set show_spinner to a message such as "Reading sales data..." while the function runs. section 6.2.3

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines decorator-based caching and demonstrates ttl and spinner options, but does not detail cache invalidation internals.
Examples
Uses a cached load_data function that reads sales data with pandas read_csv and displays loading feedback.
What it assumes
Assumes familiarity with Python functions and imports, while introducing decorators through a concrete data-loading example.
What you can do after
Apply it via the @st.cache_data decorator to cache slow data-loading functions and configure expiration or feedback.
How it connects
Connects Streamlit caching with session state, pandas dataframes, CSV loading, and Python decorators.

Also explained in

Compare how 2 books explain st.cache_data →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.