Streamlit session state deleted when switch page

Hello! I am a new one and very need help.
The structure of my app: start.py - main page with authorisation.
You need to login to see other pages.
Two other pages are in th folder pages.
Locally it works normal, but when i put it on server with docker file and others files for deployment i have problems with autentification or may be with st.session_state. On first page i log in and everything is ok. On other pages if i print st.session_state i see logged information. But after several seconds st.session_state is empty - {}. And i get mistake KeyError: ‘st.session_state has no key “authentication_status”. Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization’. Althouth several seconds ago st.session_state wasn’t empty. What can be reason of it? What should i do to solve this?

Hello @jeka28667,

Welcome to the community! We’re thrilled to have you here! :tada:

Would you have a link to your repo by any chance? That would help us diagnose the issue more accurately.

Thanks!
Charly

@Charly_Wargnier Thank you) Unfortunately, i can’t share link, it is for internal using without external access. But i can share code.
start.py:

import streamlit as st
import streamlit_authenticator as stauth
import yaml
from yaml.loader import SafeLoader


def main():
    st.set_page_config(layout="wide")

    with open('config_aut.yaml', encoding="UTF-8") as file:
        config = yaml.load(file, Loader=SafeLoader)

    authenticator = stauth.Authenticate(
        config['credentials'],
        config['cookie']['name'],
        config['cookie']['key'],
        config['cookie']['expiry_days'],
        config['preauthorized']
    )

    name, authentication_status, username = authenticator.login()
    #print(name, authentication_status, username)
    #st.write(name, authentication_status, username)
    # Initialize the authentication_status key in session state
    if "authentication_status" not in st.session_state:
        st.session_state["authentication_status"] = authentication_status

    if st.session_state["authentication_status"]:
        authenticator.logout()
        st.title(f'Welcome, *{st.session_state["name"]}*')
    elif st.session_state["authentication_status"] is False:
        st.error('Username/password is incorrect')
    elif st.session_state["authentication_status"] is None:
        st.warning('Please enter your username and password')

if __name__ == "__main__":
    main()

first_page.py:

import streamlit as st

st.write(st.session_state)  
if st.session_state["authentication_status"]:
....
elif st.session_state["authentication_status"] is None:
    st.error('You must be logged in')

Thanks for this, @jeka28667!

Did you make sure that st.session_state was initialized correctly on each page? Checking that the keys, like authentication_status, are used consistently across your app.

Charly

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