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']}"
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.