[英]Use Dash (plot.ly) in PyQt5 GUI
I am running into a problem while trying to create a dashboard with Dash (plotly), while using a GUI created with PyQt5.我在尝试使用 Dash(绘图)创建仪表板时遇到问题,同时使用 PyQt5 创建的 GUI。
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)
But everytime I get the error can't find '__main__' module in ''
但是每次我收到错误时
can't find '__main__' module in ''
I know that initially, to create a Dash a main guard like the following is used:我知道最初,要创建一个 Dash,会使用如下所示的主要守卫:
if __name__ == '__main__':
app.run_server(debug=True)
But I already have a main guard for my MainWindow so I can't figure out how to make both work together.但是我的 MainWindow 已经有了一个主要的守卫,所以我不知道如何让两者一起工作。 For reference, this is my MainWindow main guard:
作为参考,这是我的 MainWindow 主守卫:
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
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:解决方案是在另一个线程中执行 Dash,当在另一个线程中执行它时,您可能会遇到问题,但使用此答案您会得到以下结果:
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_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.