gpt4 book ai didi

Python Dash 独立 HTML 文件与 Plotly 相同

转载 作者:行者123 更新时间:2023-12-04 13:30:07 25 4
gpt4 key购买 nike

我通常用 Plotly 绘图并将它们保存为独立的 HTML 文件,这对于与同事共享它们以及“卡住”正在显示的数据非常方便。 This是我用的一个例子

plotly.offline.plot(
plotly_plot,
filename = 'standalone great plot.html',
)
现在我想用一个完全没有服务器端要求的非常简单的 Dash 应用程序做同样的事情,它只是一堆 Plotly 数字和一些文本。我怎样才能做到这一点?
我想要的例子
考虑 this tutorial 中显示的第一个示例,为了方便起见,我将其复制粘贴到此处:
# -*- coding: utf-8 -*-

# Run this app with `python app.py` and
# visit http://127.0.0.1:8050/ in your web browser.

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

# assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
df = pd.DataFrame({
"Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
"Amount": [4, 1, 2, 2, 4, 5],
"City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
})

fig = px.bar(df, x="Fruit", y="Amount", color="City", barmode="group")

app.layout = html.Div(children=[
html.H1(children='Hello Dash'),

html.Div(children='''
Dash: A web application framework for Python.
'''),

dcc.Graph(
id='example-graph',
figure=fig
)
])

if __name__ == '__main__':
app.run_server(debug=True)
当我用 Python 运行它然后打开 http://127.0.0.1:8050/在我的浏览器中,我看到了这个:
enter image description here
因此可以看出,这基本上是一个带有一些文本的 Plotly 图形。所以它 必须能够以某种方式生成独立的 HTML 文件 有了这个内容。
我想复制 plotly.offline.plot 的行为有了这个。我该怎么做?我曾尝试在 Web 浏览器中执行“文件 → 另存页面为”,但由于服务器(Python app.py 文件)未运行,因此无法显示生成的 HTML。

最佳答案

关于Python Dash 独立 HTML 文件与 Plotly 相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65561675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com