【发布时间】:2020-05-25 01:45:35
【问题描述】:
我正在尝试将 Dash 布局保存在 HTML 文件中,但我找不到实现此目的的方法。奇怪的是,保存单个 Plotly 图形很容易,但不是 Dash 布局。有人有解决办法吗?
在https://stackoverflow.com/a/51013594/3057377看到这个问题已经有答案了,但是没看懂。特别是关于交互性损失的说明。可以看到在保存单个绘图时保持交互性,因此对于整个布局应该是相同的。
以下是我已经尝试过的事情:
import dash_core_components as dcc
import dash_html_components as html
import dash
import plotly as py
import plotly.graph_objs as go
# Create two figures.
fig1 = go.Figure(data=go.Scatter(x=[0, 1, 2], y=[0, 10, 0]))
fig2 = go.Figure(data=go.Scatter(x=[0, 1, 2], y=[0, -10, 0]))
# Write fig1 to HTML. The three methods below work.
py.io.write_html(fig1, file="fig1_a.html", auto_open=True)
fig1.write_html(file="fig1_b.html", auto_open=True)
py.offline.plot(fig1, filename='fig1_c.html', auto_open=True)
# Write fig2 to HTML. The three methods below work.
py.io.write_html(fig2, file="fig2_a.html", auto_open=True)
fig2.write_html(file="fig2_b.html", auto_open=True)
py.offline.plot(fig2, filename='fig2_c.html', auto_open=True)
# Now create a layout that will be displayed in an HTML page.
app = dash.Dash(__name__)
app.layout = html.Div([dcc.Graph(id="fig1", figure=fig1),
dcc.Graph(id="fig2", figure=fig2)])
# Trying to save the layout to HTML doesn’t work with the same three methods as above.
print("############ 1")
try:
py.io.write_html(app.layout, file="app_layout_a.html", auto_open=True)
except Exception as e:
print(e)
print("############ 2")
try:
app.layout.write_html(file="app_layout_c.html", auto_open=True)
except Exception as e:
print(e)
print("############ 3")
try:
py.offline.plot(app.layout, filename='app_layout_b.html')
except Exception as e:
print(e)
# But the layout displays correctly when served by Dash.
app.run_server(debug=True)
【问题讨论】:
标签: python html plotly plotly-dash