我使用plotly (express)生成了很多图像,并将它们保存为png格式的本地目录。现在,我想创建一个带有plotly的仪表板。我生成的图像有很多依赖项,这就是为什么我不想在dash应用程序的代码中包含代码的原因。
现在我问,是否可以将图像保存为某种格式(HTML?)在我的本地目录中,然后用划线来调用它们?!
我的问题是,我必须如何保存图像,以及如何调用它?我不想使用PNG (等)。因为我想使用hoverfunction
这是我尝试过的:
import plotly.express as px
fig =px.scatter(x=range(10), y=range(10))
fig.write_html("../example_codes/saved_as_HTML.html")
#%%
import dash
import dash_html_components as html
import base64
app = dash.Dash()
image_filename = 'saved_as_HTML.html' # replace with your own image
encoded_image = base64.b64encode(open(image_filename, 'rb').read())
# app.layout = html.Div([
# html.Img(src='data:image/png;base64,{}'.format(encoded_image))
# ])
app.layout = html.Div([
html.Img(src='data:image/html;base64,{}'.format(encoded_image))
])
if __name__ == '__main__':
app.run_server(debug=True)发布于 2021-05-13 13:35:03
我会以不同的方式来处理这个问题。
我不使用html作为格式,而是使用joblib保存和加载Python图形,因为这些图形只是常规的Python对象。
# Save the figures somewhere
import joblib
fig = px.scatter(x=range(10), y=range(10))
joblib.dump(fig, "../example_codes/fig.pkl")将图形保存到某个位置后,可以使用joblib加载它,并使用Graph在您的仪表盘布局中使用它
fig = joblib.load("../example_codes/fig.pkl")
app = dash.Dash()
app.layout = html.Div([dcc.Graph(figure=fig)])
if __name__ == "__main__":
app.run_server(debug=True)https://stackoverflow.com/questions/67502511
复制相似问题