|
import streamlit as st |
|
from datetime import datetime |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
from database import register_user, user_exists, check_user_credentials, save_user_history, get_user_history |
|
except ImportError: |
|
st.error("Could not import database functions in user_management.py. Please ensure database.py exists and contains required functions.") |
|
|
|
def get_user_history(user_id): |
|
st.warning("Database function 'get_user_history' not implemented. History will not be persistent.") |
|
return [] |
|
def register_user(user_id, full_name, dob, email, password): |
|
st.warning("Database function 'register_user' not implemented. User registration will not be persistent.") |
|
return True |
|
def user_exists(user_id): |
|
st.warning("Database function 'user_exists' not implemented. User existence check will not work.") |
|
return False |
|
def check_user_credentials(user_id, password): |
|
st.warning("Database function 'check_user_credentials' not implemented. Login will not work.") |
|
return False |
|
def save_user_history(user_id, symptoms, predicted_diseases): |
|
st.warning("Database function 'save_user_history' not implemented. History saving will not be persistent.") |
|
pass |
|
|
|
|
|
def render_user_management_sidebar(session_state): |
|
""" |
|
Handles user registration and login forms in the sidebar. |
|
Receives the Streamlit session_state object to manage user login status. |
|
""" |
|
st.sidebar.header("User Management π§ββοΈ") |
|
|
|
|
|
if session_state.get("user_id"): |
|
st.sidebar.write(f"Welcome, {session_state.user_id}!") |
|
if st.sidebar.button("Logout", key="logout_button_sidebar"): |
|
session_state.user_id = None |
|
session_state.logged_in_user = None |
|
st.success("Logged out successfully!") |
|
st.rerun() |
|
else: |
|
|
|
form_type = st.sidebar.selectbox("Choose Action", ["Sign In", "Register"], key="form_type_sidebar") |
|
|
|
if form_type == "Sign In": |
|
with st.sidebar.form("login_form_sidebar"): |
|
st.markdown("### Sign In") |
|
login_user_id = st.text_input("User ID", key="login_user_id_input_sidebar") |
|
login_password = st.text_input("Password", type="password", key="login_password_input_sidebar") |
|
submit_login = st.form_submit_button("Sign In") |
|
|
|
if submit_login: |
|
if not login_user_id or not login_password: |
|
st.sidebar.error("Please enter both User ID and Password.") |
|
else: |
|
with st.spinner("Signing in..."): |
|
if check_user_credentials(login_user_id, login_password): |
|
session_state.user_id = login_user_id |
|
session_state.logged_in_user = login_user_id |
|
st.sidebar.success("Signed in successfully!") |
|
st.rerun() |
|
else: |
|
st.sidebar.error("Invalid User ID or Password.") |
|
else: |
|
with st.sidebar.form("register_form_sidebar"): |
|
st.markdown("### Register") |
|
reg_user_id = st.text_input("Choose a unique User ID", key="reg_user_id_input_sidebar") |
|
full_name = st.text_input("Full Name", key="reg_full_name_input_sidebar") |
|
dob = st.date_input("Date of Birth", min_value=datetime(1900, 1, 1), max_value=datetime.today(), key="reg_dob_input_sidebar") |
|
email = st.text_input("Email (optional)", key="reg_email_input_sidebar") |
|
reg_password = st.text_input("Password", type="password", key="reg_password_input_sidebar") |
|
submit_register = st.form_submit_button("Register") |
|
|
|
if submit_register: |
|
if not reg_user_id or not reg_password or not full_name: |
|
st.sidebar.error("User ID, Full Name, and Password are required.") |
|
else: |
|
with st.spinner("Checking User ID..."): |
|
if user_exists(reg_user_id): |
|
st.sidebar.error("User ID already taken. Please choose another.") |
|
else: |
|
with st.spinner("Registering user..."): |
|
|
|
if register_user(reg_user_id, full_name, dob, email or None, reg_password): |
|
session_state.user_id = reg_user_id |
|
session_state.logged_in_user = reg_user_id |
|
st.sidebar.success("Registered successfully! You are now logged in.") |
|
st.rerun() |
|
else: |
|
st.sidebar.error("Registration failed. Please try again.") |
|
|
|
def save_history_to_db_if_logged_in(user_id: str, symptoms: str, predicted_diseases: str): |
|
""" |
|
Saves user interaction history to the database. |
|
This function is called from app.py when a user is logged in. |
|
It directly calls the database function to save data. |
|
""" |
|
|
|
with st.spinner("Saving history..."): |
|
save_user_history(user_id, symptoms, predicted_diseases) |
|
|
|
|