蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

从CSV只创建独立网页

PYTHONWindows、Python3.12与本机浏览器;本站独立程序和4文件ZIP,无外部依赖或DSH/模型。build_dashboard.py
build_dashboard.py68 行
"""Owned CSV-to-HTML exercise. It does not use a model, SDK or Desktop application."""
from pathlib import Path
import argparse,csv,html
def read_tasks(path):
 with Path(path).open(encoding="utf-8-sig",newline="") as stream:
  reader=csv.DictReader(stream)
  if reader.fieldnames!=["task","minutes","done"]:raise ValueError("Expected task,minutes,done columns in that order")
  rows=[]
  for row in reader:
   if None in row or any(row[k] is None for k in reader.fieldnames):raise ValueError("Incomplete CSV record")
   title=row["task"].strip()
   if not title:raise ValueError("Empty task")
   try:minutes=int(row["minutes"])
   except ValueError:raise ValueError("Minutes must be an integer") from None
   if minutes<0:raise ValueError("Minutes must be nonnegative")
   done=row["done"].strip().lower()
   if done not in {"true","false"}:raise ValueError("Done must be true or false")
   rows.append(dict(task=title,minutes=minutes,done=done=="true"))
 return rows
def summary(rows):
 return dict(total=len(rows),done=sum(r["done"] for r in rows),minutes=sum(r["minutes"] for r in rows),
  done_minutes=sum(r["minutes"] for r in rows if r["done"]))
STYLE="""
*{box-sizing:border-box}body{margin:0;background:#edf3fa;color:#19304c;font:16px/1.6 system-ui,sans-serif}
main{width:calc(100% - 2rem);max-width:54rem;margin:2rem auto;padding:1.5rem;background:white;border-top:6px solid #1460ae;border-radius:8px}
h1{font-size:1.55rem;margin:0}p{margin:.6rem 0 1rem}output{font-weight:700;font-variant-numeric:tabular-nums}
progress{display:block;width:100%;height:1.5rem;accent-color:#1460ae}table{width:100%;table-layout:fixed;border-collapse:collapse;margin-top:1.4rem}
th,td{text-align:left;vertical-align:top;border-bottom:1px solid #d3deea;padding:.7rem .3rem;overflow-wrap:anywhere}
th:first-child{width:62%}th:nth-child(2){width:20%}th:last-child{width:18%}input{width:1.15rem;height:1.15rem}
label{cursor:pointer}small{display:block;color:#50657e;margin-top:1rem}@media(max-width:420px){main{padding:1rem;margin:1rem auto}h1{font-size:1.25rem}}
"""
SCRIPT="""
const items = Array.from(document.querySelectorAll('input[data-minutes]'));
function recalculate() {
  let completed = 0, used = 0, planned = 0;
  for (const item of items) {
    const amount = Number(item.dataset.minutes);
    planned += amount;
    if (item.checked) { completed += 1; used += amount; }
  }
  const ratio = items.length ? Math.round(100 * completed / items.length) : 0;
  document.querySelector('#completed').value = String(completed);
  document.querySelector('#all').value = String(items.length);
  document.querySelector('#ratio').value = String(ratio) + '%';
  document.querySelector('#used').value = String(used);
  document.querySelector('#planned').value = String(planned);
  document.querySelector('progress').value = ratio;
}
for (const item of items) item.addEventListener('change', recalculate);
recalculate();
"""
def render(rows):
 lines=[]
 for i,row in enumerate(rows):
  checked=" checked" if row["done"] else ""
  lines.append('<tr><td><label for="item-'+str(i)+'">'+html.escape(row["task"])+'</label></td><td>'+str(row["minutes"])+'</td><td><input id="item-'+str(i)+'" type="checkbox" data-minutes="'+str(row["minutes"])+'"'+checked+'></td></tr>')
 return '<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>蓝色天空学习任务</title><style>'+STYLE+'</style></head><body><main><h1>蓝色天空学习任务</h1><p>已完成 <output id="completed">0</output> / <output id="all">0</output> 项;按任务数计算 <output id="ratio">0%</output></p><progress aria-label="按任务数计算的完成比例" max="100" value="0"></progress><p>已完成分钟:<output id="used">0</output> / <output id="planned">0</output></p><table><thead><tr><th>任务</th><th>分钟</th><th>完成</th></tr></thead><tbody>'+''.join(lines)+'</tbody></table><small>勾选只影响本页。刷新恢复初值,不会写回CSV。</small></main><script>'+SCRIPT+'</script></body></html>'
def build(csv_path,output):
 rows=read_tasks(csv_path);page=render(rows)
 # Exclusive creation preserves every existing output, even if generation was requested again.
 with Path(output).open("x",encoding="utf8",newline="\n") as stream:stream.write(page)
 return summary(rows)
def main():
 p=argparse.ArgumentParser();p.add_argument("--csv",default="tasks.csv");p.add_argument("--output",default="index.html");args=p.parse_args()
 import json
 print(json.dumps(build(args.csv,args.output),ensure_ascii=False))
if __name__=="__main__":main()
本例需在 Windows、Python3.12与本机浏览器;本站独立程序和4文件ZIP,无外部依赖或DSH/模型。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows、Python3.12与本机浏览器;本站独立程序和4文件ZIP,无外部依赖或DSH/模型。

入口文件:projects/deepseek-harness-desktop/build_dashboard.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 build_dashboard.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows、Python3.12与本机浏览器;本站独立程序和4文件ZIP,无外部依赖或DSH/模型。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:DeepSeek Harness

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a133.zip

下载课程关联工程包:DeepSeek Harness

课程关联工程包(登记关联33章) · 含当前章节或实例 · mbskycom202612a128.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习