Files
MarketGrid/ui/task_panel.py
T

42 lines
1.7 KiB
Python

from datetime import datetime
import streamlit as st
from toolbox.tasks import ACTIVE, KINDS, LABELS, list_tasks, stop_task, tail_log
@st.fragment(run_every="2s")
def task_panel(task_id=None, recent=False):
tasks = list_tasks()
if task_id:
tasks = [task for task in tasks if task["id"] == task_id]
elif recent:
tasks = tasks[:1]
if not tasks:
st.info("暂无任务。")
return
for task in tasks:
with st.container(border=True):
st.markdown(f"**{KINDS[task['kind']]} · {LABELS[task['status']]}**")
st.caption(f"{task['id']} · {datetime.fromtimestamp(task['created']):%Y-%m-%d %H:%M:%S}")
st.write(task["message"])
if task.get("total"):
ratio = min(task.get("done", 0) / task["total"], 1.0)
st.progress(ratio, text=f"{task.get('done', 0)} / {task['total']}")
elif task.get("done"):
st.write(f"已处理 {task['done']} 条")
if task.get("counts"):
st.write(task["counts"])
if task["status"] in ACTIVE:
if task["stop_requested"]:
st.info("正在停止,请稍候。")
elif st.button("停止任务", key=f"stop_{task['id']}"):
stop_task(task["id"])
st.rerun(scope="fragment")
st.caption(f"成果目录:{task['output']}")
with st.expander("运行日志", expanded=bool(task_id)):
st.code(tail_log(task["directory"]), language="text")
def show_started(task_id):
st.session_state["last_task"] = task_id
st.success("任务已启动,可在任务中心查看进度。")