How to store a variable from a session to a file/internally that can be used next time in another session?

I do realize that the session states can pass on the variables but those exist only till the session(browser) is open. Is there a way to store a variable like a counter value from the current session and then load the same value again in a new session(close and rerun the app).

Hi @Ishan_Mistry,

That’s a great use case for a database! Unfortunately, there’s not a built-in database in streamlit yet, but hopefully that will be built in in a future version. In the meantime, there are a number of great options for a third-party database, and we have tutorials about connecting your streamlit app to many of them Connect to data sources - Streamlit Docs.

Im not sure its exactly what you’re looking for, but I like to allow the user an option to save a large amount of settings as json, and then load it…
the problem is that it doesnt really work with upload widgets and its a bit problematic with my apps…
but anyways, here is my implementaion:

def get_session_state_dict():
    session_state_dict = {
        k: v
        for k, v in st.session_state.items()
        if "button" not in k and "file_uploader" not in k and "FormSubmitter" not in k
        label="Save Current Settings as a File",
        data=json.dumps(session_state_dict , default=str),

    upload_settings_widget = st.file_uploader(
        label="Upload Previously Saved Settings File",

    if upload_settings_widget:
        with upload_settings_widget.getvalue() as f:
            uploaded_settings = json.loads(f)
            failed = []
            succeeded = []
            button_apply_uploaded_settings = st.button(
                "Apply Settings",

def apply_uploaded_settings(json_settings):
    failed = []
    succeeded = []
    for k, v in json_settings.items():
            st.session_state[k] = v
        f"Successfully uploaded {str(len(succeeded))} out of {str(len(succeeded) + len(failed))} settings"
    if len(failed) > 0:
            f"Failed to upload the following settings: {failed}"

Sorry if its a mess, I am away from my laptop