Streamlit Session State - Deleted when switch tabs

Hi @randyzwitch , I managed to find a solution!

What I did was basically use st.empty().
That way I got how to maintain.session_state which it kept! and maintain code performance maintain.

The code was written as follows:

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

    if "password_correct" not in st.session_state:
        st.session_state["password_correct"] = False

    if st.session_state["password_correct"] == False:   
      with var2:
        col1, col2, col3 = st.columns([1.5,2,1.5])
        with col2:
          with st.form("Login"):
            if ["username"] not in st.session_state:
              st.session_state.username = st.text_input("Username")
            if ["password"] not in st.session_state:
              st.session_state.password = st.text_input("Password", type="password")
            login = st.form_submit_button("Login")
            if login:
              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
                return True
              else:
                st.session_state["password_correct"] = False
                st.error("😕 User not known or password incorrect")
                    
    elif st.session_state["password_correct"] == True:
        return True
         
if check_password():
     var2.empty()