63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
import os
|
|
from pathlib import Path
|
|
import streamlit as st
|
|
from toolbox.tasks import ROOT, KINDS, LABELS, list_tasks
|
|
|
|
MAX_INLINE_DOWNLOAD = 128 * 1024**2
|
|
|
|
|
|
def _file_rows(directory):
|
|
if not directory.exists():
|
|
return []
|
|
return sorted(
|
|
(path for path in directory.iterdir() if path.is_file() and not path.name.startswith(".")),
|
|
key=lambda path: path.name.lower(),
|
|
)
|
|
|
|
|
|
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 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]
|
|
cols = st.columns([4, 1])
|
|
cols[0].caption(str(directory))
|
|
if os.name == "nt" and cols[1].button("打开目录", use_container_width=True):
|
|
os.startfile(directory)
|
|
|
|
files = _file_rows(directory)
|
|
if not files:
|
|
st.info("暂无文件。")
|
|
return
|
|
|
|
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}")
|