(python plotly 5.7.0) 当没有互联网时它不显示图表,它仍然需要互联网才能显示图表

(python plotly 5.7.0) It doesn't display graph when ther's NO INTERNET, it still required internet to show graphs

我正在使用 python 3.6.8 和 plotly 5.7.0

[user1@testsrv1 ~]$ python3 --version
Python 3.6.8
[user1@testsrv1 ~]$ pip3 freeze | grep -i plotly
plotly==5.7.0
[user1@testsrv1 ~]$ pip3 show plotly
Name: plotly
Version: 5.7.0
Summary: An open-source, interactive data visualization library for Python
Home-page: https://plotly.com/python/
Author: Chris P
Author-email: chris@plot.ly
License: MIT
Location: /home/opc/.local/lib/python3.6/site-packages
Requires: tenacity, six

我希望在没有互联网连接的服务器上显示绘图,但它没有显示。根据 plotly 文档,从 plotly 4 开始,图表仅离线 -- https://medium.com/plotly/plotly-py-4-0-is-here-offline-only-express-first-displayable-anywhere-fc444e5659ee! 我正在使用以下代码片段:

import pandas as pd
import plotly.express as px
graph1 = px.histogram(dataframe, x='TIME_BUCKET', color='CATEGORY',  title='Category Histogram')
graph2 = px.bar(dataframe, x="CATEGORY", y="COUNT", title="Category wise execution count")
                with open("report.html", 'w') as f:
                    f.write(graph1.to_html(full_html=False, include_plotlyjs='cdn'))
                    f.write(graph2.to_html(full_html=False, include_plotlyjs='cdn'))

现在,当我打开 report.html(从没有互联网连接的 VM)时,它不显示任何图表。

但是当我像下面那样强制使用 plotly offline 时 html 显示图表。

plotly.offline.plot(graph1, filename=outhtml_path, auto_open=False)

我期待正常的函数(不是 pyhon.offline.plot)在没有任何互联网连接的情况下在 VM 中显示图表。

我得到了解决方案。在下面的代码中 f.write(graph1.to_html(full_html=假, include_plotlyjs='cdn')) f.write(graph2.to_html(full_html=假, include_plotlyjs='cdn')) 将 include_plotlyjs='cdn' 更改为 include_plotlyjs=True 并解决了问题。

f.write(graph1.to_html(full_html=False, include_plotlyjs=True))
f.write(graph2.to_html(full_html=False, include_plotlyjs=True))

根据文档 (https://plotly.github.io/plotly.py-docs/generated/plotly.html),

include_plotlyjs (bool or string (default True)) – 指定 plotly.js 库如何在输出 div 字符串中 included/loaded。 如果为真,则包含 plotly.js 源代码 (~3MB) 的脚本标记将包含在输出中。 HTML 使用此选项生成的文件是完整的 self-contained 并且可以离线使用。 如果是“cdn”,则引用 plotly.js CDN 的脚本标记将包含在输出中。 HTML 使用此选项生成的文件比使用 include_plotlyjs=True 生成的文件小约 3MB,但它们需要有效的互联网连接才能加载 plotly.js 库。