Clear container after login form submit

try to create a simple login form and once logged in, display a welcome greet.
However, after placeholder.empty() is called, it doesn’t seem to clear the whole form, only the username text input box. I simply wish to have a clear page after login form submission successfully. Am I correct to use st.empty() ?

import streamlit as st

login_details = {
  "credentials": {
    "usernames": {
      "jsmith": {
        "email": "abc@yahoo.com",
        "name": "John Smith",
        "password": 123
      },
      "rbriggs": {
        "email": "rbriggs@gmail.com",
        "name": "Rebecca Briggs",
        "password": 456
      }
    }
  },
  "cookie": {
    "expiry_days": 1,
    "key": "some_signature_key",
    "name": "cookie_name_streamlit_authenticator"
  },
  "preauthorized": {
    "emails": [
      "abc@gmail.com"
    ]
  }
}
placeholder = st.empty()
placeholder.title("login")

with placeholder.form("login"):
    username = st.text_input("username")
    password = st.text_input("password", type="password")
    submit_button = st.form_submit_button("submit")


    if submit_button:
        if username in login_details["credentials"]["usernames"]:
            print(f"username: {username} found")
            print(f"password: {login_details['credentials']['usernames'][username]['password']}")
            if password == str(login_details["credentials"]["usernames"][username]["password"]):
                print(password)
                st.success("Login successful")
                placeholder.empty()
                with placeholder.container():
                    st.write(f"hello, {login_details['credentials']['usernames'][username]['name']}")
            else:
                st.error("Login failed")

hi @kaizhang, you could try something like:

    form = st.form(key='my_form')
    form.text_input(label='Enter some text')
    submit_button = form.form_submit_button(label='Login')
    if submit_button:
        st.session_state['loggedin']=True
        st.experimental_rerun()```

the rerun forces the script to run again, and the loggidin flag in the session_state tells the script that the form does not have to be generated again. hope that helps.

thanks for the reply but I still am struggling to get this to work.
Not sure why you set the session_state variable or why do a rerun.
Anything under the rerun will not run so if I put
st.write(f"Welcome {st.session_state.username}") under that, it simply skpped.
I also tried below

st.session_state['login'] = False
login_form = st.form(key='login_form')
username = login_form.text_input(label='username')
password = login_form.text_input(label='password', type='password')
submit_button = login_form.form_submit_button(label='submit')

if submit_button:
    if username in login_details["credentials"]["usernames"]:
        if password == str(login_details["credentials"]["usernames"][username]["password"]):
            st.session_state.username = username
            st.session_state['login'] = True
            st.success("Login successful")
            st.experimental_rerun()
        else:
            st.error("Login failed")

if st.session_state['login']:
    st.write(f"Welcome {st.session_state.username}")

The code will never get to the last 2 lines

All I want to do is simply have a clean page after login form submit successfully.

Got it working in the end. Not sure if this is the best way, but @godot63 's reply got me thinking that if st.experimental_rerun(), session_state variable stays when the page is rerun. so here is my solution hope it’s useful to others who wish to control the visibility of a container.

if 'login' not in st.session_state:
  login_form = st.form(key='login_form')
  username = login_form.text_input(label='username')
  password = login_form.text_input(label='password', type='password')
  submit_button = login_form.form_submit_button(label='submit')


  if submit_button:
    if username in login_details["credentials"]["usernames"]:
        if password == str(login_details["credentials"]["usernames"][username]["password"]):
            st.session_state.username = username
            st.session_state['login'] = True
            st.success("Login successful")
            st.experimental_rerun()
        else:
            st.error("Login failed")

if 'login' in st.session_state:
    st.empty()
    st.write(f"hello, {login_details['credentials']['usernames'][username]['name']}"

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