【问题标题】:Open plotly html file from script Python/R从脚本 Python/R 打开 plotly html 文件
【发布时间】:2020-11-05 19:06:10
【问题描述】:

我有一个存储在 html 文件 file.html 中的绘图图。这是在另一个脚本中创建的,例如:

import plotly.graph_objects as go
fig = go.Figure(data=go.Bar(y=[2, 3, 1]))
fig.write_html('file.html', auto_open=True)

有没有办法直接打开html 文件而不重写情节代码? 比如:

fig = go.read_html('file.html')

我需要变量内的绘图。例如,这必须工作:

fig = read html file file.html
fig.write_html('copyOfFile.html', auto_open=True)

我同时使用 Python 和 R,所以我想要两者/其中之一的解决方案。

【问题讨论】:

    标签: python r plotly r-plotly plotly-python


    【解决方案1】:

    您可以使用 python 标准库 webbrowser。它使用默认浏览器打开文档。

    import webbrowser
    webbrowser.open('file.html')
    

    【讨论】:

    • 对不起,我没有好好解释。我需要一个变量中的情节。 webbrowser 从外部打开图形(或者我可以将它存储在变量中吗?我认为它返回一个布尔值)。
    • 把它存入一个变量后你想做什么。为什么不按原样使用fig
    • 因为我没有用于生成绘图的代码。我只有输出文件 file.html。
    • 恐怕你做不到。