Query related to clearing widget options if the user get clicked on logout

the following is my code:

sub_selected=‘File Uploading’
username=st.sidebar.text_input(‘Username’)
password=st.sidebar.text_input(‘Password’,type=‘password’)

if 'c01' not in st.session_state:
    st.session_state.c01=False
c01_state=st.session_state.c01
c01_state=st.sidebar.checkbox('Login',value=c01_state)
if c01_state:
    
    create_usertable()
    hashed_pwsd=generated_hashes(password)
    result=login_user(username,verified_hashes(password,hashed_pwsd))
    if result:
        c01_state=True
        st.session_state.c01=True
        #st.session_state.checkbox_login = True
        progress_bar = st.progress(0.0)
        status_text = st.empty()
        #st.session_state.c01=c01_state

        for i in range(100):
            progress_bar.progress((i + 1) / 100)
            status_text.text(f"Processing {i+1}%")
            time.sleep(0.01) # Add a delay to simulate processing time

        status_text.text("Processing completed!")
        #st.success('Welcome {}'.format(username))
    else:
        st.warning('Incorrect usernmae/password')

if st.sidebar.button('Logout'):
# set login state to false and clear credentials
    st.sidebar.empty()
    c01_state = False
    st.session_state.c01 = False
    #st.session_state.checkbox_login = False
    #st.sidebar.empty()
    st.warning('Successfully logged out')
    
if c01_state:

    sub_selected = option_menu(
            menu_title='Machine Learning Menu',
            options=['File Uploading','EDA','Model Building','Deploying models without parameters',
            'Deploying models with parameters',"Comparison of Algorithm's accuracy","Comparison of Algorithm's recall-scores",
            "Comparison of Algorithm's precision-scores","Comparison of Algorithm's F1-scores",
            "Comparison of Algorithm's AUC-scores"],
            orientation='vertical',
            default_index=0,
        )
    if sub_selected=='File Uploading':
        file_upload()
    elif sub_selected=='EDA':
        eda()
        

    elif sub_selected == 'Model Building':
        model_building()
    elif sub_selected=='Deploying models without parameters':
        deploying_models_without_parameters()
    elif sub_selected=='Deploying models with parameters':
        deploying_models_with_parameters()
    elif sub_selected=="Comparison of Algorithm's accuracy":
        accuracy()
    elif sub_selected=="Comparison of Algorithm's recall-scores":
        recall()
    elif sub_selected=="Comparison of Algorithm's precision-scores":
        precision()
    elif sub_selected=="Comparison of Algorithm's F1-scores":
        f1_score()
    elif sub_selected=="Comparison of Algorithm's AUC-scores":
        auc()
        # Add a logout button

And the result of the code is below:

The functionality is working some what properly. But, when I click on logout button,
I need to clear all the widget fields into empty state.
Can anyone help me to my query?

Hi @Guna_Sekhar,

Thanks for posting! You can use the st.empty() method to remove the contents of the widget upon the user logging out.

Here’s the modified version of your logout logic that you can try:

if st.sidebar.button('Logout'):
    # Clear all widget fields
    st.sidebar.empty()
    st.empty()
    c01_state = False
    st.session_state.c01 = False
    st.warning('Successfully logged out')

What’s happening in the code above:

  1. st.sidebar.empty(): removes all widgets in the sidebar.
  2. st.empty(): removes all widgets in the main area of the app.
  3. c01_state = False: resets the value of the c01_state variable to False, which indicates that the user is not logged in.
  4. st.session_state.c01 = False: resets the value of the c01 key in the session state dictionary to False, which is used to track whether the user is logged in.

Please let me know if this is helpful!

Happy Streamlit-ing! :balloon:

1 Like

Anyways thanks for the suggestion.
But when I’m using ur logic, the functionality is performing good what I had expected. But the values of widgets are still appearing. So the following is the video link. It’s better fro you to understand my problem sir.
Link:-

1 Like

Hi @Guna_Sekhar,

Thank you for sharing the recording of the issue. To clear the sidebar credentials upon logging out, you can try the following:

if st.sidebar.button('Logout'):
    # Clear all widget fields
    st.sidebar.text_input('Username', value='')
    st.sidebar.text_input('Password', value='', type='password')
    st.sidebar.empty()
    st.empty()
    c01_state = False
    st.session_state.c01 = False
    st.warning('Successfully logged out')

The username and password inputs in the sidebar will be reset to empty strings when the user logs out of the app.

Let me know if this resolves the issue.