-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
727 lines (617 loc) · 33.3 KB
/
app.py
File metadata and controls
727 lines (617 loc) · 33.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
import streamlit as st
import pandas as pd
import numpy as np
import io
from scheduler import ScheduleGenerator
from data_manager import DataManager
from visualizations import ScheduleVisualizer
import plotly.express as px
import plotly.graph_objects as go
# Set page title and layout
st.set_page_config(
page_title="Class Scheduling System",
layout="wide",
initial_sidebar_state="expanded"
)
# Initialize session state variables if they don't exist
if 'courses' not in st.session_state:
st.session_state.courses = []
if 'teachers' not in st.session_state:
st.session_state.teachers = []
if 'rooms' not in st.session_state:
st.session_state.rooms = []
if 'classes' not in st.session_state:
st.session_state.classes = []
if 'schedule' not in st.session_state:
st.session_state.schedule = None
if 'edit_mode' not in st.session_state:
st.session_state.edit_mode = False
if 'view_mode' not in st.session_state:
st.session_state.view_mode = "Class" # Default view mode
if 'conflicts' not in st.session_state:
st.session_state.conflicts = []
# Initialize the data manager
data_manager = DataManager()
# Title and introduction
st.title("Class Scheduling System")
st.markdown("""
This system helps generate conflict-free timetables for multiple classes, accounting for:
- Teacher availability
- Room availability
- Class time constraints
- Course frequencies (3x or 5x per week)
""")
# Sidebar for navigation
st.sidebar.title("Navigation")
section = st.sidebar.radio(
"Select a section:",
["Input Data", "Generate Schedule", "View Schedule", "Manage Conflicts"]
)
# Define time slots
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
TIME_SLOTS = [
"8:00-8:50", "9:00-9:50", "10:00-10:50", "11:00-11:50",
"12:00-12:50", "1:00-1:50", "2:00-2:50", "3:00-3:50"
]
# Input Data Section
if section == "Input Data":
st.header("Input Data")
# Create tabs for different data inputs
input_tab = st.tabs(["Teachers", "Rooms", "Classes", "Courses"])
# Teachers tab
with input_tab[0]:
st.subheader("Manage Teachers")
# Input for adding new teacher
with st.form("add_teacher_form"):
new_teacher_name = st.text_input("Teacher Name")
new_teacher_subjects = st.text_input("Subjects (comma-separated)")
teacher_submit = st.form_submit_button("Add Teacher")
if teacher_submit and new_teacher_name:
subjects = [s.strip() for s in new_teacher_subjects.split(",") if s.strip()]
data_manager.add_teacher(new_teacher_name, subjects)
st.success(f"Teacher {new_teacher_name} added!")
st.session_state.teachers = data_manager.get_teachers()
st.rerun()
# Display existing teachers
if st.session_state.teachers:
teacher_df = pd.DataFrame(st.session_state.teachers)
st.dataframe(teacher_df)
# Option to remove teachers
teacher_to_remove = st.selectbox(
"Select teacher to remove",
options=[t['name'] for t in st.session_state.teachers],
index=None
)
if st.button("Remove Teacher") and teacher_to_remove:
data_manager.remove_teacher(teacher_to_remove)
st.success(f"Teacher {teacher_to_remove} removed!")
st.session_state.teachers = data_manager.get_teachers()
st.rerun()
else:
st.info("No teachers added yet. Add your first teacher above.")
# Rooms tab
with input_tab[1]:
st.subheader("Manage Rooms")
# Input for adding new room
with st.form("add_room_form"):
new_room_name = st.text_input("Room Name/Number")
new_room_capacity = st.number_input("Capacity", min_value=1, value=30)
room_submit = st.form_submit_button("Add Room")
if room_submit and new_room_name:
data_manager.add_room(new_room_name, new_room_capacity)
st.success(f"Room {new_room_name} added!")
st.session_state.rooms = data_manager.get_rooms()
st.rerun()
# Display existing rooms
if st.session_state.rooms:
room_df = pd.DataFrame(st.session_state.rooms)
st.dataframe(room_df)
# Option to remove rooms
room_to_remove = st.selectbox(
"Select room to remove",
options=[r['name'] for r in st.session_state.rooms],
index=None
)
if st.button("Remove Room") and room_to_remove:
data_manager.remove_room(room_to_remove)
st.success(f"Room {room_to_remove} removed!")
st.session_state.rooms = data_manager.get_rooms()
st.rerun()
else:
st.info("No rooms added yet. Add your first room above.")
# Classes tab
with input_tab[2]:
st.subheader("Manage Classes")
# Input for adding new class
with st.form("add_class_form"):
new_class_name = st.text_input("Class Name")
new_class_size = st.number_input("Number of Students", min_value=1, value=25)
class_submit = st.form_submit_button("Add Class")
if class_submit and new_class_name:
data_manager.add_class(new_class_name, new_class_size)
st.success(f"Class {new_class_name} added!")
st.session_state.classes = data_manager.get_classes()
st.rerun()
# Display existing classes
if st.session_state.classes:
class_df = pd.DataFrame(st.session_state.classes)
st.dataframe(class_df)
# Option to remove classes
class_to_remove = st.selectbox(
"Select class to remove",
options=[c['name'] for c in st.session_state.classes],
index=None
)
if st.button("Remove Class") and class_to_remove:
data_manager.remove_class(class_to_remove)
st.success(f"Class {class_to_remove} removed!")
st.session_state.classes = data_manager.get_classes()
st.rerun()
else:
st.info("No classes added yet. Add your first class above.")
# Courses tab
with input_tab[3]:
st.subheader("Manage Courses")
# Input for adding new course
with st.form("add_course_form"):
new_course_name = st.text_input("Course Name")
new_course_teacher = st.selectbox(
"Select Teacher",
options=[t['name'] for t in st.session_state.teachers] if st.session_state.teachers else ["No teachers available"],
index=0 if st.session_state.teachers else None,
disabled=not st.session_state.teachers
)
assigned_class = st.selectbox(
"Assign to Class",
options=[c['name'] for c in st.session_state.classes] if st.session_state.classes else ["No classes available"],
index=0 if st.session_state.classes else None,
disabled=not st.session_state.classes
)
course_duration = st.selectbox("Duration (minutes)", options=[50, 60, 90], index=0)
course_frequency = st.selectbox("Frequency (per week)", options=[1, 2, 3, 4, 5], index=2)
course_submit = st.form_submit_button("Add Course")
if course_submit and new_course_name and new_course_teacher != "No teachers available" and assigned_class != "No classes available":
data_manager.add_course(
name=new_course_name,
teacher=new_course_teacher,
assigned_class=assigned_class,
duration=course_duration,
frequency=course_frequency
)
st.success(f"Course {new_course_name} added!")
st.session_state.courses = data_manager.get_courses()
st.rerun()
# Display existing courses
if st.session_state.courses:
course_df = pd.DataFrame(st.session_state.courses)
st.dataframe(course_df)
# Option to remove courses
course_to_remove = st.selectbox(
"Select course to remove",
options=[f"{c['name']} ({c['class']})" for c in st.session_state.courses],
index=None
)
if st.button("Remove Course") and course_to_remove:
course_name = course_to_remove.split(" (")[0]
class_name = course_to_remove.split("(")[1].replace(")", "")
data_manager.remove_course(course_name, class_name)
st.success(f"Course {course_to_remove} removed!")
st.session_state.courses = data_manager.get_courses()
st.rerun()
else:
st.info("No courses added yet. Add your first course above.")
# Option to add sample data for demo
st.subheader("Sample Data")
if st.button("Load Sample Data"):
# Add sample teachers
sample_teachers = [
{"name": "Dr. Smith", "subjects": ["Physics"]},
{"name": "Mrs. Johnson", "subjects": ["Chemistry"]},
{"name": "Mr. Davis", "subjects": ["Mathematics"]},
{"name": "Ms. Wilson", "subjects": ["English"]},
{"name": "Mr. Martinez", "subjects": ["PE"]},
{"name": "Dr. Brown", "subjects": ["Biology"]},
{"name": "Ms. Lee", "subjects": ["Chinese"]},
{"name": "Mr. Harris", "subjects": ["History"]},
{"name": "Mrs. Clark", "subjects": ["Electives"]}
]
# Add sample rooms
sample_rooms = [
{"name": "Room 101", "capacity": 30},
{"name": "Room 102", "capacity": 30},
{"name": "Room 103", "capacity": 30},
{"name": "Science Lab", "capacity": 25},
{"name": "Gym", "capacity": 50},
{"name": "Library", "capacity": 40},
{"name": "Room 201", "capacity": 30},
{"name": "Room 202", "capacity": 30}
]
# Add sample classes
sample_classes = [
{"name": "Class 9A", "size": 25},
{"name": "Class 9B", "size": 27},
{"name": "Class 10A", "size": 23},
{"name": "Class 10B", "size": 26}
]
# Add sample courses
sample_courses = [
{"name": "Physics", "teacher": "Dr. Smith", "class": "Class 9A", "duration": 50, "frequency": 3},
{"name": "Chemistry", "teacher": "Mrs. Johnson", "class": "Class 9A", "duration": 50, "frequency": 3},
{"name": "Algebra", "teacher": "Mr. Davis", "class": "Class 9A", "duration": 50, "frequency": 5},
{"name": "English", "teacher": "Ms. Wilson", "class": "Class 9A", "duration": 50, "frequency": 5},
{"name": "PE", "teacher": "Mr. Martinez", "class": "Class 9A", "duration": 50, "frequency": 3},
{"name": "Biology", "teacher": "Dr. Brown", "class": "Class 9A", "duration": 50, "frequency": 3},
{"name": "Chinese", "teacher": "Ms. Lee", "class": "Class 9A", "duration": 50, "frequency": 5},
{"name": "World History", "teacher": "Mr. Harris", "class": "Class 9A", "duration": 50, "frequency": 3},
{"name": "Electives", "teacher": "Mrs. Clark", "class": "Class 9A", "duration": 50, "frequency": 3},
{"name": "Physics", "teacher": "Dr. Smith", "class": "Class 9B", "duration": 50, "frequency": 3},
{"name": "Chemistry", "teacher": "Mrs. Johnson", "class": "Class 9B", "duration": 50, "frequency": 3},
{"name": "Algebra", "teacher": "Mr. Davis", "class": "Class 9B", "duration": 50, "frequency": 5},
{"name": "English", "teacher": "Ms. Wilson", "class": "Class 9B", "duration": 50, "frequency": 5},
{"name": "PE", "teacher": "Mr. Martinez", "class": "Class 9B", "duration": 50, "frequency": 3},
{"name": "Biology", "teacher": "Dr. Brown", "class": "Class 9B", "duration": 50, "frequency": 3},
{"name": "Chinese", "teacher": "Ms. Lee", "class": "Class 9B", "duration": 50, "frequency": 5},
{"name": "World History", "teacher": "Mr. Harris", "class": "Class 9B", "duration": 50, "frequency": 3},
{"name": "Electives", "teacher": "Mrs. Clark", "class": "Class 9B", "duration": 50, "frequency": 3}
]
# Clear existing data
data_manager.clear_all_data()
# Add sample data
for teacher in sample_teachers:
data_manager.add_teacher(teacher["name"], teacher["subjects"])
for room in sample_rooms:
data_manager.add_room(room["name"], room["capacity"])
for cls in sample_classes:
data_manager.add_class(cls["name"], cls["size"])
for course in sample_courses:
data_manager.add_course(
course["name"],
course["teacher"],
course["class"],
course["duration"],
course["frequency"]
)
# Update session state
st.session_state.teachers = data_manager.get_teachers()
st.session_state.rooms = data_manager.get_rooms()
st.session_state.classes = data_manager.get_classes()
st.session_state.courses = data_manager.get_courses()
st.success("Sample data loaded successfully!")
st.rerun()
# Generate Schedule Section
elif section == "Generate Schedule":
st.header("Generate Schedule")
if not st.session_state.courses or not st.session_state.rooms or not st.session_state.teachers or not st.session_state.classes:
st.warning("Please add teachers, rooms, classes, and courses before generating a schedule.")
st.info("Go to the 'Input Data' section to add the required data.")
else:
st.subheader("Schedule Generation Options")
with st.form("schedule_options_form"):
start_time = st.time_input("School day start time", value=pd.to_datetime("08:00").time())
end_time = st.time_input("School day end time", value=pd.to_datetime("15:15").time())
period_length = st.slider("Period length (minutes)", min_value=30, max_value=120, value=50, step=5)
break_length = st.slider("Break between periods (minutes)", min_value=5, max_value=30, value=10, step=5)
lunch_period = st.selectbox("Lunch period", options=["No lunch", "After period 3", "After period 4"])
lunch_duration = st.slider("Lunch duration (minutes)", min_value=20, max_value=60, value=30, step=5, disabled=lunch_period=="No lunch")
generate_button = st.form_submit_button("Generate Schedule")
if generate_button:
with st.spinner("Generating schedule..."):
# Configure the schedule generator
scheduler = ScheduleGenerator(
teachers=st.session_state.teachers,
rooms=st.session_state.rooms,
classes=st.session_state.classes,
courses=st.session_state.courses,
start_time=start_time,
end_time=end_time,
period_length=period_length,
break_length=break_length,
lunch_period=lunch_period,
lunch_duration=lunch_duration
)
# Generate the schedule
schedule, conflicts = scheduler.generate_schedule()
# Update session state
st.session_state.schedule = schedule
st.session_state.conflicts = conflicts
if conflicts:
st.error(f"Schedule generated with {len(conflicts)} conflicts. Please see the 'Manage Conflicts' section.")
else:
st.success("Schedule generated successfully with no conflicts!")
if st.session_state.schedule is not None:
st.subheader("Generated Schedule Summary")
# Display a summary of the generated schedule
n_classes = len(set([item['class'] for item in st.session_state.schedule]))
n_courses = len(set([(item['class'], item['course']) for item in st.session_state.schedule]))
n_slots = len(st.session_state.schedule)
col1, col2, col3 = st.columns(3)
col1.metric("Classes Scheduled", n_classes)
col2.metric("Courses Assigned", n_courses)
col3.metric("Total Time Slots", n_slots)
st.info("Go to the 'View Schedule' section to see the detailed timetable.")
# View Schedule Section
elif section == "View Schedule":
st.header("View Schedule")
if st.session_state.schedule is None:
st.warning("No schedule has been generated yet.")
st.info("Go to the 'Generate Schedule' section to create a schedule.")
else:
# Create view options
st.subheader("View Options")
view_col1, view_col2 = st.columns(2)
with view_col1:
view_mode = st.radio("View by:", ["Class", "Teacher", "Room"])
st.session_state.view_mode = view_mode
with view_col2:
if view_mode == "Class":
selected_filter = st.selectbox(
"Select Class:",
options=[c['name'] for c in st.session_state.classes]
)
elif view_mode == "Teacher":
selected_filter = st.selectbox(
"Select Teacher:",
options=[t['name'] for t in st.session_state.teachers]
)
else: # Room view
selected_filter = st.selectbox(
"Select Room:",
options=[r['name'] for r in st.session_state.rooms]
)
# Create visualizer
visualizer = ScheduleVisualizer(st.session_state.schedule)
# Display the schedule
st.subheader(f"Schedule by {view_mode}: {selected_filter}")
if view_mode == "Class":
fig = visualizer.generate_class_schedule(selected_filter)
st.plotly_chart(fig, use_container_width=True)
# Show detailed table
detailed_schedule = visualizer.get_class_schedule_data(selected_filter)
if not detailed_schedule.empty:
st.subheader("Detailed Schedule")
st.dataframe(detailed_schedule, use_container_width=True)
else:
st.info(f"No schedule data available for class {selected_filter}")
elif view_mode == "Teacher":
fig = visualizer.generate_teacher_schedule(selected_filter)
st.plotly_chart(fig, use_container_width=True)
# Show detailed table
detailed_schedule = visualizer.get_teacher_schedule_data(selected_filter)
if not detailed_schedule.empty:
st.subheader("Detailed Schedule")
st.dataframe(detailed_schedule, use_container_width=True)
else:
st.info(f"No schedule data available for teacher {selected_filter}")
else: # Room view
fig = visualizer.generate_room_schedule(selected_filter)
st.plotly_chart(fig, use_container_width=True)
# Show detailed table
detailed_schedule = visualizer.get_room_schedule_data(selected_filter)
if not detailed_schedule.empty:
st.subheader("Detailed Schedule")
st.dataframe(detailed_schedule, use_container_width=True)
else:
st.info(f"No schedule data available for room {selected_filter}")
# Export options
st.subheader("Export Options")
export_col1, export_col2 = st.columns(2)
with export_col1:
if st.button("Export Schedule to CSV"):
full_schedule_df = visualizer.get_full_schedule_data()
csv = full_schedule_df.to_csv(index=False)
st.download_button(
label="Download CSV",
data=csv,
file_name="class_schedule.csv",
mime="text/csv"
)
with export_col2:
if st.button("Export Schedule to Excel"):
if view_mode == "Class":
detailed_schedule = visualizer.get_class_schedule_data(selected_filter)
title = f"Schedule for {selected_filter}"
elif view_mode == "Teacher":
detailed_schedule = visualizer.get_teacher_schedule_data(selected_filter)
title = f"Schedule for Teacher: {selected_filter}"
else: # Room view
detailed_schedule = visualizer.get_room_schedule_data(selected_filter)
title = f"Schedule for Room: {selected_filter}"
full_schedule_df = visualizer.get_full_schedule_data()
# Create a buffer to store Excel file
buffer = io.BytesIO()
# Create Excel writer
with pd.ExcelWriter(buffer, engine='xlsxwriter') as writer:
# Write current view to first sheet
if not detailed_schedule.empty:
# Reset the index to make it cleaner
if 'time' in detailed_schedule.columns:
detailed_schedule.set_index('time', inplace=True)
# First worksheet - current view
detailed_schedule.to_excel(writer, sheet_name=f'{view_mode} Schedule')
worksheet = writer.sheets[f'{view_mode} Schedule']
# Get access to the workbook
workbook = writer.book
# Create title format
title_format = workbook.add_format({
'bold': True,
'font_size': 14,
'align': 'center',
'valign': 'vcenter'
})
# Create header format
header_format = workbook.add_format({
'bold': True,
'bg_color': '#4B5563', # Dark gray like in the screenshot
'font_color': 'white',
'border': 1,
'align': 'center',
'valign': 'vcenter'
})
# Create time cell format
time_format = workbook.add_format({
'bold': True,
'bg_color': '#374151', # Darker gray for time cells
'font_color': 'white',
'border': 1,
'align': 'center',
'valign': 'vcenter'
})
# Create course cell formats for different subjects (using Plotly colors)
colors = {
'Physics': '#19D3F3', # Light blue
'Chemistry': '#636EFA', # Blue
'Algebra': '#FF6692', # Pink
'English': '#EF553B', # Red-orange
'PE': '#FFA15A', # Light orange
'Biology': '#AB63FA', # Purple
'Chinese': '#00CC96', # Teal
'World History': '#B6E880', # Light green
'Electives': '#FECB52' # Yellow
}
course_formats = {}
for course, color in colors.items():
course_formats[course] = workbook.add_format({
'bg_color': color,
'font_color': 'white' if course in ['Physics', 'Chemistry', 'Algebra', 'English', 'Biology', 'World History'] else 'black',
'border': 1,
'align': 'center',
'valign': 'vcenter'
})
# Default course format for any courses not in our predefined list
default_course_format = workbook.add_format({
'bg_color': '#D3D3D3', # Light gray
'border': 1,
'align': 'center',
'valign': 'vcenter'
})
# Add title above the schedule (with merged cells)
col_count = len(detailed_schedule.columns) + 1 # +1 for the index column
worksheet.merge_range(0, 0, 0, col_count - 1, title, title_format)
# Offset the data by one row to make room for the title
worksheet.write_row(1, 0, [""] + list(detailed_schedule.columns), header_format)
# Apply formatting to all cells including the time column
for row_idx, time_slot in enumerate(detailed_schedule.index):
# Write time slot with special formatting
worksheet.write(row_idx + 2, 0, time_slot, time_format)
# Format the data cells based on course name
for col_idx, day in enumerate(detailed_schedule.columns):
cell_value = detailed_schedule.at[time_slot, day]
if pd.notna(cell_value):
# Find the appropriate format based on the course name
cell_format = course_formats.get(cell_value, default_course_format)
worksheet.write(row_idx + 2, col_idx + 1, cell_value, cell_format)
else:
# Empty cell
worksheet.write(row_idx + 2, col_idx + 1, "", default_course_format)
# Auto-adjust column widths
for i in range(col_count):
col_width = 15 # Standardized width for all columns
worksheet.set_column(i, i, col_width)
# Write full schedule to second sheet
full_schedule_df.to_excel(writer, sheet_name='Full Schedule', index=False)
worksheet_full = writer.sheets['Full Schedule']
# Format the full schedule sheet
full_header_format = workbook.add_format({
'bold': True,
'bg_color': '#4B5563',
'font_color': 'white',
'border': 1,
'align': 'center',
'valign': 'vcenter'
})
# Write the header with the formatted style
for col_num, value in enumerate(full_schedule_df.columns.values):
worksheet_full.write(0, col_num, value, full_header_format)
# Format the data rows with alternating colors
row_format1 = workbook.add_format({
'border': 1,
'bg_color': '#F3F4F6', # Light gray for even rows
'align': 'center',
'valign': 'vcenter'
})
row_format2 = workbook.add_format({
'border': 1,
'bg_color': '#FFFFFF', # White for odd rows
'align': 'center',
'valign': 'vcenter'
})
# Apply alternating row colors
for row_num in range(1, len(full_schedule_df) + 1):
format_to_use = row_format1 if row_num % 2 == 0 else row_format2
for col_num in range(len(full_schedule_df.columns)):
worksheet_full.write(row_num, col_num, full_schedule_df.iloc[row_num-1, col_num], format_to_use)
# Auto-adjust columns' width
for i, col in enumerate(full_schedule_df.columns):
max_len = max(full_schedule_df[col].astype(str).map(len).max(), len(col)) + 2
worksheet_full.set_column(i, i, max_len)
# Reset buffer position
buffer.seek(0)
# Provide download link for Excel file
st.download_button(
label="Download Excel",
data=buffer,
file_name=f"{view_mode.lower()}_{selected_filter}_schedule.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
# Manage Conflicts Section
elif section == "Manage Conflicts":
st.header("Manage Conflicts")
if st.session_state.schedule is None:
st.warning("No schedule has been generated yet.")
st.info("Go to the 'Generate Schedule' section to create a schedule.")
elif not st.session_state.conflicts:
st.success("No conflicts detected in the current schedule.")
else:
st.warning(f"There are {len(st.session_state.conflicts)} conflicts in the current schedule.")
# Display conflicts
conflict_data = []
for conflict in st.session_state.conflicts:
conflict_data.append({
"Type": conflict["type"],
"Day": conflict["day"],
"Time": conflict["time"],
"Description": conflict["description"]
})
conflict_df = pd.DataFrame(conflict_data)
st.dataframe(conflict_df, use_container_width=True)
# Conflict resolution
st.subheader("Resolve Conflicts")
conflict_to_resolve = st.selectbox(
"Select conflict to resolve:",
options=[f"{c['type']} - {c['day']} {c['time']} - {c['description']}" for c in st.session_state.conflicts],
index=0
)
selected_conflict_idx = [f"{c['type']} - {c['day']} {c['time']} - {c['description']}" for c in st.session_state.conflicts].index(conflict_to_resolve)
selected_conflict = st.session_state.conflicts[selected_conflict_idx]
st.subheader("Resolution Options")
resolution_type = st.radio(
"Resolution approach:",
["Reschedule", "Change room", "Change teacher"]
)
if resolution_type == "Reschedule":
new_day = st.selectbox("New day:", options=DAYS)
new_time = st.selectbox("New time:", options=TIME_SLOTS)
if st.button("Apply Change"):
st.info("This would update the schedule with the new time slot.")
st.warning("Manual conflict resolution is not fully implemented. Please regenerate the schedule.")
elif resolution_type == "Change room":
new_room = st.selectbox(
"New room:",
options=[r['name'] for r in st.session_state.rooms]
)
if st.button("Apply Change"):
st.info("This would update the room assignment for the conflicting course.")
st.warning("Manual conflict resolution is not fully implemented. Please regenerate the schedule.")
elif resolution_type == "Change teacher":
new_teacher = st.selectbox(
"New teacher:",
options=[t['name'] for t in st.session_state.teachers]
)
if st.button("Apply Change"):
st.info("This would update the teacher assignment for the conflicting course.")
st.warning("Manual conflict resolution is not fully implemented. Please regenerate the schedule.")
# Option to regenerate schedule
st.subheader("Regenerate Schedule")
if st.button("Regenerate Schedule"):
st.info("This would regenerate the schedule with updated constraints.")
st.warning("Please go to the 'Generate Schedule' section to regenerate the schedule.")