Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,7 +21,6 @@ def load_tasks_from_file():
|
|
| 21 |
try:
|
| 22 |
with open(TASKS_FILE, "r", encoding="utf-8") as f:
|
| 23 |
data = json.load(f)
|
| 24 |
-
# 基本校验
|
| 25 |
for k in ["easy", "medium", "hard"]:
|
| 26 |
if k not in data or not isinstance(data[k], list):
|
| 27 |
raise ValueError("JSON 结构不正确,应包含 easy/medium/hard 三个数组。")
|
|
@@ -38,7 +37,7 @@ def init_state():
|
|
| 38 |
"current": None,
|
| 39 |
"current_level": None,
|
| 40 |
"tasks": tasks,
|
| 41 |
-
"load_msg": msg #
|
| 42 |
}
|
| 43 |
|
| 44 |
# ====== 业务逻辑 ======
|
|
@@ -86,7 +85,6 @@ def reset_state():
|
|
| 86 |
def reload_tasks(state):
|
| 87 |
tasks, msg = load_tasks_from_file()
|
| 88 |
state["tasks"] = tasks
|
| 89 |
-
# 若当前难度没题,将 current 清空,避免脏状态
|
| 90 |
state["current"], state["current_level"] = None, None
|
| 91 |
tip = msg if msg else "已从 tasks.json 重新加载题库。"
|
| 92 |
return state, tip
|
|
@@ -101,7 +99,6 @@ def export_progress(state):
|
|
| 101 |
note_part = f" 反思:{note}" if note else ""
|
| 102 |
lines.append(f"{i}. [{ts}] {LEVEL_LABELS[lvl]}任务:{task}{note_part}")
|
| 103 |
content = "\n".join(lines) if len(lines) > 3 else "暂无记录"
|
| 104 |
-
# 使用内存文件导出(避免路径权限问题)
|
| 105 |
return gr.File.update(value=("progress.txt", content))
|
| 106 |
|
| 107 |
# ====== 界面样式 ======
|
|
@@ -147,7 +144,6 @@ with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
|
|
| 147 |
current_task = gr.Markdown("(点击 🎲 抽取任务)", elem_classes=["taskbox"])
|
| 148 |
progress = gr.Markdown("进度:0%\n░░░░░░░░░░░░░░░░░░░░░░")
|
| 149 |
tip = gr.Markdown("", elem_classes=["tip"]) # 用于显示加载/刷新提示
|
| 150 |
-
|
| 151 |
file_out = gr.File(label="下载导出记录", visible=True)
|
| 152 |
|
| 153 |
# ====== 事件处理 ======
|
|
@@ -158,15 +154,19 @@ with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
|
|
| 158 |
|
| 159 |
def on_complete(st, note):
|
| 160 |
st, msg, prog = complete_task(st, note)
|
| 161 |
-
return st, msg, prog, ""
|
|
|
|
| 162 |
def on_skip(st):
|
| 163 |
st, msg, prog = skip_task(st)
|
| 164 |
return st, msg, prog, ""
|
|
|
|
| 165 |
def on_export(st):
|
| 166 |
return export_progress(st)
|
|
|
|
| 167 |
def on_reset():
|
| 168 |
st, msg, prog, info = reset_state()
|
| 169 |
-
return st, "(点击 🎲 抽取任务)", prog,
|
|
|
|
| 170 |
def on_reload(st):
|
| 171 |
st, info = reload_tasks(st)
|
| 172 |
return st, info
|
|
@@ -176,14 +176,13 @@ with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
|
|
| 176 |
btn_complete.click(on_complete, inputs=[state, reflect], outputs=[state, current_task, progress, tip, reflect])
|
| 177 |
btn_skip.click(on_skip, inputs=[state], outputs=[state, current_task, progress, tip])
|
| 178 |
btn_export.click(on_export, inputs=[state], outputs=[file_out])
|
| 179 |
-
btn_reset.click(on_reset, outputs=[state, current_task, progress, tip, file_out
|
| 180 |
btn_reload.click(on_reload, inputs=[state], outputs=[state, tip])
|
| 181 |
|
| 182 |
-
#
|
| 183 |
def show_first_tip(st):
|
| 184 |
return st.get("load_msg") or ""
|
| 185 |
-
|
| 186 |
|
| 187 |
if __name__ == "__main__":
|
| 188 |
-
# 在 HF Spaces 上建议关闭 SSR 提示;share 是否公开交给 Space 设置
|
| 189 |
demo.launch(ssr_mode=False)
|
|
|
|
| 21 |
try:
|
| 22 |
with open(TASKS_FILE, "r", encoding="utf-8") as f:
|
| 23 |
data = json.load(f)
|
|
|
|
| 24 |
for k in ["easy", "medium", "hard"]:
|
| 25 |
if k not in data or not isinstance(data[k], list):
|
| 26 |
raise ValueError("JSON 结构不正确,应包含 easy/medium/hard 三个数组。")
|
|
|
|
| 37 |
"current": None,
|
| 38 |
"current_level": None,
|
| 39 |
"tasks": tasks,
|
| 40 |
+
"load_msg": msg # 首次加载提示
|
| 41 |
}
|
| 42 |
|
| 43 |
# ====== 业务逻辑 ======
|
|
|
|
| 85 |
def reload_tasks(state):
|
| 86 |
tasks, msg = load_tasks_from_file()
|
| 87 |
state["tasks"] = tasks
|
|
|
|
| 88 |
state["current"], state["current_level"] = None, None
|
| 89 |
tip = msg if msg else "已从 tasks.json 重新加载题库。"
|
| 90 |
return state, tip
|
|
|
|
| 99 |
note_part = f" 反思:{note}" if note else ""
|
| 100 |
lines.append(f"{i}. [{ts}] {LEVEL_LABELS[lvl]}任务:{task}{note_part}")
|
| 101 |
content = "\n".join(lines) if len(lines) > 3 else "暂无记录"
|
|
|
|
| 102 |
return gr.File.update(value=("progress.txt", content))
|
| 103 |
|
| 104 |
# ====== 界面样式 ======
|
|
|
|
| 144 |
current_task = gr.Markdown("(点击 🎲 抽取任务)", elem_classes=["taskbox"])
|
| 145 |
progress = gr.Markdown("进度:0%\n░░░░░░░░░░░░░░░░░░░░░░")
|
| 146 |
tip = gr.Markdown("", elem_classes=["tip"]) # 用于显示加载/刷新提示
|
|
|
|
| 147 |
file_out = gr.File(label="下载导出记录", visible=True)
|
| 148 |
|
| 149 |
# ====== 事件处理 ======
|
|
|
|
| 154 |
|
| 155 |
def on_complete(st, note):
|
| 156 |
st, msg, prog = complete_task(st, note)
|
| 157 |
+
return st, msg, prog, "", "" # 清空 tip 与反思框
|
| 158 |
+
|
| 159 |
def on_skip(st):
|
| 160 |
st, msg, prog = skip_task(st)
|
| 161 |
return st, msg, prog, ""
|
| 162 |
+
|
| 163 |
def on_export(st):
|
| 164 |
return export_progress(st)
|
| 165 |
+
|
| 166 |
def on_reset():
|
| 167 |
st, msg, prog, info = reset_state()
|
| 168 |
+
return st, "(点击 🎲 抽取任务)", prog, info, None
|
| 169 |
+
|
| 170 |
def on_reload(st):
|
| 171 |
st, info = reload_tasks(st)
|
| 172 |
return st, info
|
|
|
|
| 176 |
btn_complete.click(on_complete, inputs=[state, reflect], outputs=[state, current_task, progress, tip, reflect])
|
| 177 |
btn_skip.click(on_skip, inputs=[state], outputs=[state, current_task, progress, tip])
|
| 178 |
btn_export.click(on_export, inputs=[state], outputs=[file_out])
|
| 179 |
+
btn_reset.click(on_reset, outputs=[state, current_task, progress, tip, file_out])
|
| 180 |
btn_reload.click(on_reload, inputs=[state], outputs=[state, tip])
|
| 181 |
|
| 182 |
+
# ⚠️ 修正点:用 demo.load,而不是 tip.load
|
| 183 |
def show_first_tip(st):
|
| 184 |
return st.get("load_msg") or ""
|
| 185 |
+
demo.load(show_first_tip, inputs=[state], outputs=[tip])
|
| 186 |
|
| 187 |
if __name__ == "__main__":
|
|
|
|
| 188 |
demo.launch(ssr_mode=False)
|