How to know if the singleton function has been called before

I want to have a heavy object shared between all connected users. The singleton does this for me:

@st.experimental_singleton
def get_big_database(url):
   ...

but additionally I’d like to have this behaviour: if you’re the first user opening the app, i.e. no one has given the database url and therefore get_big_database() hasn’t been called before, then display a bunch of widgets like text input for entering database URL and a button that is connected to get_big_database(). Otherwise I want to display something like “Database already loaded: {URL}”.

Is there an easy way to do this? i.e. I’m looking for something like a global_state instead of session_state

Hi @milad, we actually have something like global_state on the roadmap! The next frontier for Streamlit For now, the best alternative, if you’re running your app on Streamlit Cloud you could use a lightweight third-party database for keeping track of something like this – here are some options to consider Connect to data sources - Streamlit Docs

If you’re working locally, or on a permanent machine that you control, you could do something as simple as saving a little file to your disk that keeps track of whether the database has been loaded.

Hope that’s helpful

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.