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() ?
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']}"