首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带离线绘图图表的cefpython3

带离线绘图图表的cefpython3
EN

Stack Overflow用户
提问于 2021-10-19 21:59:46
回答 1查看 106关注 0票数 1

我正在寻找一个在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

代码语言:javascript
复制
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()

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

代码语言:javascript
复制
[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的本地主机页面。但是使用这种设置,它只能运行其中的一个-但不能在一个脚本中同时运行这两个脚本。

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

最重要的是--我可能用完全错误的方式来做这件事。希望我只是忽略了这里的一个基本步骤。谢谢你的关注。

EN

回答 1

Stack Overflow用户

发布于 2021-10-27 21:37:42

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

代码语言:javascript
复制
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()

这似乎对我很有效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69638083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档