Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - 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.

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_())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

1.4m articles

1.4m replys

5 comments

57.0k users

...