Update app.py
Browse files
app.py
CHANGED
|
@@ -421,9 +421,9 @@ def create_demo():
|
|
| 421 |
current_score = 0.0
|
| 422 |
|
| 423 |
empty_md = "*等待开始...*"
|
| 424 |
-
|
| 425 |
# 初始状态
|
| 426 |
-
yield "状态: 准备执行任务...",
|
| 427 |
|
| 428 |
for i in range(1, int(max_iter) + 1):
|
| 429 |
# 执行完整迭代
|
|
@@ -491,33 +491,54 @@ def create_demo():
|
|
| 491 |
# 更新数据
|
| 492 |
chart_data.append({"iteration": i, "score": round(current_score, 2)})
|
| 493 |
|
| 494 |
-
# 生成 HTML
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
# 每次迭代完成后更新 UI
|
| 519 |
status = f"状态: 第 {i}/{int(max_iter)} 次迭代完成 (分数: {current_score:.2f})"
|
| 520 |
-
yield status,
|
| 521 |
|
| 522 |
# 检查是否达到目标
|
| 523 |
if current_score >= target:
|
|
@@ -526,7 +547,7 @@ def create_demo():
|
|
| 526 |
time.sleep(0.3)
|
| 527 |
|
| 528 |
final_status = f"状态: 任务完成\n最终分数: {current_score:.2f}\n总迭代次数: {len(chart_data)}"
|
| 529 |
-
yield final_status,
|
| 530 |
|
| 531 |
run_btn.click(
|
| 532 |
fn=run_task,
|
|
|
|
| 421 |
current_score = 0.0
|
| 422 |
|
| 423 |
empty_md = "*等待开始...*"
|
| 424 |
+
empty_svg = '<svg width="400" height="250"><text x="200" y="130" text-anchor="middle" fill="#999">等待开始...</text></svg>'
|
| 425 |
# 初始状态
|
| 426 |
+
yield "状态: 准备执行任务...", empty_svg, empty_md, empty_md, empty_md, empty_md
|
| 427 |
|
| 428 |
for i in range(1, int(max_iter) + 1):
|
| 429 |
# 执行完整迭代
|
|
|
|
| 491 |
# 更新数据
|
| 492 |
chart_data.append({"iteration": i, "score": round(current_score, 2)})
|
| 493 |
|
| 494 |
+
# 生成 HTML 折线图 - SVG 实现
|
| 495 |
+
if len(chart_data) == 1:
|
| 496 |
+
# 只有一个点,画一个点
|
| 497 |
+
svg = f'''
|
| 498 |
+
<svg width="400" height="250" style="border:1px solid #ccc; background:white;">
|
| 499 |
+
<text x="200" y="130" text-anchor="middle" fill="#666">分数: {chart_data[0]["score"]:.2f}</text>
|
| 500 |
+
<circle cx="50" cy="{200 - chart_data[0]["score"]*180}" r="8" fill="#22c55e"/>
|
| 501 |
+
</svg>
|
| 502 |
+
'''
|
| 503 |
+
else:
|
| 504 |
+
# 多个点,画折线
|
| 505 |
+
width = 400
|
| 506 |
+
height = 250
|
| 507 |
+
padding = 40
|
| 508 |
+
plot_width = width - padding * 2
|
| 509 |
+
plot_height = height - padding * 2
|
| 510 |
+
|
| 511 |
+
# 生成点和线的 SVG
|
| 512 |
+
points_svg = ""
|
| 513 |
+
lines_svg = ""
|
| 514 |
+
for idx, item in enumerate(chart_data):
|
| 515 |
+
x = padding + idx * (plot_width / (len(chart_data) - 1))
|
| 516 |
+
y = padding + plot_height - item["score"] * plot_height
|
| 517 |
+
points_svg += f'<circle cx="{x}" cy="{y}" r="6" fill="#22c55e" stroke="white" stroke-width="2"/>'
|
| 518 |
+
points_svg += f'<text x="{x}" y="{y-15}" text-anchor="middle" font-size="12" fill="#333">{item["score"]:.2f}</text>'
|
| 519 |
+
if idx > 0:
|
| 520 |
+
prev_x = padding + (idx - 1) * (plot_width / (len(chart_data) - 1))
|
| 521 |
+
prev_y = padding + plot_height - chart_data[idx-1]["score"] * plot_height
|
| 522 |
+
lines_svg += f'<line x1="{prev_x}" y1="{prev_y}" x2="{x}" y2="{y}" stroke="#22c55e" stroke-width="3"/>'
|
| 523 |
+
|
| 524 |
+
# 添加坐标轴
|
| 525 |
+
svg = f'''
|
| 526 |
+
<svg width="{width}" height="{height}" style="border:1px solid #ccc; background:white; border-radius:8px;">
|
| 527 |
+
<!-- Y轴标签 -->
|
| 528 |
+
<text x="15" y="50" font-size="12" fill="#666">1.0</text>
|
| 529 |
+
<text x="15" y="{padding + plot_height/2}" font-size="12" fill="#666">0.5</text>
|
| 530 |
+
<text x="15" y="{height-20}" font-size="12" fill="#666">0.0</text>
|
| 531 |
+
<!-- X轴标签 -->
|
| 532 |
+
<text x="{width/2}" y="{height-5}" font-size="12" fill="#666" text-anchor="middle">迭代次数</text>
|
| 533 |
+
<!-- 折线 -->
|
| 534 |
+
{lines_svg}
|
| 535 |
+
{points_svg}
|
| 536 |
+
</svg>
|
| 537 |
+
'''
|
| 538 |
|
| 539 |
# 每次迭代完成后更新 UI
|
| 540 |
status = f"状态: 第 {i}/{int(max_iter)} 次迭代完成 (分数: {current_score:.2f})"
|
| 541 |
+
yield status, svg, plan_md, exec1_md, exec2_md, summary_md
|
| 542 |
|
| 543 |
# 检查是否达到目标
|
| 544 |
if current_score >= target:
|
|
|
|
| 547 |
time.sleep(0.3)
|
| 548 |
|
| 549 |
final_status = f"状态: 任务完成\n最终分数: {current_score:.2f}\n总迭代次数: {len(chart_data)}"
|
| 550 |
+
yield final_status, svg, plan_md, exec1_md, exec2_md, summary_md
|
| 551 |
|
| 552 |
run_btn.click(
|
| 553 |
fn=run_task,
|