Hi,
I am new to streamlit, so there is probably an easy way to do this, but basically I have an expanding container in which I’m putting a login widget. I want to control the state of the login widget based on the authentication. It is initialized in the expanded state. On successful login, I want to minimize the expander and change the title of the expander to “Welcome {user name}”
I know session.state might help, but the only implementation I’ve found is to use a dedicated button to switch between the expander states. but that is not possible for the UI I want.
Any help?
Here’s an implementation that you can modify to fit your requirements.
import streamlit as st
def main():
st.title("Welcome to My Super Cool App")
if 'is_logged_in' not in st.session_state:
st.session_state['is_logged_in'] = False
if st.session_state['is_logged_in']:
with st.expander(label=f"Welcome {st.session_state['user_name']}", expanded=False):
st.write("🎉 Hooray! You've unlocked the secret chamber of this app!")
else:
with st.expander(label="Please log in", expanded=True):
user_name = st.text_input("Username")
password = st.text_input("Password", type="password")
if st.button("Login", on_click=login, args=(user_name, password)):
pass
def login(user_name, password):
if user_name == "User" and password == "supersecret": # In real life, use secure password handling!
st.session_state['is_logged_in'] = True
st.session_state['user_name'] = user_name
# Kick off the app
main()
Hi @tonykip, thanks for replying! I think the implementation of the login widget might be a newer one(or older one I don’t know).
Please check my code and help me out with implementing your solution
so what I want to achieve is a command with which I can change the attributes(label and expanded) of login_widget, which I would write under if authentication status:
My confusion is arising from your use of a separate function for login, which is not the case in this repository. the passwords are hashed and the login authenticator is written in a different file. Thank you very much for your patience!
Can you please paste the code in a code block so I can copy it and also include the code for authentication? You can mask out any sensitive information or add dummy values.
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.