Python Dash 独立 HTML 文件与 Plotly 相同

问题描述 投票:0回答:3

我通常使用 Plotly 绘图并将其保存为独立的 HTML 文件,这对于与同事共享和“冻结”正在显示的数据非常方便。 是我用

制作的一个例子
plotly.offline.plot(
    plotly_plot, 
    filename = 'standalone great plot.html',
)

现在我想用一个非常简单的 Dash 应用程序做同样的事情,它根本没有服务器端要求,它只是一堆 Plotly 图形和一些文本。我该怎么做?

我想要的例子

考虑本教程中显示的第一个示例,为了方便起见,我将其复制粘贴到此处:

# -*- 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 浏览器中执行“文件 → 另存页面为”,但生成的 HTML 无法显示,因为服务器(Python
app.py
文件)未运行。

python html plotly plotly-dash
3个回答

2
投票

这个主题很旧,但我一直在寻找同样的问题来制作带有可点击破折号表的 html 文件。

对于带有静态文本的独立绘图,您可以查看 jupyter notebook。笔记本可以转换为独立的 html 文件,同时保留交互式绘图。


0
投票

您可以考虑分发完整的桌面应用程序。面对类似的问题,我最近为此创建了一个样板存储库。它可以在这里找到:https://github.com/Ivorforce/Dash-Standalone-Boilerplate

© www.soinside.com 2019 - 2024. All rights reserved.