liveBook · Manning

Caching

As explained in Build Python Web Apps with Streamlit

Caching is the storage of computed results or loaded data so they can be reused without repeating the underlying work.

Caching stores the result of slow work so later calls with the same parameters can reuse it instead of repeating the work. In a Streamlit dashboard, this keeps data loading from slowing every interaction or page refresh. section 6.2.3 section 6.2

A practical caching flow looks like this:

  • Identify a slow function, such as one that reads a CSV file into a pandas DataFrame. section 6.2.3
  • Add the @st.cache_data decorator above the function; the decorator adds caching without rewriting its logic. section 6.2.3
  • Let Streamlit reuse the stored result on subsequent calls, app reruns, and page refreshes. section 6.2.3
  • Set a time-to-live or a user-friendly loading message when freshness and feedback matter. section 6.2.3

Caching dashboard data

Decorate a data-loading function so the CSV is read once and reused: @st.cache_data(ttl="1d", show_spinner="Reading sales data...") followed by load_data returning pd.read_csv(SALES_DATA_PATH). The first load can show progress; later loads should appear instantly. section 6.2.3

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

Inspect the minimal decorator and data-loading pattern for applying caching to a Streamlit function. section 6.2.3

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains cached function results, decorators, refresh behavior, TTL, and loading feedback, but does not detail cache internals.
Examples
Uses a Streamlit load_data function that reads a sales CSV into a pandas DataFrame and displays spinner feedback.
What it assumes
Assumes familiarity with Python functions, Streamlit elements, external files, and pandas-based data loading.
What you can do after
Apply caching via Streamlit's st.cache_data decorator to reduce repeated data-loading work.
How it connects
Connects caching with Streamlit, decorators, pandas, data loading, session state, and application performance.

Also explained in

Compare how 38 books explain caching →

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.