Reputation: 322
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)
But everytime I get the error can't find '__main__' module in ''
I know that initially, to create a Dash a main guard like the following is used:
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. For reference, this is my MainWindow main guard:
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
Upvotes: 4
Views: 8002
Reputation: 244132
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_())
Upvotes: 5