I have to click the login button twice initially to show the welcome message
if 'username' not in st.session_state:
st.session_state.username = ' '
if st.session_state.username:
st.write('welcome ',st.session_state.username)
else:
st.title('Login/Signup')
email = st.text_input('Email')
password = st.text_input('Password',type='password')
if st.button('Login'):
try:
userinfo = sign_in_with_email_and_password(email,password)
st.session_state.username = userinfo['username']
st.session_state.useremail = userinfo['email']
except:
st.warning('Login Failed')
I am trying to create a login form. Once a person is logged in using correct user name and password, only then userinfo[âusernameâ] will contain a value. Not sure how to display the welcome message on the first button click itself. If I click the button twice, then it is working fine.
I have already tried it. st.rerun() fails inside the try except blocks.
Error:
RerunData(page_script_hash=âd575aef6f69f53c7a18ce42c5b281cbbâ, is_fragment_scoped_rerun=True)
Ah yes, I should have thought of that. The problem is that the way that st.rerun works is that it raises a special exception, and that tells Streamlit that it needs to rerun the page.
The best situation would be to figure out what sort of error the sign_in_with_email_and_password would raise, or just raise one yourself, and handle that specific error.
For example:
def sign_in_with_email_and_password(email, password):
# Replace with real implementation
if (email, password) not in get_logins():
raise ValueError("Invalid login")
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.