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






