feat: 正式成果并入任务清单并限制成果页列表高度

This commit is contained in:
nixevol
2026-09-16 17:27:33 +08:00
parent 28f3eb877a
commit 6fdc613bac
4 changed files with 60 additions and 43 deletions
+43 -31
View File
@@ -1,9 +1,10 @@
import os
from pathlib import Path
import streamlit as st
from toolbox.tasks import ROOT, KINDS, LABELS, list_tasks
from toolbox.tasks import KINDS, LABELS, list_tasks
MAX_INLINE_DOWNLOAD = 128 * 1024**2
LIST_HEIGHT = 420
def _file_rows(directory):
@@ -15,30 +16,20 @@ def _file_rows(directory):
)
def _download_button(path, key):
size = path.stat().st_size
if size <= MAX_INLINE_DOWNLOAD:
st.download_button(
"下载",
data=path.read_bytes(),
file_name=path.name,
mime="application/octet-stream",
key=key,
)
else:
st.caption("文件较大,请打开目录后复制")
def _task_label(task):
return f"{task['id']} · {KINDS.get(task['kind'], task['kind'])} · {LABELS.get(task['status'], task['status'])}"
def render():
st.markdown("## 成果浏览")
tasks = list_tasks()
roots = {"现有正式成果": ROOT / "output"}
roots.update({
f"{task['id']} · {KINDS[task['kind']]} · {LABELS[task['status']]}": Path(task["output"])
for task in tasks
})
selected = st.selectbox("成果清单", list(roots))
directory = roots[selected]
if not tasks:
st.info("暂无成果。完成出图或查询后会显示在这里。")
return
labels = {_task_label(task): Path(task["output"]) for task in tasks}
selected = st.selectbox("成果清单", list(labels))
directory = labels[selected]
cols = st.columns([4, 1])
cols[0].caption(str(directory))
if os.name == "nt" and cols[1].button("打开目录", use_container_width=True):
@@ -46,17 +37,38 @@ def render():
files = _file_rows(directory)
if not files:
st.info("暂无文件。")
st.info("该任务暂无文件。")
return
download_key = f"pending_download::{selected}"
pending_name = st.session_state.get(download_key)
st.markdown("#### 文件列表")
header = st.columns([5, 2, 2])
header[0].markdown("**文件**")
header[1].markdown("**大小**")
header[2].markdown("**操作**")
for path in files:
row = st.columns([5, 2, 2])
row[0].write(path.name)
row[1].write(f"{path.stat().st_size / 1024**2:,.2f} MB")
with row[2]:
_download_button(path, key=f"download_{selected}_{path.name}")
with st.container(height=LIST_HEIGHT, border=True):
for path in files:
row = st.columns([6, 2, 2])
row[0].markdown(f"`{path.name}`")
row[1].write(f"{path.stat().st_size / 1024**2:,.2f} MB")
if row[2].button("下载", key=f"pick_{selected}_{path.name}", use_container_width=True):
st.session_state[download_key] = path.name
pending_name = path.name
if pending_name:
target = directory / pending_name
if not target.is_file():
st.session_state.pop(download_key, None)
st.warning("文件已不存在,请重新选择。")
return
size = target.stat().st_size
st.markdown(f"已选择:**{pending_name}**({size / 1024**2:,.2f} MB)")
if size <= MAX_INLINE_DOWNLOAD:
st.download_button(
"保存到本地",
data=target.read_bytes(),
file_name=target.name,
mime="application/octet-stream",
key=f"save_{selected}_{pending_name}",
type="primary",
)
else:
st.info("文件较大,请点击上方“打开目录”后直接复制。")