Store data across multiple sessions

Hello streamlit community,

my goal is to write an app that stores variables/data across multiple sessions. To visualize the task I created an app that firstly prompts a login screen when called, secondly verifies the credentials, and thirdly shows a button that increments and displays a number whenever the button ‘increment’ is pressed. This works just fine for one user using the session state.

Expectedly, when another user logs in the counter variable is initialized again as zero and not with the value of the last session. In other words, I want to achieve that the session_state is somehow preserved and transformed to another state.

Hosted example:
https://appkiller-urtvusthnsngwxxhvbyepr.streamlit.app/
Login: alice_foo, streamlit123

Code:

import streamlit as st

def check_password():
    """Returns `True` if the user had a correct password."""

    def password_entered():
        """Checks whether a password entered by the user is correct."""
        if (
            st.session_state["username"] in st.secrets["passwords"]
            and st.session_state["password"]
            == st.secrets["passwords"][st.session_state["username"]]
        ):
            st.session_state["password_correct"] = True
            del st.session_state["password"]  # don't store username + password
            # del st.session_state["username"]
        else:
            st.session_state["password_correct"] = False

    if "password_correct" not in st.session_state:
        # First run, show inputs for username + password.
        st.text_input("Username", on_change=password_entered, key="username")
        st.text_input(
            "Password", type="password", on_change=password_entered, key="password"
        )
        return False
    elif not st.session_state["password_correct"]:
        # Password not correct, show input + error.
        st.text_input("Username", on_change=password_entered, key="username")
        st.text_input(
            "Password", type="password", on_change=password_entered, key="password"
        )
        st.error("😕 User not known or password incorrect")
        return False
    else:
        # Password correct.
        return True

if check_password():

    st.title('Counter Example')
    if 'count' not in st.session_state:
        st.session_state.count = 0

    increment = st.button('Increment')
    if increment:
        st.session_state.count += 1

    st.write('Count = ', st.session_state.count)

Hey @DrTiWe,

Thanks for sharing this question!

Just to make sure I fully understand the use case – when another user logs in, are you hoping for that user to be able to view the incremented value of that count variable (which was incremented by another user)? In other words, you want the count variable to be accessible across different user sessions (not initialized to zero for new users)?

@Caroline: Exactly, the count variable should be accessible across different user sessions :slightly_smiling_face:

Thanks for clarifying!

In that case, session state isn’t the best way to implement this, because session state is specific to each user session. I would actually recommend using a database to store the count variable if the value needs to be accessible across different user sessions.

st.experimental_connection is an easy way to connect to a data source, and we also have a host of tutorials on connecting to data sources without st.experimental_connection if you prefer.

1 Like

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