在 PyQt5 GUI 中使用 Dash (plot.ly) [英] Use Dash (plot.ly) in PyQt5 GUI
问题描述
我在尝试使用 Dash(绘图)创建仪表板时遇到问题,同时使用 PyQt5 创建的 GUI.
I am running into a problem while trying to create a dashboard with Dash (plotly), while using a GUI created with PyQt5.
我尝试将以下示例代码作为模块和代码末尾:
I have tried to have the following example code both as a module and at the end of my code:
import dash
import dash_core_components as dcc
import dash_html_components as html
def run_dash(data, layout):
app = dash.Dash()
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={
'data': data,
'layout': layout
}
)
])
app.run_server(debug=True)
但是每次我收到错误 can't find '__main__' module in ''
我知道最初要创建一个 Dash,使用如下所示的主要守卫:
I know that initially, to create a Dash a main guard like the following is used:
if __name__ == '__main__':
app.run_server(debug=True)
但是我的 MainWindow 已经有了一个主守卫,所以我不知道如何让两者一起工作.作为参考,这是我的 MainWindow 主守卫:
But I already have a main guard for my MainWindow so I can't figure out how to make both work together. For reference, this is my MainWindow main guard:
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
推荐答案
解决方案是在另一个线程中执行 Dash,在另一个线程中执行时可能会出现问题,但使用 这个答案 你会得到以下内容:
The solution is to execute Dash in another thread, when executing it in another thread you could have problems, but using this answer you get the following:
import sys
import threading
from PyQt5 import QtWidgets
import dash
import dash_core_components as dcc
import dash_html_components as html
def run_dash(data, layout):
app = dash.Dash()
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={
'data': data,
'layout': layout
})
])
app.run_server(debug=False)
class MainWindow(QtWidgets.QMainWindow):
pass
if __name__ == '__main__':
data = [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
]
layout = {
'title': 'Dash Data Visualization'
}
threading.Thread(target=run_dash, args=(data, layout), daemon=True).start()
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
这篇关于在 PyQt5 GUI 中使用 Dash (plot.ly)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!