Improve MIP optimization and add log export feature

This commit is contained in:
dmy
2026-01-08 15:08:04 +08:00
parent ebd5883dbf
commit 04a5e19451
4 changed files with 215 additions and 9 deletions

62
gui.py
View File

@@ -92,6 +92,7 @@ def index():
"info_container": None, # 新增信息展示容器
"ga_switch": None, # 遗传算法开关
"mip_switch": None, # MIP开关
"log_content": "", # 存储计算日志内容
}
def update_info_panel():
@@ -632,6 +633,63 @@ def index():
"icon=folder_zip color=secondary"
)
# --- 导出计算日志 ---
async def on_click_export_log(e):
# 尝试多种方式获取日志内容
log_content = ""
method_used = "unknown"
# 方法1: 首先尝试从保存的日志内容获取
if refs.get("log_content") and refs["log_content"].strip():
log_content = refs["log_content"]
method_used = "saved_memory"
# 方法2: 如果保存的日志为空,尝试使用 JavaScript 获取 log 组件的内容
if not log_content.strip() and refs["log_box"]:
try:
log_id = refs["log_box"].id
js_code = f"""
(function() {{
const logElement = document.querySelector("#c{log_id}");
if (logElement) {{
console.log("Found log element:", logElement);
return logElement.innerText || logElement.textContent || "";
}}
console.log("Log element not found for ID: c{log_id}");
return "";
}})()
"""
result = await ui.run_javascript(js_code)
if result and result.strip():
log_content = result
method_used = "javascript"
except Exception as js_error:
print(f"JavaScript method failed: {js_error}")
if not log_content.strip():
ui.notify(
"没有可导出的日志内容。请先运行计算任务。", type="warning"
)
print(f"Log export failed. Method tried: {method_used}")
return
print(
f"Successfully exported log using method: {method_used}, length: {len(log_content)}"
)
default_name = f"{file_prefix}_calculation_log.txt"
async def save_log(path):
with open(path, "w", encoding="utf-8") as f:
f.write(log_content)
await save_file_with_dialog(
default_name, save_log, "Text Files (*.txt)", sender=e.sender
)
ui.button("导出计算日志", on_click=on_click_export_log).props(
"icon=description color=info"
)
def update_plot(result):
if refs["plot_container"]:
refs["plot_container"].clear()
@@ -686,6 +744,8 @@ def index():
return
if refs["log_box"]:
refs["log_box"].clear()
# 重置日志内容
refs["log_content"] = ""
log_queue = queue.Queue()
# 获取开关状态
@@ -706,6 +766,8 @@ def index():
try:
msg = log_queue.get_nowait()
refs["log_box"].push(msg)
# 同时保存到日志内容中
refs["log_content"] += msg + "\n"
new_msg = True
if msg.startswith("--- Scenario"):
scenario_name = msg.replace("---", "").strip()