【问题标题】:cefpython3 with an offline plotly chart带有离线绘图图表的cefpython3
【发布时间】:2021-10-19 21:59:45
【问题描述】:

我正在寻找在 cefpython3 中嵌入离线绘图图表的示例。据我了解,cefpython3 是为这样的项目而构建的。但是,我没有成功识别出使用 plotly 的示例——这对我是 python 新手很有帮助。对于它的价值,我会提供我尝试过的:

我下面的代码演示了尝试将来自 https://dash.plotly.com/layout 的示例绘图图表与来自 cefpython 的 hello_world.py 示例相结合,可在此处找到:https://github.com/cztomczak/cefpython/blob/master/examples/hello_world.py

dashtest.py

from cefpython3 import cefpython as cef
import platform
import sys
#dash (plotly)---------------------
import dash
from dash import dcc
from dash import html
import plotly.express as px
import pandas as pd
#---------------------------------

def main():
    check_versions()
    sys.excepthook = cef.ExceptHook  # To shutdown all CEF processes on error
    cef.Initialize()
    cef.CreateBrowserSync(url="http://127.0.0.1:8050/",
                          window_title="Test")
    DashApp()
    cef.MessageLoop()
    cef.Shutdown()

def check_versions():
    ver = cef.GetVersion()
    print("[dashtest.py] CEF Python {ver}".format(ver=ver["version"]))
    print("[dashtest.py] Chromium {ver}".format(ver=ver["chrome_version"]))
    print("[dashtest.py] CEF {ver}".format(ver=ver["cef_version"]))
    print("[dashtest.py] Python {ver} {arch}".format(
           ver=platform.python_version(),
           arch=platform.architecture()[0]))
    assert cef.__version__ >= "57.0", "CEF Python v57.0+ required to run this"


def DashApp():
    app = dash.Dash()

    # 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 your data.
        '''),

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

    app.run_server(debug=False)

if __name__ == '__main__':
    main()

这是我的命令提示符调试输出:

[dashtest.py] CEF Python 66.1
[dashtest.py] Chromium 66.0.3359.181
[dashtest.py] CEF 3.3359.1774.gd49d25f
[dashtest.py] Python 3.9.6 64bit

DevTools listening on ws://127.0.0.1:55905/devtools/browser/3e64411d-3a5b-4493-be7b-c12e8498e125
Dash is running on http://127.0.0.1:8050/

 * Serving Flask app 'dashtest' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)

通过调试,我发现如果我先在单独的CMD窗口中运行plotly脚本,我可以让cefpython3加载plotly的localhost页面。但是使用这种设置,它只会运行其中一个 - 但不能同时在一个脚本中运行。

如何启动 plotly 服务器;并将cefpython作为单个应用程序运行?因为最终,我将拥有正在读取和写入并刷新图表的数据。

最重要的是 - 我可能会以完全错误的方式处理这件事。希望我只是在这里忽略了一个基本步骤。感谢观看。

【问题讨论】:

    标签: python plotly plotly-dash chromium-embedded cefpython


    【解决方案1】:

    您需要在一个线程中运行 CEF 窗口:

    import threading
    
    def cef_in_parallel():
        cef.Initialize()
        cef.CreateBrowserSync(url="http://127.0.0.1:8050/",
                              window_title="Test")
        cef.MessageLoop()
    
    
    def main():
        cef_thread = theading.Thread(target=cef_in_parallel)
        cef_thread.start()
        DashApp()
        cef.Shutdown()
    
    

    这似乎对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 2015-07-02
      相关资源
      最近更新 更多