Session_state initializing errors

Hey I’ve been working on a website where you have to log in first before accessing the website. I’ve been using a checkbox to log in but I want to switch over to a button and using the session state function to get this to work. Only I’ve been getting this error “AttributeError: st.session_state has no attribute “load_state”. Did you forget to initialize it?” and I’ve no clue how to fix this.

This is the code I’ve been using:

import pyrebase
import streamlit as st
from matlab_integration_laptop import Matlab_Calc

amount_of_peoples = 1

firebaseConfig = {
  "apiKey": "AIzaSyCxJfOQVBDT3cck7kMKJyCBgK8cdLCkeUI",
  "authDomain": "test-fireestore-streamlit.firebaseapp.com",
  "projectId": "test-fireestore-streamlit",
  "databaseURL": "https://test-fireestore-streamlit-default-rtdb.europe-west1.firebasedatabase.app/",
  "storageBucket": "test-fireestore-streamlit.appspot.com",
  "messagingSenderId": "110694863664",
  "appId": "1:110694863664:web:03f705aa4e180ff7762ef5",
  "measurementId": "G-2F25EY60QH"
}

firebase = pyrebase.initialize_app(firebaseConfig)
auth = firebase.auth()

db = firebase.database()
storage = firebase.storage()

st.sidebar.title("Our diabetes app")

choice = st.sidebar.selectbox('login/signup',['Login','Sign up'])

email = st.sidebar.text_input('Please enter your email address')
password = st.sidebar.text_input('Please enter your password', type = 'password')

if choice == 'Sign up':
    handle = st.sidebar.text_input('Please input your app username')
    submit = st.sidebar.button('Create my account')
    
    if submit:
        user = auth.create_user_with_email_and_password(email,password)
        st.success('Your account has been created successfully')
        st.balloons()
        
        user = auth.sign_in_with_email_and_password(email,password)
        db.child(user['localId']).child("Handle").set(handle)
        db.child(user['localId']).child("ID").set(user['localId'])
        st.title('Welcome '+handle)
        st.info('Login via login option')
        
data_pd = {}
weight_var = 0

if "load_state" not in st.session_state:
    st.session_state.load_state = False



if choice == 'Login':
    login = st.sidebar.checkbox('Login')
    if login or st.session_state.load_state: 
        st.session_state.load_state = True
        user = auth.sign_in_with_email_and_password(email,password)
        st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)
    
        bio = st.radio('Choose an option:',['Home','Add food','Pick food','Base settings'])
        if bio == 'Base settings':
            if db.child(user['localId']).child("Weight").get().val() is not None:
                st.write("Your last weight input was "+str(db.child(user['localId']).child("Weight").get().val())+" kg")
            if db.child(user['localId']).child("Weight").get().val() is not None:
                weight_var = db.child(user['localId']).child("Weight").get().val()
            weight = st.number_input('Please enter your bodyweight (in kg)')
            weight_button = st.button('Submit!')
            if weight_button and weight != 0:
                if weight_var>weight:
                    st.success("Great job!")
                db.child(user['localId']).child("Weight").set(weight)
                st.success('Your weight has been added successfully')
        elif bio == 'Home':
            st.write("Welcome to the Pentabetes diabetes tester!!")
        elif bio == 'Add food':
            Food_addition = st.text_input("Type the name of what food you want to add")
            Carb_addition = st.number_input("Amount of carbohydrates per 100 grams of chosen food")
            press_input = st.button("Add food")
            if press_input and Carb_addition != 0:
                data_pd[Food_addition] = Carb_addition
                db.child(user['localId']).child("Food").child(Food_addition).set(data_pd)
                st.success('Your food has been added successfully')
        elif bio == 'Pick food':
            if db.child(user['localId']).child("Food").get().val() is not None and db.child(user['localId']).child("Weight").get().val() is not None:
                data_pd_1 = db.child(user['localId']).child("Food").get().val()
                weight_1 = db.child(user['localId']).child("Weight").get().val()
                Chosen_food = st.selectbox("Select food you want to eat",data_pd_1)
                Amount_food = st.number_input("grams of "+Chosen_food+" you want to eat")
                time = st.number_input('Time until consumption (min)')
                Select_food = st.button("Calculate food")
                Add_food = st.checkbox("Add second food item")
                if Select_food and weight_1 != None:
                    carb_100 = db.child(user['localId']).child("Food").child(Chosen_food).get().val()
                    carbs = ((((carb_100[Chosen_food])/100)*1000)*Amount_food)
                    Test_1 = Matlab_Calc(amount_of_peoples, weight_1, carbs, time)
                    if Test_1 == 'YES GOOD YES EAT':
                        st.success("You can eat "+str(Amount_food)+" grams of "+Chosen_food)
                    else:
                        st.error("You cannot eat "+str(Amount_food)+" grams of "+Chosen_food)
                if Add_food:
                    Chosen_food_1 = st.selectbox("Select second food you want to eat",data_pd_1)
                    Amount_food_1 = st.number_input("grams of "+Chosen_food_1+" you want to eat ")
                    Select_food_1 = st.button("Calculate first and second food items")
                    Add_food_1 = st.checkbox("Add third food item")
                    if Select_food_1 and weight_1 != None:
                        carb_100 = db.child(user['localId']).child("Food").child(Chosen_food).get().val()
                        carb_100_1 = db.child(user['localId']).child("Food").child(Chosen_food_1).get().val()
                        carbs_1 = (((((carb_100_1[Chosen_food_1])/100)*1000)*Amount_food_1)+((((carb_100[Chosen_food])/100)*1000)*Amount_food))
                        Test_1_1 = Matlab_Calc(amount_of_peoples, weight_1, carbs_1, time)
                        if Test_1_1 == 'YES GOOD YES EAT':
                            st.success("You can eat "+str(Amount_food)+" grams of "+Chosen_food+" and "+str(Amount_food_1)+" grams of "+Chosen_food_1)
                        else:
                            st.error("You cannot eat "+str(Amount_food)+" grams of "+Chosen_food+" and "+str(Amount_food_1)+" grams of "+Chosen_food_1)
                    if Add_food_1:
                        Chosen_food_2 = st.selectbox("Select third food you want to eat",data_pd_1)
                        Amount_food_2 = st.number_input("grams of "+Chosen_food_2+" you want to eat  ")
                        Select_food_2 = st.button("Calculate all food items")
                        if Select_food_2 and weight_1 != None:
                            carb_100 = db.child(user['localId']).child("Food").child(Chosen_food).get().val()
                            carb_100_1 = db.child(user['localId']).child("Food").child(Chosen_food_1).get().val()
                            carb_100_2 = db.child(user['localId']).child("Food").child(Chosen_food_2).get().val()
                            carbs_2 = (((((carb_100_1[Chosen_food_1])/100)*1000)*Amount_food_1)+((((carb_100[Chosen_food])/100)*1000)*Amount_food)+((((carb_100_2[Chosen_food_2])/100)*1000)*Amount_food_2))
                            Test_1_2 = Matlab_Calc(amount_of_peoples, weight_1, carbs_2, time)
                            if Test_1_2 == 'YES GOOD YES EAT':
                                st.success("You can eat "+str(Amount_food)+" grams of "+Chosen_food+" and "+str(Amount_food_1)+" grams of "+Chosen_food_1+" and "+str(Amount_food_2)+" grams of "+Chosen_food_2)
                            else:
                                st.error("You cannot eat "+str(Amount_food)+" grams of "+Chosen_food+" and "+str(Amount_food_1)+" grams of "+Chosen_food_1+" and "+str(Amount_food_2)+" grams of "+Chosen_food_2)
                                           
            elif db.child(user['localId']).child("Food").get().val() is None and db.child(user['localId']).child("Weight").get().val() is not None:
                st.info('Please add food')
            elif db.child(user['localId']).child("Weight").get().val() is None and db.child(user['localId']).child("Food").get().val() is not None :
                st.info('Please add your weight in the base settings')
            elif db.child(user['localId']).child("Weight").get().val() is None and db.child(user['localId']).child("Food").get().val() is None:
                st.info('Please add your weight in the base settings')
                st.info('Please add food')
                ```

Hi @Jeroen_Frieling ,

Welcome to the Streamlit community forum :balloon:
If I’m not wrong, we ( @andfanilo ) spoke about this error before .
Thank you for the code snippet. I went through it quickly, I don’t see any issue with session state initializing (though I’ve not tried it!)
I would like to request you to try session state code snippet from the Streamlit doc . Does that work for you?

Best
Avra

I’ve already tried the snippet of code from the streamlit doc. I’m starting to think it might have something to do with my streamlit version or something like that, since I haven’t been able to find any other type of code to do this.

Thanks for all the help,
Jeroen

Hey Jeroen ,

So, does that work for you ?

Hey Avra,

Nope it doesn’t work :sweat:

In that case, please paste here with the error message. And also the streamlit version you are working with . It be helpful to identify your issue and reproduce it.

I’m certain someone from the community can help you out🎈

(Cc @andfanilo )

Best,
Avra

I’m using the latest version of streamlit, so 1.3.0 and the error that I got is KeyError: 'st.session_state has no key “load_state”. Did you forget to initialize it?