import streamlit as st import pandas as pd from datetime import datetime, timedelta # Load schedule data from CSV @st.cache_data def load_schedule(): try: schedule_data = pd.read_csv('schedule.csv') except FileNotFoundError: schedule_data = pd.DataFrame(columns=['Class', 'Day', 'Start Time', 'Username', 'System']) return schedule_data # Save schedule data to CSV def save_schedule(schedule_data): schedule_data.to_csv('schedule.csv', index=False) # Load user details from CSV @st.cache_data def load_user_details(): try: user_details = pd.read_csv('details.csv') except FileNotFoundError: user_details = pd.DataFrame(columns=['username', 'password']) return user_details # Check for system availability def has_system_availability(schedule_data, class_selected, day_selected, start_time_selected, system_selected): bookings = schedule_data[ (schedule_data['Class'] == class_selected) & (schedule_data['Day'] == day_selected) & (schedule_data['Start Time'] == start_time_selected) ] if len(bookings) >= 6: return False elif system_selected in bookings['System'].values: return False else: return True # Convert string day and time to datetime def get_class_datetime(day_selected, start_time_selected): days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday"] today = datetime.now() current_day_index = today.weekday() # Find the date of the next occurrence of the given day target_day_index = days_of_week.index(day_selected) delta_days = (target_day_index - current_day_index) % 7 # If the class day is today but time has passed, move to the next week if delta_days == 0 and datetime.strptime(start_time_selected, "%I:%M %p").time() < today.time(): delta_days = 7 class_date = today + timedelta(days=delta_days) class_time = datetime.strptime(start_time_selected, "%I:%M %p").time() class_datetime = datetime.combine(class_date.date(), class_time) return class_datetime # Initialize session state variables if 'username' not in st.session_state: st.session_state.username = "" if 'password' not in st.session_state: st.session_state.password = "" if 'schedule_data' not in st.session_state: st.session_state.schedule_data = load_schedule() if 'authenticated' not in st.session_state: st.session_state.authenticated = False if 'is_admin' not in st.session_state: st.session_state.is_admin = False # Login Page def login_page(): st.title("Login") st.session_state.username = st.text_input("Username") st.session_state.password = st.text_input("Password", type='password') user_details = load_user_details() if st.button("Login"): if ( (st.session_state.username in user_details['username'].values) and (st.session_state.password in user_details[user_details['username'] == st.session_state.username]['password'].values) ) or (st.session_state.username == "armin" and st.session_state.password == "12345679"): st.session_state.authenticated = True st.session_state.is_admin = (st.session_state.username == "armin") st.experimental_rerun() else: st.error("Invalid username or password. Please try again.") # Class Schedule Page def schedule_page(): st.title("Class Schedule") classes = ["mobile", "Math", "Chemistry", "Programming"] days = ["Sunday", "Monday", "Tuesday", "Wednesday"] start_times = ["8:00 AM", "10:00 AM", "12:00 PM", "2:00 PM"] systems = ["System 1", "System 2", "System 3", "System 4", "System 5", "System 6"] st.header("Manage Classes") col1, col2 = st.columns(2) with col1: st.subheader("Add a Class") class_selected = st.selectbox("Class", classes, key='add_class_select') day_selected = st.selectbox("Day", days, key='add_day_select') start_time_selected = st.selectbox("Start Time", start_times, key='add_time_select') system_selected = st.selectbox("System", systems, key='add_system_select') if st.button("Add Class"): class_datetime = get_class_datetime(day_selected, start_time_selected) if class_datetime - datetime.now() < timedelta(hours=30): st.error("Classes can only be booked at least 30 hours in advance.") elif not has_system_availability(st.session_state.schedule_data, class_selected, day_selected, start_time_selected, system_selected): st.error(f"System '{system_selected}' is not available for '{class_selected}' at {start_time_selected} on {day_selected}.") else: new_row = pd.DataFrame([[class_selected, day_selected, start_time_selected, st.session_state.username, system_selected]], columns=['Class', 'Day', 'Start Time', 'Username', 'System']) st.session_state.schedule_data = pd.concat([st.session_state.schedule_data, new_row], ignore_index=True) save_schedule(st.session_state.schedule_data) st.success(f"Class {class_selected} added successfully with {system_selected}.") with col2: st.subheader("Remove a Class") class_to_remove = st.selectbox("Class", st.session_state.schedule_data['Class'].unique(), key='remove_class_select') user_booking = st.session_state.schedule_data[ (st.session_state.schedule_data['Class'] == class_to_remove) & (st.session_state.schedule_data['Username'] == st.session_state.username) ] if st.button("Remove Class"): if st.session_state.is_admin or not user_booking.empty: st.session_state.schedule_data = st.session_state.schedule_data[~( (st.session_state.schedule_data['Class'] == class_to_remove) & ((st.session_state.is_admin) or (st.session_state.schedule_data['Username'] == st.session_state.username)) )] save_schedule(st.session_state.schedule_data) st.success(f"Class {class_to_remove} removed successfully.") else: st.error("You can only remove your own classes.") st.header("Your Schedule") st.table(st.session_state.schedule_data) if st.button("Log Out"): st.session_state['authenticated'] = False st.session_state['username'] = "" st.session_state['password'] = "" st.session_state['is_admin'] = False st.experimental_rerun() # Authentication check if not st.session_state['authenticated']: login_page() else: schedule_page()