Update app.py
Browse files
app.py
CHANGED
|
@@ -596,120 +596,107 @@ def generate_timetable(course_df, room_df, output_dir, room_slots, timetable):
|
|
| 596 |
|
| 597 |
# --- Final Working Process Function ---
|
| 598 |
def process(course_file, room_file):
|
|
|
|
| 599 |
import shutil
|
| 600 |
import zipfile
|
| 601 |
from collections import defaultdict
|
| 602 |
import pandas as pd
|
| 603 |
import os
|
| 604 |
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
room_df = pd.read_excel(room_file.name)
|
| 608 |
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
"Seminar Halls", "Seminar Capacity"
|
| 614 |
-
]
|
| 615 |
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
|
|
|
|
|
|
|
|
|
| 619 |
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
os.makedirs(path)
|
| 624 |
|
| 625 |
-
|
| 626 |
-
|
|
|
|
|
|
|
| 627 |
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 633 |
|
| 634 |
-
# β
Build master room data
|
| 635 |
-
room_data = []
|
| 636 |
-
for i in range(len(room_df)):
|
| 637 |
-
# Class Room
|
| 638 |
-
if pd.notna(room_df["Class Room"].iloc[i]) and pd.notna(room_df["Class Capacity"].iloc[i]):
|
| 639 |
-
room_data.append((
|
| 640 |
-
str(room_df["Class Room"].iloc[i]).strip(),
|
| 641 |
-
int(room_df["Class Capacity"].iloc[i]),
|
| 642 |
-
"Class Room"
|
| 643 |
-
))
|
| 644 |
-
|
| 645 |
-
# Computer Lab
|
| 646 |
-
if pd.notna(room_df["Computer Lab"].iloc[i]) and pd.notna(room_df["Computer Capacity"].iloc[i]):
|
| 647 |
-
room_data.append((
|
| 648 |
-
str(room_df["Computer Lab"].iloc[i]).strip(),
|
| 649 |
-
int(room_df["Computer Capacity"].iloc[i]),
|
| 650 |
-
"Computer Lab"
|
| 651 |
-
))
|
| 652 |
-
|
| 653 |
-
# Seminar Halls
|
| 654 |
-
if pd.notna(room_df["Seminar Halls"].iloc[i]) and pd.notna(room_df["Seminar Capacity"].iloc[i]):
|
| 655 |
-
room_data.append((
|
| 656 |
-
str(room_df["Seminar Halls"].iloc[i]).strip(),
|
| 657 |
-
int(room_df["Seminar Capacity"].iloc[i]),
|
| 658 |
-
"Seminar Halls"
|
| 659 |
-
))
|
| 660 |
-
|
| 661 |
-
# --- Initialize Central Timetable ---
|
| 662 |
-
timetable = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
|
| 663 |
-
|
| 664 |
-
# Step 1: Generate lectures for 2ndβ5th year
|
| 665 |
-
first_year_lectures = generate_timetable(course_df, room_df, output_dir, room_slots, timetable)
|
| 666 |
-
|
| 667 |
-
# Step 2: Schedule BS-MS 1st year labs and tutorials
|
| 668 |
-
tutorial_groups, lab_groups, unscheduled_1st = schedule_bs_ms_first_year(
|
| 669 |
-
course_df,
|
| 670 |
-
room_df,
|
| 671 |
-
room_slots,
|
| 672 |
-
time_slots,
|
| 673 |
-
days,
|
| 674 |
-
output_1st_year_dir,
|
| 675 |
-
first_year_lectures_by_day_slot=first_year_lectures,
|
| 676 |
-
timetable=timetable
|
| 677 |
-
)
|
| 678 |
-
|
| 679 |
-
# Step 3: Generate all reports
|
| 680 |
-
generate_available_halls_report(room_slots)
|
| 681 |
-
generate_central_timetable_excel(timetable)
|
| 682 |
-
|
| 683 |
-
# β
NEW: Generate unused halls report
|
| 684 |
-
unused_hall_path = "Unused_Halls_Report.xlsx"
|
| 685 |
-
generate_unused_halls_report(room_slots, output_path=unused_hall_path)
|
| 686 |
-
|
| 687 |
-
# Step 4: Zip Timetables for download
|
| 688 |
-
zip_path_main = "Final_Timetable.zip"
|
| 689 |
-
with zipfile.ZipFile(zip_path_main, 'w') as zipf:
|
| 690 |
-
for foldername, _, filenames in os.walk(output_dir):
|
| 691 |
-
for filename in filenames:
|
| 692 |
-
filepath = os.path.join(foldername, filename)
|
| 693 |
-
arcname = os.path.relpath(filepath, output_dir)
|
| 694 |
-
zipf.write(filepath, arcname)
|
| 695 |
-
|
| 696 |
-
zip_path_1st = "1st_Year_Labs_Tutorials.zip"
|
| 697 |
-
with zipfile.ZipFile(zip_path_1st, 'w') as zipf:
|
| 698 |
-
for foldername, _, filenames in os.walk(output_1st_year_dir):
|
| 699 |
-
for filename in filenames:
|
| 700 |
-
filepath = os.path.join(foldername, filename)
|
| 701 |
-
arcname = os.path.relpath(filepath, output_1st_year_dir)
|
| 702 |
-
zipf.write(filepath, arcname)
|
| 703 |
-
|
| 704 |
-
# Step 5: Return all generated files
|
| 705 |
-
return (
|
| 706 |
-
zip_path_main,
|
| 707 |
-
zip_path_1st,
|
| 708 |
-
("Unscheduled_LTP_Report.xlsx" if os.path.exists("Unscheduled_LTP_Report.xlsx") else None),
|
| 709 |
-
("Available_Halls.xlsx" if os.path.exists("Available_Halls.xlsx") else None),
|
| 710 |
-
("Central_Timetable.xlsx" if os.path.exists("Central_Timetable.xlsx") else None),
|
| 711 |
-
(unused_hall_path if os.path.exists(unused_hall_path) else None)
|
| 712 |
-
)
|
| 713 |
|
| 714 |
|
| 715 |
|
|
|
|
| 596 |
|
| 597 |
# --- Final Working Process Function ---
|
| 598 |
def process(course_file, room_file):
|
| 599 |
+
import traceback
|
| 600 |
import shutil
|
| 601 |
import zipfile
|
| 602 |
from collections import defaultdict
|
| 603 |
import pandas as pd
|
| 604 |
import os
|
| 605 |
|
| 606 |
+
try:
|
| 607 |
+
print("β
Starting process...")
|
|
|
|
| 608 |
|
| 609 |
+
# --- Read Excel Files ---
|
| 610 |
+
print("π Reading course and room files...")
|
| 611 |
+
course_df = pd.read_excel(course_file.name)
|
| 612 |
+
room_df = pd.read_excel(room_file.name)
|
|
|
|
|
|
|
| 613 |
|
| 614 |
+
# β
Rename columns for clarity
|
| 615 |
+
room_df.columns = [
|
| 616 |
+
"Class Room", "Class Capacity",
|
| 617 |
+
"Computer Lab", "Computer Capacity",
|
| 618 |
+
"Seminar Halls", "Seminar Capacity"
|
| 619 |
+
]
|
| 620 |
|
| 621 |
+
# --- Create output directories ---
|
| 622 |
+
output_dir = "generated_routines"
|
| 623 |
+
output_1st_year_dir = "generated_1st_year_LT"
|
|
|
|
| 624 |
|
| 625 |
+
for path in [output_dir, output_1st_year_dir]:
|
| 626 |
+
if os.path.exists(path):
|
| 627 |
+
shutil.rmtree(path)
|
| 628 |
+
os.makedirs(path)
|
| 629 |
|
| 630 |
+
# --- Initialize room availability map ---
|
| 631 |
+
room_slots = defaultdict(lambda: defaultdict(lambda: True))
|
| 632 |
+
|
| 633 |
+
for day in days:
|
| 634 |
+
for room in room_slots:
|
| 635 |
+
for slot in ["2-3", "3-4", "4-5"]:
|
| 636 |
+
room_slots[room][f"{day}-{slot}"] = False
|
| 637 |
+
|
| 638 |
+
# --- Initialize Central Timetable ---
|
| 639 |
+
timetable = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
|
| 640 |
+
|
| 641 |
+
# β
Step 1: Generate lectures for 2ndβ5th year
|
| 642 |
+
print("π Generating timetable for 2ndβ5th years...")
|
| 643 |
+
first_year_lectures = generate_timetable(course_df, room_df, output_dir, room_slots, timetable)
|
| 644 |
+
|
| 645 |
+
# β
Step 2: Schedule BS-MS 1st year labs and tutorials
|
| 646 |
+
print("π¨βπ¬ Scheduling 1st year labs/tutorials...")
|
| 647 |
+
tutorial_groups, lab_groups, unscheduled_1st = schedule_bs_ms_first_year(
|
| 648 |
+
course_df,
|
| 649 |
+
room_df,
|
| 650 |
+
room_slots,
|
| 651 |
+
time_slots,
|
| 652 |
+
days,
|
| 653 |
+
output_1st_year_dir,
|
| 654 |
+
first_year_lectures_by_day_slot=first_year_lectures,
|
| 655 |
+
timetable=timetable
|
| 656 |
+
)
|
| 657 |
+
|
| 658 |
+
# β
Step 3: Reports
|
| 659 |
+
print("π Generating reports...")
|
| 660 |
+
generate_available_halls_report(room_slots)
|
| 661 |
+
generate_central_timetable_excel(timetable)
|
| 662 |
+
unused_hall_path = "Unused_Halls_Report.xlsx"
|
| 663 |
+
generate_unused_halls_report(room_slots, output_path=unused_hall_path)
|
| 664 |
+
|
| 665 |
+
# β
Step 4: Zipping folders
|
| 666 |
+
print("ποΈ Zipping timetables...")
|
| 667 |
+
zip_path_main = "Final_Timetable.zip"
|
| 668 |
+
with zipfile.ZipFile(zip_path_main, 'w') as zipf:
|
| 669 |
+
for foldername, _, filenames in os.walk(output_dir):
|
| 670 |
+
for filename in filenames:
|
| 671 |
+
filepath = os.path.join(foldername, filename)
|
| 672 |
+
arcname = os.path.relpath(filepath, output_dir)
|
| 673 |
+
zipf.write(filepath, arcname)
|
| 674 |
+
|
| 675 |
+
zip_path_1st = "1st_Year_Labs_Tutorials.zip"
|
| 676 |
+
with zipfile.ZipFile(zip_path_1st, 'w') as zipf:
|
| 677 |
+
for foldername, _, filenames in os.walk(output_1st_year_dir):
|
| 678 |
+
for filename in filenames:
|
| 679 |
+
filepath = os.path.join(foldername, filename)
|
| 680 |
+
arcname = os.path.relpath(filepath, output_1st_year_dir)
|
| 681 |
+
zipf.write(filepath, arcname)
|
| 682 |
+
|
| 683 |
+
print("β
All done successfully.")
|
| 684 |
+
|
| 685 |
+
return (
|
| 686 |
+
zip_path_main,
|
| 687 |
+
zip_path_1st,
|
| 688 |
+
"Unscheduled_LTP_Report.xlsx" if os.path.exists("Unscheduled_LTP_Report.xlsx") else None,
|
| 689 |
+
"Available_Halls.xlsx" if os.path.exists("Available_Halls.xlsx") else None,
|
| 690 |
+
"Central_Timetable.xlsx" if os.path.exists("Central_Timetable.xlsx") else None,
|
| 691 |
+
unused_hall_path if os.path.exists(unused_hall_path) else None,
|
| 692 |
+
""
|
| 693 |
+
)
|
| 694 |
+
|
| 695 |
+
except Exception as e:
|
| 696 |
+
err_msg = f"π¨ Error: {str(e)}\n\n" + traceback.format_exc()
|
| 697 |
+
print(err_msg)
|
| 698 |
+
return None, None, None, None, None, None, err_msg
|
| 699 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 700 |
|
| 701 |
|
| 702 |
|