I need help with something, my sidebar disappears

Summary

When i click on the Login/Sign Up dropdown, and then click on the email input field, after i enter the first alphabet the sidebar disappears. This problem started after i started using the session_state function

Code snippet:

import streamlit as st
import re

st.set_page_config(page_title = 'Test Envt', page_icon = '🚫', layout = 'wide')

if "user_status" not in st.session_state:
    st.session_state.user_status = 'logged_out'

# Define a regular expression pattern for a basic email validation
def is_valid_email(email):
    email_pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
    return re.match(email_pattern, email) is not None

# Define criteria for password validation
def is_valid_password(password):
    return len(password) >= 7

def userauth():
    print('enter userauth()')
    st.sidebar.title("BikeBuddy")
    st.sidebar.empty()

    if st.session_state.user_status == "logged_out":
        print('enter if')
        choice = st.sidebar.selectbox("Login/Sign Up", ['Login', 'Sign Up'])
        
        # Login
        if choice == "Login":
            print('enter login choice')
            email = st.sidebar.text_input("E-Mail")
            password = st.sidebar.text_input("Password", type="password")
            submit = st.sidebar.button("Login")
            
            if submit and is_valid_email(email) and is_valid_password(password):
                print('enter nested if')
                try:
                    print('enter try')
                    # st.title("Welcome, " + username)

                    # # Hide the sidebar after successful login
                    # st.markdown(
                    #     """
                    #     <style>
                    #     section[data-testid="stSidebar"][aria-expanded="true"]{
                    #         display: none;
                    #     }
                    #     </style>
                    #     """,
                    #     unsafe_allow_html=True,
                    # )
                    return "logged_in"
                except Exception as e:
                    print('enter except')
                    st.warning("Incorrect Email or Password.")
        
        # Sign Up
        elif choice == "Sign Up":
            name = st.sidebar.text_input("Full Name")
            email = st.sidebar.text_input("E-Mail")
            username = st.sidebar.text_input("Username")
            password = st.sidebar.text_input("Password", type="password")
            age = st.sidebar.number_input("Age", min_value=18, step=1)
            blood_types = ['Select', 'A+', 'A-', 'B+', 'B-', 'O+', 'O-', 'AB+', 'AB-']
            blood_group = st.sidebar.selectbox("Blood Group", blood_types)
            mob_number = st.sidebar.text_input("Mobile Number")
            submit = st.sidebar.button("Sign Up")
            
            if submit:
                try:
                    st.write("sign up try block")

                    st.success("Your account has been created successfully. Go ahead and log in.")
                    st.info("Welcome, " + username)
                except Exception as e:
                    pass

# ---------> Webapp entry point
if __name__ == "__main__":
    if st.session_state.user_status == "logged_out":
        # After successful login, change the session_state to "logged_in"
        print('before userauth()', st.session_state.user_status)
        st.session_state.user_status = userauth() # should return "logged_in"
        print('after userauth()', st.session_state.user_status)
        if st.session_state.user_status == "logged_in":
            print('runapp might execute..')

Output

This is output i was getting in the console after some debugging using print statements…
before userauth() logged_out
enter userauth()
enter if
enter login choice
after userauth() None

Expected behavior:

So initially the user isn’t logged in, so we let them login or sign up (then finally) login, so after the user logs in successfully, the sidebar is programmed to remain hidden(using the markdown function, which is commented for now)

Actual behavior:
So when i go to the Login/Sign Up dropdown, and click on it, the sidebar disappears, even when type something into the email field in login part, just by merely interacting with it the side disappears.

Debug info

  • Streamlit version: 1.26.0
  • Python version: 3.8.5
  • Using VSCODE and my full application is inside a virtual envt.
  • OS version: Windows 11
  • Browser version: FireFox Version 116.0.3 (64 Bit)

Links

Additional Stuff

If possible visit my github and check the code, it would really help me.
I have searched the internet, but i could not find any solution, This is my first post, i hope i fix it! Sorry if my post is too big and untidy and thanks in advance community!

Hi @Jason_Abba,

Thanks for posting!

So the main culprit for the disappearing sidebar is likely the st.sidebar.empty() function in your userauth() function. This made your app clear out and vanish with each interaction with the sidebar elements. Here’s a modified working example from your code:

import streamlit as st
import re

st.set_page_config(page_title='Test Envt', page_icon='🚫', layout='wide')

if "user_status" not in st.session_state:
    st.session_state.user_status = 'logged_out'

# Define a regular expression pattern for a basic email validation
def is_valid_email(email):
    email_pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
    return re.match(email_pattern, email) is not None

# Define criteria for password validation
def is_valid_password(password):
    return len(password) >= 7

def userauth():
    print('enter userauth()')
    st.sidebar.title("BikeBuddy")

    if st.session_state.user_status == "logged_out":
        print('enter if')
        choice = st.sidebar.selectbox("Login/Sign Up", ['Login', 'Sign Up'])
        
        # Login
        if choice == "Login":
            print('enter login choice')
            email = st.sidebar.text_input("E-Mail")
            password = st.sidebar.text_input("Password", type="password")
            submit = st.sidebar.button("Login")
            
            if submit and is_valid_email(email) and is_valid_password(password):
                print('enter nested if')
                try:
                    print('enter try')
                    st.title("Welcome back!")
                    st.session_state.user_status = "logged_in"
                    return "logged_in"
                except Exception as e:
                    print('enter except')
                    st.warning("Incorrect Email or Password.")
        
        # Sign Up
        elif choice == "Sign Up":
            name = st.sidebar.text_input("Full Name")
            email = st.sidebar.text_input("E-Mail")
            username = st.sidebar.text_input("Username")
            password = st.sidebar.text_input("Password", type="password")
            age = st.sidebar.number_input("Age", min_value=18, step=1)
            blood_types = ['Select', 'A+', 'A-', 'B+', 'B-', 'O+', 'O-', 'AB+', 'AB-']
            blood_group = st.sidebar.selectbox("Blood Group", blood_types)
            mob_number = st.sidebar.text_input("Mobile Number")
            submit = st.sidebar.button("Sign Up")
            
            if submit:
                try:
                    st.write("sign up try block")
                    st.success("Your account has been created successfully. Go ahead and log in.")
                    st.info("Welcome, " + username)
                except Exception as e:
                    pass

# ---------> Webapp entry point
if __name__ == "__main__":
    userauth()
    if st.session_state.user_status == "logged_in":
        st.write("You are logged in!")

Let me know if this is helpful.

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