6 unmodified lines789101112110 unmodified lines1231241251261271283 unmodified lines1321331341351361371381391401411421431444 unmodified lines1491501511521531549 unmodified lines16416516616716816917014 unmodified lines18518618718818919019119219319419519619781 unmodified lines2792802812822832842852862872882892902 unmodified lines29329429529629729877 unmodified lines376377378379380381382383384385386387129 unmodified lines5175185195205215225232 unmodified lines52652752852953053153291 unmodified lines6246256266276 unmodified linesfrom typing import Dict, List, Optional, Tuple, Anyimport jsonimport os# ============== 全局状态 ==============110 unmodified lines)def load_chart_data(source: str, chart_type: str, chart_id: str, model: str):"""加载图表数据并返回所有 UI 更新3 unmodified lines"""if not all([source, chart_type, chart_id, model]):return ["", # html_display"", # label_info"[]", # qa_data (JSON string)"等待加载数据...", # status_text"请在左侧选择图表", # progress_text"{}", # current_qa_reviews (JSON string)gr.Radio(choices=[], value=None) # qa_selector]# 更新状态4 unmodified lineshtml_content = chart_data.get('html_content', '')label_info = chart_data.get('label_info', {})# 格式化标签信息if label_info:label_text = f"""9 unmodified lines| **链接** | [查看原图]({label_info.get('Weblink', '#')}) |"""else:label_text = "暂无标签信息"# 获取 QA 列表qa_list = data_manager.get_qa_list(source, chart_type, model, chart_id)14 unmodified linesqa_choices = [f"Q{i+1}: {qa.question[:50]}..." for i, qa in enumerate(qa_list)] if qa_list else []return [html_content, # html_displaylabel_text, # label_infojson.dumps([{"id": qa.id, "question": qa.question, "answer": qa.answer} for qa in qa_list]), # qa_data (JSON string)status_text, # status_textprogress_text, # progress_textjson.dumps(existing_reviews), # current_qa_reviews (JSON string)gr.Radio(choices=qa_choices, value=qa_choices[0] if qa_choices else None) # qa_selector]81 unmodified lines# 自定义 CSScustom_css = """.chart-container {height: 500px;overflow: auto;border: 1px solid #e0e0e0;border-radius: 8px;padding: 10px;background: #fafafa;}.control-panel {2 unmodified linesborder-radius: 8px;margin-bottom: 10px;}"""with gr.Blocks(77 unmodified linesvalue="default",interactive=True)# ===== 中间:图表展示 =====with gr.Column(scale=2, min_width=400):gr.Markdown("### 📈 图表展示")# HTML 图表展示html_display = gr.HTML(value="<div style='text-align:center;padding:50px;color:#999;'>请选择图表</div>",elem_classes=["chart-container"]129 unmodified linesinputs=[source_dropdown, chart_type_dropdown, chart_dropdown, model_dropdown],outputs=[html_display, label_display, qa_data_json, status_text, progress_text,current_reviews_json, qa_selector])2 unmodified linesinputs=[source_dropdown, chart_type_dropdown, chart_dropdown, model_dropdown],outputs=[html_display, label_display, qa_data_json, status_text, progress_text,current_reviews_json, qa_selector])91 unmodified linesserver_name="0.0.0.0",server_port=7860,share=False)6 unmodified lines78910111213110 unmodified lines1241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621633 unmodified lines1671681691701711721731741751761771781791804 unmodified lines1851861871881891901911921931941951969 unmodified lines20620720820921021121214 unmodified lines22722822923023123223323423523623723823924081 unmodified lines3223233243253263273282 unmodified lines33133233333433533633733833934034134234334434577 unmodified lines423424425426427428429430431432433434435436437438439440441442443129 unmodified lines5735745755765775785792 unmodified lines58258358458558658758891 unmodified lines6806816826836 unmodified linesfrom typing import Dict, List, Optional, Tuple, Anyimport jsonimport osimport base64# ============== 全局状态 ==============110 unmodified lines)def create_embedded_html(html_content: str, chart_id: str = "") -> str:"""创建嵌入式的 HTML 显示使用 data URI 方式嵌入 HTML 内容到 iframe 中"""if not html_content:return f"""<div style="display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:400px;color:#999;border:2px dashed #ddd;border-radius:12px;background:#fafafa;"><div style="font-size:48px;margin-bottom:16px;">📭</div><div style="font-size:18px;font-weight:500;">暂无图表内容</div><div style="font-size:14px;margin-top:8px;">图表 ID: {chart_id or '未知'}</div><div style="font-size:12px;margin-top:16px;color:#888;">请检查数据集目录中是否存在该图表的 HTML 文件</div></div>"""# 使用 base64 编码 HTML 内容,避免引号转义问题html_bytes = html_content.encode('utf-8')html_base64 = base64.b64encode(html_bytes).decode('utf-8')# 使用 data URIiframe_html = f"""<iframesrc="data:text/html;base64,{html_base64}"style="width:100%;height:500px;border:1px solid #e0e0e0;border-radius:8px;background:#fff;"sandbox="allow-scripts allow-same-origin"loading="lazy"></iframe>"""return iframe_htmldef load_chart_data(source: str, chart_type: str, chart_id: str, model: str):"""加载图表数据并返回所有 UI 更新3 unmodified lines"""if not all([source, chart_type, chart_id, model]):return [create_embedded_html(""), # html_display"### 请在左侧选择图表", # label_info"[]", # qa_data (JSON string)"等待加载数据...", # status_text"请在左侧选择图表", # progress_text"{}", # current_qa_reviews (JSON string)gr.Radio(choices=[], value=None), # qa_selector"" # debug_info]# 更新状态4 unmodified lineshtml_content = chart_data.get('html_content', '')label_info = chart_data.get('label_info', {})# 创建嵌入式 HTMLembedded_html = create_embedded_html(html_content, chart_id)# 调试信息debug_info = f"📁 {source}/{chart_type}/{chart_id} | HTML: {len(html_content)} 字符"# 格式化标签信息if label_info:label_text = f"""9 unmodified lines| **链接** | [查看原图]({label_info.get('Weblink', '#')}) |"""else:label_text = "### ⚠️ 暂无标签信息"# 获取 QA 列表qa_list = data_manager.get_qa_list(source, chart_type, model, chart_id)14 unmodified linesqa_choices = [f"Q{i+1}: {qa.question[:50]}..." for i, qa in enumerate(qa_list)] if qa_list else []return [embedded_html, # html_displaylabel_text, # label_infojson.dumps([{"id": qa.id, "question": qa.question, "answer": qa.answer} for qa in qa_list]), # qa_data (JSON string)status_text, # status_textprogress_text, # progress_textjson.dumps(existing_reviews), # current_qa_reviews (JSON string)gr.Radio(choices=qa_choices, value=qa_choices[0] if qa_choices else None), # qa_selectordebug_info # debug_info]81 unmodified lines# 自定义 CSScustom_css = """.chart-container {min-height: 520px;}.control-panel {2 unmodified linesborder-radius: 8px;margin-bottom: 10px;}.debug-panel {font-size: 12px;color: #666;padding: 8px;background: #f5f5f5;border-radius: 4px;margin-top: 10px;}"""with gr.Blocks(77 unmodified linesvalue="default",interactive=True)# 调试信息debug_info = gr.Textbox(label="调试信息",value="",interactive=False,show_label=False,elem_classes=["debug-panel"])# ===== 中间:图表展示 =====with gr.Column(scale=2, min_width=400):gr.Markdown("### 📈 图表展示")# HTML 图表展示(使用 iframe)html_display = gr.HTML(value="<div style='text-align:center;padding:50px;color:#999;'>请选择图表</div>",elem_classes=["chart-container"]129 unmodified linesinputs=[source_dropdown, chart_type_dropdown, chart_dropdown, model_dropdown],outputs=[html_display, label_display, qa_data_json, status_text, progress_text,current_reviews_json, qa_selector, debug_info])2 unmodified linesinputs=[source_dropdown, chart_type_dropdown, chart_dropdown, model_dropdown],outputs=[html_display, label_display, qa_data_json, status_text, progress_text,current_reviews_json, qa_selector, debug_info])91 unmodified linesserver_name="0.0.0.0",server_port=7860,share=False)
app.py+72-16