DocumentationsearchSearchrocket_launchGet startedInstallationaddFundamentalsaddFirst stepsaddcodeDevelopConceptsaddAPI referenceremovePAGE ELEMENTSWrite and magicaddText elementsaddData elementsaddChart elementsaddInput widgetsaddMedia elementsaddLayouts and containersaddChat elementsaddStatus elementsaddThird-party componentsopen_in_newAPPLICATION LOGICNavigation and pagesaddExecution flowaddCaching and stateremovest.cache_datast.cache_resourcest.cachedeletest.session_statest.query_paramsst.experimental_get_query_paramsdeletest.experimental_set_query_paramsdeleteConnections and secretsaddCustom componentsaddUtilitiesaddConfigurationaddTOOLSApp testingaddCommand lineaddTutorialsaddQuick referenceaddweb_assetDeployConceptsaddStreamlit Community CloudaddStreamlit in Snowflakeopen_in_newOther platformsaddschoolKnowledge baseFAQInstalling dependenciesDeployment issuesHome/Develop/API reference/Caching and stateCaching and state
Optimize performance and add statefulness to your app!
Caching
Streamlit provides powerful cache primitives for data and global resources. They allow your app to stay performant even when loading data from the web, manipulating large datasets, or performing expensive computations.
Cache dataFunction decorator to cache functions that return data (e.g. dataframe transforms, database queries, ML inference).@st.cache_data
def long_function(param1, param2):
  # Perform expensive computation here or
  # fetch data from the web here
  return data
Cache resourceFunction decorator to cache functions that return global resources (e.g. database connections, ML models).@st.cache_resource
def init_model():
  # Return a global resource here
  return pipeline(
    "sentiment-analysis",
    model="distilbert-base-uncased-finetuned-sst-2-english"
  )

Manage state
Streamlit re-executes your script with each user interaction. Widgets have built-in statefulness between reruns, but Session State lets you do more!
Session StateSave data between reruns and across pages.st.session_state["foo"] = "bar"
Query parametersGet, set, or clear the query parameters that are shown in the browser's URL bar.st.query_params[key] = value
st.query_params.clear()

Deprecated commands
delete
This command was deprecated in version 1.18.0. Use st.cache_data or st.cache_resource instead.
CachingFunction decorator to memoize function executions.@st.cache(ttl=3600)
def run_long_computation(arg1, arg2):
  # Do stuff here
  return computation_output
delete
This command was deprecated in version 1.18.0. Use st.cache_data instead.
MemoExperimental function decorator to memoize function executions.@st.experimental_memo
def fetch_and_clean_data(url):
  # Fetch data from URL here, and then clean it up.
  return data
delete
This command was deprecated in version 1.18.0. Use st.cache_resource instead.
SingletonExperimental function decorator to store singleton objects.@st.experimental_singleton
def get_database_session(url):
  # Create a database session object that points to the URL.
  return session
deleteGet query parametersGet query parameters that are shown in the browser's URL bar.param_dict = st.experimental_get_query_params()
deleteSet query parametersSet query parameters that are shown in the browser's URL bar.st.experimental_set_query_params(
  {"show_all"=True, "selected"=["asia", "america"]}
)
Previous: Execution flowNext: st.cache_dataforumStill have questions?Our forums are full of helpful information and Streamlit experts.HomeContact UsCommunity© 2024 Snowflake Inc.Cookie policy