liveBook · Manning

Decorator

As explained in Build Python Web Apps with Streamlit

A decorator is a Python construct that modifies or extends a function or class by wrapping it with additional behavior.

A decorator modifies or extends a function or class by wrapping it with additional behavior. In Streamlit, placing a decorator above a function adds capabilities without rewriting that function’s main logic. section 6.2.3

Streamlit uses decorators to attach practical application behavior:

  • Use @st.cache_data to store a slow function’s result and reuse it when the function receives the same parameters. section 6.2.3
  • Use @st.dialog to render a decorated function’s content inside a popup dialog. section 7.5.1

Caching a data-loading function

Place @st.cache_data above load_data. Streamlit then stores the returned pandas dataframe, so reruns or page refreshes can reuse the cached result instead of executing the loading logic again. A one-day TTL and a custom loading message can refine the behavior. section 6.2.3

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

Shows the minimal decorator syntax for turning load_data into a cached data-loading function. section 6.2.3

Listing 7.1 A drilldown dialog
import streamlit as st

@st.dialog("Drilldown", width="large")
def drilldown(main_df, compare_df):
  main_tab, compare_tab = st.tabs(["Main", "Compare"])
  with main_tab:
    st.dataframe(main_df, use_container_width=True)
  with compare_tab:
    st.dataframe(compare_df, use_container_width=True)

Shows a decorated drilldown function that renders main and comparison dataframes in dialog tabs. section 7.5.1

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines decorators through two working Streamlit examples, but does not explain decorator implementation internals.
Examples
Uses @st.cache_data on load_data and @st.dialog on a drilldown function displaying main and comparison dataframes.
What it assumes
Assumes Python functions, Streamlit imports, and basic pandas dataframe handling.
What you can do after
Apply Streamlit decorators to cache data-loading results and render interactive drilldown content in modal dialogs.
How it connects
Connects decorators with Streamlit, caching, pandas dataframes, popup dialogs, tabs, and sidebar interactions.

Also explained in

Compare how 7 books explain decorator →

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.