Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,157 +1,163 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
from datetime import datetime, timedelta
|
4 |
-
|
5 |
-
# Load schedule data from CSV
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
user_details = pd.
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
(schedule_data['
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
return False
|
36 |
-
|
37 |
-
return
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
class_datetime
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
if '
|
63 |
-
st.session_state.
|
64 |
-
if '
|
65 |
-
st.session_state.
|
66 |
-
if '
|
67 |
-
st.session_state.
|
68 |
-
if '
|
69 |
-
st.session_state.
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
st.
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
st.session_state.
|
88 |
-
st.experimental_rerun()
|
89 |
-
else:
|
90 |
-
st.error("Invalid username or password. Please try again.")
|
91 |
-
|
92 |
-
# Class Schedule Page
|
93 |
-
def schedule_page():
|
94 |
-
st.title("Class Schedule")
|
95 |
-
|
96 |
-
classes = ["
|
97 |
-
days = ["Sunday", "Monday", "Tuesday", "Wednesday"]
|
98 |
-
start_times = ["8:00 AM", "10:00 AM", "12:00 PM", "2:00 PM"]
|
99 |
-
systems = ["System 1", "System 2", "System 3", "System 4", "System 5", "System 6"]
|
100 |
-
|
101 |
-
st.header("Manage Classes")
|
102 |
-
|
103 |
-
col1, col2 = st.columns(2)
|
104 |
-
|
105 |
-
with col1:
|
106 |
-
st.subheader("Add a Class")
|
107 |
-
class_selected = st.selectbox("Class", classes, key='add_class_select')
|
108 |
-
day_selected = st.selectbox("Day", days, key='add_day_select')
|
109 |
-
start_time_selected = st.selectbox("Start Time", start_times, key='add_time_select')
|
110 |
-
system_selected = st.selectbox("System", systems, key='add_system_select')
|
111 |
-
|
112 |
-
if st.button("Add Class"):
|
113 |
-
class_datetime = get_class_datetime(day_selected, start_time_selected)
|
114 |
-
if class_datetime - datetime.now() < timedelta(hours=30):
|
115 |
-
st.error("Classes can only be booked at least 30 hours in advance.")
|
116 |
-
elif not has_system_availability(st.session_state.schedule_data, class_selected, day_selected, start_time_selected, system_selected):
|
117 |
-
st.error(f"System '{system_selected}' is not available for '{class_selected}' at {start_time_selected} on {day_selected}.")
|
118 |
-
else:
|
119 |
-
new_row = pd.DataFrame([[class_selected, day_selected, start_time_selected, st.session_state.username, system_selected]], columns=['Class', 'Day', 'Start Time', 'Username', 'System'])
|
120 |
-
st.session_state.schedule_data = pd.concat([st.session_state.schedule_data, new_row], ignore_index=True)
|
121 |
-
save_schedule(st.session_state.schedule_data)
|
122 |
-
st.success(f"Class {class_selected} added successfully with {system_selected}.")
|
123 |
-
|
124 |
-
with col2:
|
125 |
-
st.subheader("Remove a Class")
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from datetime import datetime, timedelta
|
4 |
+
|
5 |
+
# Load schedule data from CSV
|
6 |
+
def load_schedule():
|
7 |
+
try:
|
8 |
+
schedule_data = pd.read_csv('schedule.csv')
|
9 |
+
except FileNotFoundError:
|
10 |
+
schedule_data = pd.DataFrame(columns=['Class', 'Day', 'Start Time', 'Username', 'System'])
|
11 |
+
return schedule_data
|
12 |
+
|
13 |
+
# Save schedule data to CSV
|
14 |
+
def save_schedule(schedule_data):
|
15 |
+
schedule_data.to_csv('schedule.csv', index=False)
|
16 |
+
|
17 |
+
# Load user details from CSV
|
18 |
+
def load_user_details():
|
19 |
+
try:
|
20 |
+
user_details = pd.read_csv('details.csv')
|
21 |
+
except FileNotFoundError:
|
22 |
+
user_details = pd.DataFrame(columns=['username', 'password'])
|
23 |
+
return user_details
|
24 |
+
|
25 |
+
# Check for system availability
|
26 |
+
def has_system_availability(schedule_data, class_selected, day_selected, start_time_selected, system_selected):
|
27 |
+
bookings = schedule_data[
|
28 |
+
(schedule_data['Class'] == class_selected) &
|
29 |
+
(schedule_data['Day'] == day_selected) &
|
30 |
+
(schedule_data['Start Time'] == start_time_selected)
|
31 |
+
]
|
32 |
+
if len(bookings) >= 6:
|
33 |
+
return False
|
34 |
+
elif system_selected in bookings['System'].values:
|
35 |
+
return False
|
36 |
+
else:
|
37 |
+
return True
|
38 |
+
|
39 |
+
# Convert string day and time to datetime
|
40 |
+
def get_class_datetime(day_selected, start_time_selected):
|
41 |
+
days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Friday"]
|
42 |
+
today = datetime.now()
|
43 |
+
current_day_index = today.weekday()
|
44 |
+
|
45 |
+
# Find the date of the next occurrence of the given day
|
46 |
+
target_day_index = days_of_week.index(day_selected)
|
47 |
+
delta_days = (target_day_index - current_day_index) % 7
|
48 |
+
|
49 |
+
# If the class day is today but time has passed, move to the next week
|
50 |
+
if delta_days == 0 and datetime.strptime(start_time_selected, "%I:%M %p").time() < today.time():
|
51 |
+
delta_days = 7
|
52 |
+
|
53 |
+
class_date = today + timedelta(days=delta_days)
|
54 |
+
class_time = datetime.strptime(start_time_selected, "%I:%M %p").time()
|
55 |
+
class_datetime = datetime.combine(class_date.date(), class_time)
|
56 |
+
|
57 |
+
return class_datetime
|
58 |
+
|
59 |
+
# Initialize session state variables
|
60 |
+
if 'username' not in st.session_state:
|
61 |
+
st.session_state.username = ""
|
62 |
+
if 'password' not in st.session_state:
|
63 |
+
st.session_state.password = ""
|
64 |
+
if 'schedule_data' not in st.session_state:
|
65 |
+
st.session_state.schedule_data = load_schedule()
|
66 |
+
if 'authenticated' not in st.session_state:
|
67 |
+
st.session_state.authenticated = False
|
68 |
+
if 'is_admin' not in st.session_state:
|
69 |
+
st.session_state.is_admin = False
|
70 |
+
|
71 |
+
# Login Page
|
72 |
+
def login_page():
|
73 |
+
st.title("Login")
|
74 |
+
st.session_state.username = st.text_input("Username")
|
75 |
+
st.session_state.password = st.text_input("Password", type='password')
|
76 |
+
|
77 |
+
user_details = load_user_details()
|
78 |
+
|
79 |
+
if st.button("Login"):
|
80 |
+
if (
|
81 |
+
(st.session_state.username in user_details['username'].values) and
|
82 |
+
(st.session_state.password in user_details[user_details['username'] == st.session_state.username]['password'].values)
|
83 |
+
) or (st.session_state.username == "armin" and st.session_state.password == "12345679"):
|
84 |
+
st.session_state.authenticated = True
|
85 |
+
st.session_state.is_admin = (st.session_state.username == "armin")
|
86 |
+
# Load schedule data again after login
|
87 |
+
st.session_state.schedule_data = load_schedule()
|
88 |
+
st.experimental_rerun()
|
89 |
+
else:
|
90 |
+
st.error("Invalid username or password. Please try again.")
|
91 |
+
|
92 |
+
# Class Schedule Page
|
93 |
+
def schedule_page():
|
94 |
+
st.title("Class Schedule")
|
95 |
+
|
96 |
+
classes = ["Physics", "Math", "Chemistry", "Programming"]
|
97 |
+
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Friday"]
|
98 |
+
start_times = ["8:00 AM", "10:00 AM", "12:00 PM", "2:00 PM"]
|
99 |
+
systems = ["System 1", "System 2", "System 3", "System 4", "System 5", "System 6"]
|
100 |
+
|
101 |
+
st.header("Manage Classes")
|
102 |
+
|
103 |
+
col1, col2 = st.columns(2)
|
104 |
+
|
105 |
+
with col1:
|
106 |
+
st.subheader("Add a Class")
|
107 |
+
class_selected = st.selectbox("Class", classes, key='add_class_select')
|
108 |
+
day_selected = st.selectbox("Day", days, key='add_day_select')
|
109 |
+
start_time_selected = st.selectbox("Start Time", start_times, key='add_time_select')
|
110 |
+
system_selected = st.selectbox("System", systems, key='add_system_select')
|
111 |
+
|
112 |
+
if st.button("Add Class"):
|
113 |
+
class_datetime = get_class_datetime(day_selected, start_time_selected)
|
114 |
+
if class_datetime - datetime.now() < timedelta(hours=30):
|
115 |
+
st.error("Classes can only be booked at least 30 hours in advance.")
|
116 |
+
elif not has_system_availability(st.session_state.schedule_data, class_selected, day_selected, start_time_selected, system_selected):
|
117 |
+
st.error(f"System '{system_selected}' is not available for '{class_selected}' at {start_time_selected} on {day_selected}.")
|
118 |
+
else:
|
119 |
+
new_row = pd.DataFrame([[class_selected, day_selected, start_time_selected, st.session_state.username, system_selected]], columns=['Class', 'Day', 'Start Time', 'Username', 'System'])
|
120 |
+
st.session_state.schedule_data = pd.concat([st.session_state.schedule_data, new_row], ignore_index=True)
|
121 |
+
save_schedule(st.session_state.schedule_data)
|
122 |
+
st.success(f"Class {class_selected} added successfully with {system_selected}.")
|
123 |
+
|
124 |
+
with col2:
|
125 |
+
st.subheader("Remove a Class")
|
126 |
+
if not st.session_state.schedule_data.empty and 'Username' in st.session_state.schedule_data.columns:
|
127 |
+
user_classes = st.session_state.schedule_data[
|
128 |
+
(st.session_state.schedule_data['Username'] == st.session_state.username) |
|
129 |
+
st.session_state.is_admin
|
130 |
+
]
|
131 |
+
if not user_classes.empty:
|
132 |
+
class_to_remove = st.selectbox("Class", user_classes['Class'].unique(), key='remove_class_select')
|
133 |
+
|
134 |
+
if st.button("Remove Class"):
|
135 |
+
if st.session_state.is_admin or not user_classes.empty:
|
136 |
+
st.session_state.schedule_data = st.session_state.schedule_data[~(
|
137 |
+
(st.session_state.schedule_data['Class'] == class_to_remove) &
|
138 |
+
((st.session_state.is_admin) or (st.session_state.schedule_data['Username'] == st.session_state.username))
|
139 |
+
)]
|
140 |
+
save_schedule(st.session_state.schedule_data)
|
141 |
+
st.success(f"Class {class_to_remove} removed successfully.")
|
142 |
+
else:
|
143 |
+
st.error("You can only remove your own classes.")
|
144 |
+
else:
|
145 |
+
st.error("You have no classes to remove.")
|
146 |
+
else:
|
147 |
+
st.error("No classes available for removal or 'Username' column is missing.")
|
148 |
+
|
149 |
+
st.header("Your Schedule")
|
150 |
+
st.table(st.session_state.schedule_data)
|
151 |
+
|
152 |
+
if st.button("Log Out"):
|
153 |
+
st.session_state['authenticated'] = False
|
154 |
+
st.session_state['username'] = ""
|
155 |
+
st.session_state['password'] = ""
|
156 |
+
st.session_state['is_admin'] = False
|
157 |
+
st.experimental_rerun()
|
158 |
+
|
159 |
+
# Authentication check
|
160 |
+
if not st.session_state['authenticated']:
|
161 |
+
login_page()
|
162 |
+
else:
|
163 |
+
schedule_page()
|