使用 Python 从 Plotly 获取 <script> 和 <div> 标签
Getting <script> and <div> tags from Plotly using Python
我想知道是否有人知道从HTML Plotly 离线客户端的输出。
我已经熟悉 bokeh 并且非常喜欢将它用于 2D 可视化,但真的很想集成 Plotly 以及它的 3D 可视化功能。
如果您需要有关该项目的任何额外详细信息,请告诉我。
如果您致电:
plotly.offline.plot(data, filename='file.html')
它会创建一个名为 file.html
的文件并在您的网络浏览器中打开它。但是,如果您这样做:
plotly.offline.plot(data, include_plotlyjs=False, output_type='div')
调用将 return 一个仅包含创建图表所需的 div 的字符串,您可以将其存储在您想要的任何变量中(而不是磁盘)。
我刚刚试过了,它 returned,对于我正在做的给定图表:
<div id="82072c0d-ba8d-4e86-b000-0892be065ca8" style="height: 100%; width: 100%;" class="plotly-graph-div"></div>
<script type="text/javascript">window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL="https://plot.ly";Plotly.newPlot("82072c0d-ba8d-4e86-b000-0892be065ca8",
[{"y": ..bunch of data..., "x": ..lots of data.., {"showlegend": true, "title": "the title", "xaxis": {"zeroline": true, "showline": true},
"yaxis": {"zeroline": true, "showline": true, "range": [0, 22.63852380952382]}}, {"linkText": "Export to plot.ly", "showLink": true})</script>
注意它只是 html 的一小部分,您应该将其嵌入到更大的页面中。为此,我使用标准模板引擎,如 Jinga2。
有了这个,您可以创建一个 html 页面,其中包含按您想要的方式排列的多个图表,甚至 return 它可以作为 服务器对 ajax 的响应呼唤,很可爱
更新:
请记住,您需要包含 plotly js 文件才能使所有这些图表正常工作。
您可以在放入 div 之前包含 <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
。如果将此 js 放在页面底部,图表将无法使用。
对死灵的回答表示歉意,我真的很想对 Fermin Silva 留下的评论添加评论 () - 但长期存在的潜伏者声誉阻止了我。
无论如何,我有类似的需求并遇到了 plotly 2.2.2 的问题
plotly.offline.plot(data, include_plotlyjs=False, output_type='div')
输出到 div 时忽略了 include_plotlyjs
参数。根据上面的评论,我找到了解决方法。基本上让 plotly 绘制到文件,它确实尊重 include_plotlyjs
参数。载入美汤将link注入cdn上最新的plotly.js
import plotly
import bs4
# return as html fragment
# the include_plotlyjs argument seems to be
# ignored as it's included regardless when outputting to div
# found an open issue on here - https://github.com/plotly/plotly.py/issues/1043
plotly.offline.plot(
plot_output,
filename = filename,
config = plot_config,
include_plotlyjs = False,
auto_open = False,
)
# load the file
with open(filename) as inf:
txt = inf.read()
soup = bs4.BeautifulSoup(txt)
# add in the latest plot-ly js as per
js_src = soup.new_tag("script", src="https://cdn.plot.ly/plotly-latest.min.js")
# insert it into the document
soup.head.insert(0, js_src)
# save the file again
with open(filename, "w") as outf:
outf.write(str(soup))
干杯
对于 Plotly 4,使用 plotly.io.to_html
:
import plotly
# Returns a `<div>` and `<script>`
plotly.io.to_html(figure, include_plotlyjs=False, full_html=False)
# Returns a full standalone HTML
plotly.io.to_html(figure)
参考:https://plotly.com/python-api-reference/generated/plotly.io.to_html.html
我想知道是否有人知道从HTML Plotly 离线客户端的输出。
我已经熟悉 bokeh 并且非常喜欢将它用于 2D 可视化,但真的很想集成 Plotly 以及它的 3D 可视化功能。
如果您需要有关该项目的任何额外详细信息,请告诉我。
如果您致电:
plotly.offline.plot(data, filename='file.html')
它会创建一个名为 file.html
的文件并在您的网络浏览器中打开它。但是,如果您这样做:
plotly.offline.plot(data, include_plotlyjs=False, output_type='div')
调用将 return 一个仅包含创建图表所需的 div 的字符串,您可以将其存储在您想要的任何变量中(而不是磁盘)。
我刚刚试过了,它 returned,对于我正在做的给定图表:
<div id="82072c0d-ba8d-4e86-b000-0892be065ca8" style="height: 100%; width: 100%;" class="plotly-graph-div"></div>
<script type="text/javascript">window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL="https://plot.ly";Plotly.newPlot("82072c0d-ba8d-4e86-b000-0892be065ca8",
[{"y": ..bunch of data..., "x": ..lots of data.., {"showlegend": true, "title": "the title", "xaxis": {"zeroline": true, "showline": true},
"yaxis": {"zeroline": true, "showline": true, "range": [0, 22.63852380952382]}}, {"linkText": "Export to plot.ly", "showLink": true})</script>
注意它只是 html 的一小部分,您应该将其嵌入到更大的页面中。为此,我使用标准模板引擎,如 Jinga2。
有了这个,您可以创建一个 html 页面,其中包含按您想要的方式排列的多个图表,甚至 return 它可以作为 服务器对 ajax 的响应呼唤,很可爱
更新:
请记住,您需要包含 plotly js 文件才能使所有这些图表正常工作。
您可以在放入 div 之前包含 <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
。如果将此 js 放在页面底部,图表将无法使用。
对死灵的回答表示歉意,我真的很想对 Fermin Silva 留下的评论添加评论 (
无论如何,我有类似的需求并遇到了 plotly 2.2.2 的问题
plotly.offline.plot(data, include_plotlyjs=False, output_type='div')
输出到 div 时忽略了 include_plotlyjs
参数。根据上面的评论,我找到了解决方法。基本上让 plotly 绘制到文件,它确实尊重 include_plotlyjs
参数。载入美汤将link注入cdn上最新的plotly.js
import plotly
import bs4
# return as html fragment
# the include_plotlyjs argument seems to be
# ignored as it's included regardless when outputting to div
# found an open issue on here - https://github.com/plotly/plotly.py/issues/1043
plotly.offline.plot(
plot_output,
filename = filename,
config = plot_config,
include_plotlyjs = False,
auto_open = False,
)
# load the file
with open(filename) as inf:
txt = inf.read()
soup = bs4.BeautifulSoup(txt)
# add in the latest plot-ly js as per
js_src = soup.new_tag("script", src="https://cdn.plot.ly/plotly-latest.min.js")
# insert it into the document
soup.head.insert(0, js_src)
# save the file again
with open(filename, "w") as outf:
outf.write(str(soup))
干杯
对于 Plotly 4,使用 plotly.io.to_html
:
import plotly
# Returns a `<div>` and `<script>`
plotly.io.to_html(figure, include_plotlyjs=False, full_html=False)
# Returns a full standalone HTML
plotly.io.to_html(figure)
参考:https://plotly.com/python-api-reference/generated/plotly.io.to_html.html