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)