wxy01giser commited on
Commit
c9ec321
·
verified ·
1 Parent(s): d4d8e80

Update sankey_plot.py

Browse files
Files changed (1) hide show
  1. sankey_plot.py +20 -4
sankey_plot.py CHANGED
@@ -46,9 +46,25 @@ def plot_sankey_from_df(sankey_df: pd.DataFrame, title="问题 → 关键词共
46
 
47
  fig.update_layout(title_text=title, font=dict(family="Microsoft YaHei", size=18), width=900, height=600,
48
  margin=dict(l=50, r=50, t=80, b=50),autosize=False ) # 关闭自动缩放
 
 
49
  # === 5. 导出高清 PNG(关键!)===
50
- img_bytes = fig.to_image( format="png", width=900, height=600, scale=2, engine="kaleido") # 2倍 DPI → 超清!
51
- # return base64.b64encode(img_bytes).decode()
52
- b64 = base64.b64encode(img_bytes).decode("utf-8")
53
- print(f"✅ 桑基图base64生成成功,长度:{len(b64)} 字符")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  return fig, b64
 
46
 
47
  fig.update_layout(title_text=title, font=dict(family="Microsoft YaHei", size=18), width=900, height=600,
48
  margin=dict(l=50, r=50, t=80, b=50),autosize=False ) # 关闭自动缩放
49
+ print(f"✅ 桑基图{fig}生成成功")
50
+
51
  # === 5. 导出高清 PNG(关键!)===
52
+ # 6. 关键:生成base64(增加异常捕获,确保生成成功)
53
+ try:
54
+ # 生成高清PNG字节流(scale=2 → 2倍DPI,避免模糊)
55
+ img_bytes = fig.to_image(
56
+ format="png",
57
+ width=900,
58
+ height=600,
59
+ scale=2,
60
+ engine="kaleido" # 显式指定引擎,避免依赖自动检测
61
+ )
62
+ # 转base64字符串
63
+ b64 = base64.b64encode(img_bytes).decode("utf-8")
64
+ print(f"✅ 桑基图base64生成成功,长度:{len(b64)} 字符") # 正常长度约10万+字符
65
+ except Exception as img_err:
66
+ # 图片生成失败时,用空图表的base64兜底
67
+ print(f"⚠️ 桑基图转base64失败:{str(img_err)}")
68
+ empty_img_bytes = fig.to_image(format="png", width=900, height=600, scale=1)
69
+ b64 = base64.b64encode(empty_img_bytes).decode("utf-8")
70
  return fig, b64