rafaym commited on
Commit
63c3a31
·
verified ·
1 Parent(s): b6a151a

ux: clear template comments on first keystroke via change handler

Browse files
Files changed (1) hide show
  1. app.py +26 -2
app.py CHANGED
@@ -414,9 +414,33 @@ def build_app() -> gr.Blocks:
414
  # ---- State ----
415
  tier_idx_state = gr.State(value=0)
416
  lang_state = gr.State(value="zh")
 
417
 
418
  # ---- Event wiring ----
419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
  def tier_to_idx(tier_label: str) -> int:
421
  return TIER_LABELS.index(tier_label)
422
 
@@ -480,8 +504,8 @@ def build_app() -> gr.Blocks:
480
  )
481
 
482
  clear_btn.click(
483
- fn=lambda: ("", "", ""),
484
- outputs=[code_editor, stdin_input, run_output],
485
  )
486
 
487
  submit_btn.click(
 
414
  # ---- State ----
415
  tier_idx_state = gr.State(value=0)
416
  lang_state = gr.State(value="zh")
417
+ template_cleared = gr.State(value=False)
418
 
419
  # ---- Event wiring ----
420
 
421
+ _TEMPLATE_LINES = {
422
+ "# Write your solution here using Legesher",
423
+ "# Use native-language variable and function names",
424
+ }
425
+
426
+ def on_code_change(code: str, cleared: bool):
427
+ """Strip template comment lines on the user's very first edit, then never again."""
428
+ if cleared:
429
+ return gr.update(), True
430
+ lines = code.split("\n")
431
+ filtered = [l for l in lines if l.strip() not in _TEMPLATE_LINES]
432
+ # Remove leading blank lines left behind
433
+ while filtered and not filtered[0].strip():
434
+ filtered.pop(0)
435
+ cleaned = "\n".join(filtered)
436
+ return cleaned, True
437
+
438
+ code_editor.change(
439
+ fn=on_code_change,
440
+ inputs=[code_editor, template_cleared],
441
+ outputs=[code_editor, template_cleared],
442
+ )
443
+
444
  def tier_to_idx(tier_label: str) -> int:
445
  return TIER_LABELS.index(tier_label)
446
 
 
504
  )
505
 
506
  clear_btn.click(
507
+ fn=lambda: ("# Write your solution here using Legesher\n# Use native-language variable and function names\n", "", "", False),
508
+ outputs=[code_editor, stdin_input, run_output, template_cleared],
509
  )
510
 
511
  submit_btn.click(