我正在寻找在 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作为单个应用程序运行?因为最终,我将拥有正在读取和写入并刷新图表的数据。
最重要的是 - 我可能会以完全错误的方式去做。希望我只是在这里忽略了一个基本步骤。感谢您的关注。