Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from notha99y/timetable_selection: direct link, hf CLI and curl.
- Browser
- Download file 5.93 kB
-
https://huggingface.co/spaces/notha99y/timetable_selection/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/notha99y/timetable_selection/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/notha99y/timetable_selection/resolve/main/src/streamlit_app.py
5.93 kB
| import io | |
| import pandas as pd | |
| import streamlit as st | |
| from search import get_solution | |
| from utils import refine_time_parsing, get_day | |
| def main(): | |
| st.title("Curtin Timetable Picker") | |
| # st.write("Developed with ❤️ by Ren Jie") | |
| st.write( | |
| "Paste values in the format: Module, Class, Lecture, Tutorial (tab-separated)" | |
| ) | |
| st.write( | |
| "Lecture and Tutorial Format is day, start_time , end_time. e.g. Mon 4.30pm to 6pm" | |
| ) | |
| num_combi = st.number_input("Number of Combination", min_value=0, value=4) | |
| # Toggle to restrict back-to-back classes (end == start on same day) | |
| restrict_back_to_back = st.checkbox("Restrict back to back", value=False) | |
| # Initialize session state for the DataFrame | |
| if "df" not in st.session_state: | |
| st.session_state.df = pd.DataFrame( | |
| columns=["Module", "Class", "Lecture", "Tutorial"] | |
| ) | |
| # Text area for pasting tabular data | |
| pasted_data = st.text_area("Paste data here (tab-separated):", height=200) | |
| # Convert pasted data to DataFrame | |
| if pasted_data: | |
| rows = [line.split("\t") for line in pasted_data.strip().split("\n")] | |
| st.session_state.df = pd.DataFrame( | |
| rows, columns=["Module", "Class", "Lecture", "Tutorial"] | |
| ) | |
| # Display the editable table | |
| edited_df = st.data_editor( | |
| st.session_state.df, num_rows="dynamic", use_container_width=True | |
| ) | |
| # Save changes | |
| st.session_state.df = edited_df | |
| # Button to run the solution | |
| if st.button("Run"): | |
| result_df = get_solution(edited_df, num_combi, restrict_back_to_back) | |
| if result_df is not None: | |
| st.session_state.solution_df = ( | |
| result_df # Store solution in session state | |
| ) | |
| # Show result if available | |
| st.write("Generated Schedule:") | |
| st.dataframe(result_df) | |
| # Visualization per combination | |
| vis_df = result_df.copy() | |
| # populate start/end and day columns for lecture and tutorial | |
| vis_df = refine_time_parsing(vis_df, "Lecture") | |
| vis_df = refine_time_parsing(vis_df, "Tutorial") | |
| vis_df["lecture_day"] = vis_df["Lecture"].apply(get_day) | |
| vis_df["tutorial_day"] = vis_df["Tutorial"].apply(get_day) | |
| def minutes(t): | |
| return t.hour * 60 + t.minute | |
| days_order = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] | |
| for combi in sorted(vis_df["Combi"].unique()): | |
| st.markdown(f"### Combination {combi}") | |
| sub = vis_df[vis_df["Combi"] == combi] | |
| # determine range | |
| starts = [] | |
| ends = [] | |
| for _, r in sub.iterrows(): | |
| if r.get("Lecture_Start") is not None: | |
| starts.append(r.get("Lecture_Start")) | |
| ends.append(r.get("Lecture_End")) | |
| if r.get("Tutorial_Start") is not None: | |
| starts.append(r.get("Tutorial_Start")) | |
| ends.append(r.get("Tutorial_End")) | |
| if not starts or not ends: | |
| st.write("No time data to visualize for this combination.") | |
| continue | |
| min_min = min(minutes(t) for t in starts) | |
| max_min = max(minutes(t) for t in ends) | |
| # round to nearest 30 | |
| min_min = (min_min // 30) * 30 | |
| max_min = ((max_min + 29) // 30) * 30 | |
| slots = [min_min + 30 * i for i in range((max_min - min_min) // 30)] | |
| slot_labels = [] | |
| for m in slots: | |
| hh = m // 60 | |
| mm = m % 60 | |
| slot_labels.append(f"{hh:02d}:{mm:02d}") | |
| grid = {d: ["" for _ in slots] for d in days_order} | |
| # fill grid | |
| for _, r in sub.iterrows(): | |
| # lecture | |
| if r.get("lecture_day") in days_order and r.get("Lecture_Start") is not None: | |
| d = r.get("lecture_day") | |
| s = minutes(r.get("Lecture_Start")) | |
| e = minutes(r.get("Lecture_End")) | |
| for i, m in enumerate(slots): | |
| if m >= s and m < e: | |
| grid[d][i] = f"{r.get('Module')} ({r.get('Class')}) L" | |
| # tutorial | |
| if r.get("tutorial_day") in days_order and r.get("Tutorial_Start") is not None: | |
| d = r.get("tutorial_day") | |
| s = minutes(r.get("Tutorial_Start")) | |
| e = minutes(r.get("Tutorial_End")) | |
| for i, m in enumerate(slots): | |
| if m >= s and m < e: | |
| existing = grid[d][i] | |
| add = f"{r.get('Module')} ({r.get('Class')}) T" | |
| grid[d][i] = existing + " | " + add if existing else add | |
| import pandas as _pd | |
| df_grid = _pd.DataFrame(grid, index=slot_labels) | |
| # only show weekdays that actually appear | |
| used_days = [d for d in days_order if df_grid[d].astype(bool).any()] | |
| if not used_days: | |
| st.write("No scheduled days for this combination.") | |
| else: | |
| st.dataframe(df_grid[used_days]) | |
| else: | |
| st.info("Schedule cannot be found", icon="ℹ️") | |
| # Download button | |
| if "solution_df" in st.session_state: | |
| csv_buffer = io.StringIO() | |
| st.session_state.solution_df.to_csv(csv_buffer, index=False) | |
| st.download_button( | |
| "Download Solution", | |
| csv_buffer.getvalue(), | |
| "table_data.csv", | |
| "text/csv", | |
| ) | |
| if __name__ == "__main__": | |
| main() | |