import streamlit as st from streamlit_gsheets import GSheetsConnection import hmac # Standard imports import numpy as np import pandas as pd from datetime import datetime import os import random # Import torch import torch # Data visualization import matplotlib.pyplot as plt import seaborn as sns # Path manipulation from pathlib import Path import sys # Custom and other imports import project_config from utils import add_logo from menu import menu # Insert logo # add_logo(project_config.MEDIA_DIR / 'gravity_logo.png') # Initialize st.session_state.role to None if "role" not in st.session_state: st.session_state.role = None # # Retrieve the role from Session State to initialize the widget # st.session_state._role = st.session_state.role # def set_role(): # # Callback function to save the role selection to Session State # st.session_state.role = st.session_state._role def check_password(): """Returns `True` if the user had a correct password.""" def login_form(): """Form with widgets to collect user information""" with st.form("Credentials"): st.text_input("Username", key="username") st.text_input("Password", type="password", key="password") st.form_submit_button("Log In", on_click=password_entered) def password_entered(): """Checks whether a password entered by the user is correct.""" # Create a connection object to Google Sheets conn = st.connection("gsheets", type=GSheetsConnection) # Read the user database user_db = conn.read() user_db.dropna(axis=0, how="all", inplace=True) user_db.dropna(axis=1, how="all", inplace=True) # Check if the username is in the database if st.session_state["username"] in user_db.username.values: st.session_state["username_correct"] = True # Check if the password is correct if hmac.compare_digest( st.session_state["password"], user_db.loc[user_db.username == st.session_state["username"], "password"].values[0], ): st.session_state["password_correct"] = True # Check if the username is an admin if st.session_state["username"] in user_db[user_db.role == "admin"].username.values: st.session_state["role"] = "admin" else: st.session_state["role"] = "user" # Retrieve and store user name and team st.session_state["name"] = user_db.loc[user_db.username == st.session_state["username"], "name"].values[0] st.session_state["team"] = user_db.loc[user_db.username == st.session_state["username"], "team"].values[0] # Don't store the username or password del st.session_state["password"] # del st.session_state["username"] else: st.session_state["password_correct"] = False else: st.session_state["username_correct"] = False st.session_state["password_correct"] = False # Return True if the username + password is validated if st.session_state.get("password_correct", False): return True # Show inputs for username + password login_form() if "password_correct" in st.session_state: if not st.session_state["username_correct"]: st.error("User not found.") elif not st.session_state["password_correct"]: st.error("The password you entered is incorrect.") else: st.error("An unexpected error occurred.") return False menu() # Render the dynamic menu! if not check_password(): st.stop()