Function decorator to store singleton objects.

Each singleton object is shared across all users connected to the app. Singleton objects must be thread-safe, because they can be accessed from multiple threads concurrently.

(If thread-safety is an issue, consider using st.session_state to store per-session singleton objects instead.)

Function signature

st.experimental_singleton(func=None, show_spinner=True, suppress_st_warning=False)


func (callable)

The function that creates the singleton. Streamlit hashes the function's source code.

show_spinner (boolean)

Enable the spinner. Default is True to show a spinner when there is a "cache miss" and the singleton is being created.

suppress_st_warning (boolean)

Suppress warnings about calling Streamlit functions from within the singleton function.

Was this page helpful?

editSuggest edits

Still have questions?

Our forums are full of helpful information and Streamlit experts.