在使用由Dash创建的仪表板(使用PyQt5创建的GUI)时,我遇到了问题。
我尝试将以下示例代码作为模块以及在代码末尾:
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时要使用如下所示的主后卫:
if __name__ == '__main__':
app.run_server(debug=True)
但是我已经为MainWindow设置了一个主后卫,所以我不知道如何使两者一起工作。供参考,这是我的MainWindow主警卫:
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
答案 0 :(得分:4)
解决方案是在另一个线程中执行Dash,在另一个线程中执行Dash时可能会遇到问题,但是使用this answer会得到以下信息:
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_())