Streamlit-Authenticator
To upload files, please first save the app
import streamlit as st
import streamlit_authenticator as stauth
import yaml
from yaml.loader import SafeLoader
import os
# Initialize configuration file if it doesn't exist
if not os.path.exists('config.yaml'):
config = {
'credentials': {
'usernames': {
'jsmith': {
'email': 'jsmith@gmail.com',
'name': 'John Smith',
'password': stauth.Hasher(['abc123']).generate()[0]
},
'rbriggs': {
'email': 'rbriggs@gmail.com',
'name': 'Rebecca Briggs',
'password': stauth.Hasher(['def456']).generate()[0]
}
}
},
'cookie': {
'expiry_days': 30,
'key': 'some_signature_key',
'name': 'some_cookie_name'
}
}
with open('config.yaml', 'w') as file:
yaml.dump(config, file, default_flow_style=False)
# Load configuration file
with open('config.yaml') as file:
config = yaml.load(file, Loader=SafeLoader)
# Create an authentication object
authenticator = stauth.Authenticate(
config['credentials'],
config['cookie']['name'],
config['cookie']['key'],
config['cookie']['expiry_days']
)
# Add login widget
name, authentication_status, username = authenticator.login('Login', 'main')
# Handle authentication status
if authentication_status == False:
st.error('Username/password is incorrect')
elif authentication_status == None:
st.warning('Please enter your username and password')
elif authentication_status:
# Show the main app content
authenticator.logout('Logout', 'main')
st.write(f'Welcome *{name}*')
st.title('Some content')
# Add a registration widget
try:
if authenticator.register_user('Register user', preauthorization=False):
st.success('User registered successfully')
with open('config.yaml', 'w') as file:
yaml.dump(config, file, default_flow_style=False)
except Exception as e:
st.error(e)
# Add a forgot password widget
try:
username_forgot_pw, email_forgot_password, random_password = authenticator.forgot_password('Forgot password')
if username_forgot_pw:
st.success('New password sent securely')
with open('config.yaml', 'w') as file:
yaml.dump(config, file, default_flow_style=False)
except Exception as e:
st.error(e)
# Add a forgot username widget
try:
username_forgot_username, email_forgot_username = authenticator.forgot_username('Forgot username')
if username_forgot_username:
st.success('Username sent securely')
except Exception as e:
st.error(e)
# Add a reset password widget
if authentication_status:
try:
if authenticator.reset_password(username, 'Reset password'):
st.success('Password modified successfully')
with open('config.yaml', 'w') as file:
yaml.dump(config, file, default_flow_style=False)
except Exception as e:
st.error(e)
# Add an update user details widget
if authentication_status:
try:
if authenticator.update_user_details(username, 'Update user details'):
st.success('Entries updated successfully')
with open('config.yaml', 'w') as file:
yaml.dump(config, file, default_flow_style=False)
except Exception as e:
st.error(e)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?