import streamlit as st from datetime import datetime, timedelta import random import logging # Sample data structure for user_tasks if 'user_tasks' not in st.session_state: st.session_state.user_tasks = {} # Initialize user preferences (this can also be loaded from a database or file if needed) if 'user_preferences' not in st.session_state: st.session_state.user_preferences = {"name": None, "email": None} st.session_state.logged_in = False # Initialize the logger logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') # Helper functions def generate_numeric_id(): return int(datetime.now().timestamp() * 1000) # Login form def login(): st.title("Login") username = st.text_input("Enter your name") email = st.text_input("Enter your email") if st.button("Login"): if username and email: # Set session state for logged-in user st.session_state.user_preferences['name'] = username st.session_state.user_preferences['email'] = email st.session_state.logged_in = True logging.info(f"User logged in: {username}") else: st.warning("Please enter both name and email.") # Logout function def logout(): st.session_state.logged_in = False st.session_state.user_preferences['name'] = None st.session_state.user_preferences['email'] = None st.success("Logged out successfully.") # Display tasks based on the selected section def display_tasks(tasks): for task in tasks: st.write(f"Task: {task['text']} - Due Date: {task['date']} - Completed: {task['completed']}") # Main App Logic if st.session_state.logged_in: st.sidebar.title(f"Welcome, {st.session_state.user_preferences['name']}!") if st.sidebar.button("Logout"): logout() # Clear session to log out # Task Input Form st.subheader("Add a New Task") with st.form("add_task_form"): task_text = st.text_input("Task Description") due_date = st.date_input("Due Date") if st.form_submit_button("Add Task"): task_id = generate_numeric_id() st.session_state.user_tasks[task_id] = { "id": task_id, "text": task_text, "date": due_date.strftime('%Y-%m-%d'), "completed": False } st.success("Task added successfully.") # Display Tasks st.subheader("Your Tasks") display_tasks(st.session_state.user_tasks.values()) # Clear All Tasks if st.button("Delete All Tasks"): st.session_state.user_tasks.clear() st.success("All tasks deleted.") else: # Render login form if the user is not logged in login()