2052man commited on
Commit
9705159
1 Parent(s): d8f6194

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +163 -157
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
- @st.cache_data
7
- def load_schedule():
8
- try:
9
- schedule_data = pd.read_csv('schedule.csv')
10
- except FileNotFoundError:
11
- schedule_data = pd.DataFrame(columns=['Class', 'Day', 'Start Time', 'Username', 'System'])
12
- return schedule_data
13
-
14
- # Save schedule data to CSV
15
- def save_schedule(schedule_data):
16
- schedule_data.to_csv('schedule.csv', index=False)
17
-
18
- # Load user details from CSV
19
- @st.cache_data
20
- def load_user_details():
21
- try:
22
- user_details = pd.read_csv('details.csv')
23
- except FileNotFoundError:
24
- user_details = pd.DataFrame(columns=['username', 'password'])
25
- return user_details
26
-
27
- # Check for system availability
28
- def has_system_availability(schedule_data, class_selected, day_selected, start_time_selected, system_selected):
29
- bookings = schedule_data[
30
- (schedule_data['Class'] == class_selected) &
31
- (schedule_data['Day'] == day_selected) &
32
- (schedule_data['Start Time'] == start_time_selected)
33
- ]
34
- if len(bookings) >= 6:
35
- return False
36
- elif system_selected in bookings['System'].values:
37
- return False
38
- else:
39
- return True
40
-
41
- # Convert string day and time to datetime
42
- def get_class_datetime(day_selected, start_time_selected):
43
- days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday"]
44
- today = datetime.now()
45
- current_day_index = today.weekday()
46
-
47
- # Find the date of the next occurrence of the given day
48
- target_day_index = days_of_week.index(day_selected)
49
- delta_days = (target_day_index - current_day_index) % 7
50
-
51
- # If the class day is today but time has passed, move to the next week
52
- if delta_days == 0 and datetime.strptime(start_time_selected, "%I:%M %p").time() < today.time():
53
- delta_days = 7
54
-
55
- class_date = today + timedelta(days=delta_days)
56
- class_time = datetime.strptime(start_time_selected, "%I:%M %p").time()
57
- class_datetime = datetime.combine(class_date.date(), class_time)
58
-
59
- return class_datetime
60
-
61
- # Initialize session state variables
62
- if 'username' not in st.session_state:
63
- st.session_state.username = ""
64
- if 'password' not in st.session_state:
65
- st.session_state.password = ""
66
- if 'schedule_data' not in st.session_state:
67
- st.session_state.schedule_data = load_schedule()
68
- if 'authenticated' not in st.session_state:
69
- st.session_state.authenticated = False
70
- if 'is_admin' not in st.session_state:
71
- st.session_state.is_admin = False
72
-
73
- # Login Page
74
- def login_page():
75
- st.title("Login")
76
- st.session_state.username = st.text_input("Username")
77
- st.session_state.password = st.text_input("Password", type='password')
78
-
79
- user_details = load_user_details()
80
-
81
- if st.button("Login"):
82
- if (
83
- (st.session_state.username in user_details['username'].values) and
84
- (st.session_state.password in user_details[user_details['username'] == st.session_state.username]['password'].values)
85
- ) or (st.session_state.username == "armin" and st.session_state.password == "12345679"):
86
- st.session_state.authenticated = True
87
- st.session_state.is_admin = (st.session_state.username == "armin")
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 = ["mobile", "Math", "Chemistry", "Programming"]
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
- class_to_remove = st.selectbox("Class", st.session_state.schedule_data['Class'].unique(), key='remove_class_select')
127
- user_booking = st.session_state.schedule_data[
128
- (st.session_state.schedule_data['Class'] == class_to_remove) &
129
- (st.session_state.schedule_data['Username'] == st.session_state.username)
130
- ]
131
-
132
- if st.button("Remove Class"):
133
- if st.session_state.is_admin or not user_booking.empty:
134
- st.session_state.schedule_data = st.session_state.schedule_data[~(
135
- (st.session_state.schedule_data['Class'] == class_to_remove) &
136
- ((st.session_state.is_admin) or (st.session_state.schedule_data['Username'] == st.session_state.username))
137
- )]
138
- save_schedule(st.session_state.schedule_data)
139
- st.success(f"Class {class_to_remove} removed successfully.")
140
- else:
141
- st.error("You can only remove your own classes.")
142
-
143
- st.header("Your Schedule")
144
- st.table(st.session_state.schedule_data)
145
-
146
- if st.button("Log Out"):
147
- st.session_state['authenticated'] = False
148
- st.session_state['username'] = ""
149
- st.session_state['password'] = ""
150
- st.session_state['is_admin'] = False
151
- st.experimental_rerun()
152
-
153
- # Authentication check
154
- if not st.session_state['authenticated']:
155
- login_page()
156
- else:
157
- schedule_page()
 
 
 
 
 
 
 
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()