cgreszes commited on
Commit
153ed09
·
verified ·
1 Parent(s): 67a1cce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -16
app.py CHANGED
@@ -1012,8 +1012,7 @@ DATASET = [
1012
  {'class_id': 'C0999', 'name': 'Finance 401 — Valuation', 'professor': 'Jamie Wright', 'days': 'Thu,Fri', 'times': '9:15 AM - 10:45 AM', 'subject': 'Finance'},
1013
  {'class_id': 'C1000', 'name': 'Finance 101 — Behavioral Finance: Foundations', 'professor': 'Parker Anderson', 'days': 'Mon,Fri', 'times': '4:15 PM - 6:15 PM', 'subject': 'Finance'},
1014
  ]
1015
-
1016
- df = pd.DataFrame(DATASET)
1017
 
1018
  # ---------- CONSTANTS ----------
1019
  SUBJECTS = sorted(df["subject"].unique().tolist())
@@ -1130,8 +1129,17 @@ def pick_schedules(df_pool: pd.DataFrame, demand: Dict[str, int], max_attempts=5
1130
  def draw_timetable(schedule_rows: List[dict], title: str):
1131
  fig, ax = plt.subplots(figsize=(10, 7), dpi=150)
1132
  ax.set_xlim(0, 7); ax.set_ylim(8, 21)
1133
- ax.set_xticks(range(7)); ax.set_xticklabels(DAYS_AXIS)
1134
- ax.set_yticks(range(8, 22, 1)); ax.set_ylabel("Time"); ax.set_title(title)
 
 
 
 
 
 
 
 
 
1135
 
1136
  for x in range(8): ax.axvline(x, linewidth=0.5)
1137
  for y in range(8, 22): ax.axhline(y, linewidth=0.3)
@@ -1203,7 +1211,8 @@ def generate(tbl, instructions):
1203
  tbl = _ensure_array_table(tbl)
1204
  if not tbl:
1205
  blank = draw_timetable([], "Schedule option 1 (empty)")
1206
- return blank, "Schedule option 1", [], 0, gr.Button.update(visible=False), gr.Button.update(visible=False), gr.Button.update(visible=False), gr.update(visible=False)
 
1207
 
1208
  demand = {}
1209
  for subject, count in tbl:
@@ -1213,21 +1222,35 @@ def generate(tbl, instructions):
1213
 
1214
  if not demand:
1215
  blank = draw_timetable([], "Schedule option 1 (empty)")
1216
- return blank, "Schedule option 1", [], 0, gr.Button.update(visible=False), gr.Button.update(visible=False), gr.Button.update(visible=False), gr.update(visible=False)
1217
 
1218
  pool = filter_by_constraints(df, instructions)
1219
  scheds = pick_schedules(pool, demand)
 
 
1220
  idx = 0
1221
  title = f"Schedule option {idx+1}"
1222
- img = draw_timetable(scheds[idx], title)
1223
- return img, title, scheds, idx, gr.Button.update(visible=False), gr.Button.update(visible=True if len(scheds)>1 else False), gr.Button.update(visible=True), gr.update(visible=False)
 
 
 
 
 
 
 
1224
 
1225
  def step(direction, scheds, idx):
1226
- if not scheds: return gr.update(), "", idx, gr.Button.update(visible=False), gr.Button.update(visible=False)
1227
- idx = (idx + 1) % len(scheds) if direction=="next" else (idx - 1) % len(scheds)
 
 
 
 
1228
  title = f"Schedule option {idx+1}"
1229
  img = draw_timetable(scheds[idx], title)
1230
- return img, title, idx, gr.Button.update(visible=True), gr.Button.update(visible=True)
 
1231
 
1232
  def get_details(scheds, idx):
1233
  rows = scheds[idx] if (scheds and 0 <= idx < len(scheds)) else []
@@ -1252,8 +1275,8 @@ with gr.Blocks(css="""
1252
  subject_table = gr.Dataframe(
1253
  headers=["Subject","Count"],
1254
  datatype=["str","number"],
1255
- type="array", # returns list-of-lists
1256
- value=[], # ✅ start empty
1257
  row_count=(0,"dynamic"),
1258
  col_count=2,
1259
  interactive=True,
@@ -1297,9 +1320,7 @@ with gr.Blocks(css="""
1297
  subject_table.change(update_total, inputs=[subject_table], outputs=[total_text])
1298
 
1299
  def _gen(tbl, instr):
1300
- img, title, scheds, idx, pvis, nvis, dvis, hide_details = generate(tbl, instr)
1301
- return img, title, scheds, idx, pvis, nvis, dvis, hide_details
1302
-
1303
  generate_btn.click(
1304
  _gen,
1305
  inputs=[subject_table, custom_instructions],
 
1012
  {'class_id': 'C0999', 'name': 'Finance 401 — Valuation', 'professor': 'Jamie Wright', 'days': 'Thu,Fri', 'times': '9:15 AM - 10:45 AM', 'subject': 'Finance'},
1013
  {'class_id': 'C1000', 'name': 'Finance 101 — Behavioral Finance: Foundations', 'professor': 'Parker Anderson', 'days': 'Mon,Fri', 'times': '4:15 PM - 6:15 PM', 'subject': 'Finance'},
1014
  ]
1015
+ df = pd.DataFrame(DATASET)
 
1016
 
1017
  # ---------- CONSTANTS ----------
1018
  SUBJECTS = sorted(df["subject"].unique().tolist())
 
1129
  def draw_timetable(schedule_rows: List[dict], title: str):
1130
  fig, ax = plt.subplots(figsize=(10, 7), dpi=150)
1131
  ax.set_xlim(0, 7); ax.set_ylim(8, 21)
1132
+
1133
+ # Put days on TOP, not bottom
1134
+ ax.set_xticks(range(7))
1135
+ ax.set_xticklabels(DAYS_AXIS)
1136
+ ax.xaxis.tick_top()
1137
+ ax.xaxis.set_label_position('top')
1138
+ ax.tick_params(axis='x', which='both', bottom=False, top=True, labelbottom=False, labeltop=True)
1139
+
1140
+ ax.set_yticks(range(8, 22, 1))
1141
+ ax.set_ylabel("Time")
1142
+ ax.set_title(title, pad=20)
1143
 
1144
  for x in range(8): ax.axvline(x, linewidth=0.5)
1145
  for y in range(8, 22): ax.axhline(y, linewidth=0.3)
 
1211
  tbl = _ensure_array_table(tbl)
1212
  if not tbl:
1213
  blank = draw_timetable([], "Schedule option 1 (empty)")
1214
+ # hide nav + details when empty
1215
+ return blank, "Schedule option 1", [], 0, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
1216
 
1217
  demand = {}
1218
  for subject, count in tbl:
 
1222
 
1223
  if not demand:
1224
  blank = draw_timetable([], "Schedule option 1 (empty)")
1225
+ return blank, "Schedule option 1", [], 0, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
1226
 
1227
  pool = filter_by_constraints(df, instructions)
1228
  scheds = pick_schedules(pool, demand)
1229
+
1230
+ # Always render something; if algo couldn't fit, you'll just see a blank grid
1231
  idx = 0
1232
  title = f"Schedule option {idx+1}"
1233
+ img = draw_timetable(scheds[idx] if idx < len(scheds) else [], title)
1234
+
1235
+ # Show nav (if >1) and details button
1236
+ prev_vis = gr.update(visible=False) # first page: prev hidden
1237
+ next_vis = gr.update(visible=True if len(scheds) > 1 else False)
1238
+ details_vis = gr.update(visible=True)
1239
+ hide_details_table = gr.update(visible=False) # reset details table until clicked again
1240
+
1241
+ return img, title, scheds, idx, prev_vis, next_vis, details_vis, hide_details_table
1242
 
1243
  def step(direction, scheds, idx):
1244
+ if not scheds:
1245
+ return gr.update(), "", idx, gr.update(visible=False), gr.update(visible=False)
1246
+ if direction == "next":
1247
+ idx = (idx + 1) % len(scheds)
1248
+ else:
1249
+ idx = (idx - 1) % len(scheds)
1250
  title = f"Schedule option {idx+1}"
1251
  img = draw_timetable(scheds[idx], title)
1252
+ # once you start navigating, show both buttons
1253
+ return img, title, idx, gr.update(visible=True), gr.update(visible=True)
1254
 
1255
  def get_details(scheds, idx):
1256
  rows = scheds[idx] if (scheds and 0 <= idx < len(scheds)) else []
 
1275
  subject_table = gr.Dataframe(
1276
  headers=["Subject","Count"],
1277
  datatype=["str","number"],
1278
+ type="array", # returns list-of-lists
1279
+ value=[],
1280
  row_count=(0,"dynamic"),
1281
  col_count=2,
1282
  interactive=True,
 
1320
  subject_table.change(update_total, inputs=[subject_table], outputs=[total_text])
1321
 
1322
  def _gen(tbl, instr):
1323
+ return generate(tbl, instr)
 
 
1324
  generate_btn.click(
1325
  _gen,
1326
  inputs=[subject_table, custom_instructions],