Optimize performance

Caching

Function decorator to memoize function executions.

@st.cache(ttl=3600)
def run_long_computation(arg1, arg2):
  # Do stuff here
  return computation_output

Memo

Experimental 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

Singleton

Experimental 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

Clear memo

Clear all in-memory and on-disk memo caches.

@st.experimental_memo
def fetch_and_clean_data(url):
  # Fetch data from URL here, and then clean it up.
  return data

if st.checkbox("Clear All"):
  # Clear values from *all* memoized functions
  st.experimental_memo.clear()

Clear singleton

Clear all singleton caches.

@st.experimental_singleton
def get_database_session(url):
  # Create a database session object that points to the URL.
  return session

if st.button("Clear All"):
  # Clears all singleton caches:
  st.experimental_singleton.clear()

Was this page helpful?

editSuggest edits
forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.