Use Dash in PyQt5 GUI

I have a script for which I have created a GUI with PyQT5. As of now, I am able to plot my data with plot.ly, with the snippet of the code at the end of my script that follows:

    data = [trace1, trace1_dots, trace1_reliability,trace2, trace2_dots, trace2_reliability]

    layout = {
'xaxis': {
    'range': [lower_limit,upper_limit]
},

'shapes': [

    # Line Horizontal
    {
        'type': 'line',
        'x0': 0,
        'y0': 1_current_rank,
        'x1': upper_limit,
        'y1': 1_current_rank,
        'opacity': 0.5,
        'line': {
            'color': 'rgb(0,179,134)',
            'width': 4,
            'dash': 'dash',
        },
    },
    # Line Diagonal
    {
        'type': 'line',
        'x0': 0,
        'y0': 2_current_rank,
        'x1': upper_limit,
        'y1': 2_current_rank,
        'opacity': 0.5,
        'line': {
            'color': 'rgb(255,191,0)',
            'width': 4,
            'dash': 'dash',
        }
    }
]
}

    plotly.offline.plot({
        "data": data,
        "layout": layout


    }, auto_open=True)




if __name__ == '__main__':

app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())

However, I would like to use Dash instead but cannot wrap my head around how it could be done, mainly because ‘app’ is already used by PyQt5 and when I try to use ‘app1’ for example, it says it is not defined when running the script.

I am new to Dash and tried to look around but couldn’t find a solution to my problem.

hi, have you solved you problem, I encouter similar question, any experience share?

Hi,

It might not be the best solution but what I have done is using subprocess.Popen to open a file Dash.py where I am creating the Dash and pass the variables I have created so far.

Something like:

subprocess.Popen([“python”, path_to_Dash.py, variable1, variable2, variable3], creationflags=CREATE_NEW_CONSOLE)

Maybe this might help…

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

Example with callbacks

import functools
import os
import threading

from PySide6 import QtCore, QtWidgets

import dash
import dash_html_components as html
from dash.dependencies import Input, Output


class MainWindow(QtWidgets.QMainWindow):
    closed = QtCore.Signal()

    def closeEvent(self, event):
        self.closed.emit()
        super().closeEvent(event)


class Manager(QtCore.QObject):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._view = None

    @property
    def view(self):
        return self._view

    def init_gui(self):
        #self._view = QtWidgets.QMainWindow()
        self._view = MainWindow()

    @QtCore.Slot()
    def show_popup(self):
        if self.view is not None:
            self.view.show()


qt_manager = Manager()

app = dash.Dash()

app.layout = html.Div(
    children=[
        html.H1(children="Hello Dash"),
        html.Button("show pop up", id="button"),
        html.H2(children="", id="result"),
    ]
)


@app.callback(
    Output(component_id="result", component_property="children"),
    [Input(component_id="button", component_property="n_clicks")],
)
def popUp(n_clicks):
    if not n_clicks:
        dash.no_update

    loop = QtCore.QEventLoop()
    qt_manager.view.closed.connect(loop.quit)
    QtCore.QMetaObject.invokeMethod(
        qt_manager, "show_popup", QtCore.Qt.QueuedConnection
    )
    loop.exec()

    return "You saw a pop-up"


def main():
    qt_app = QtWidgets.QApplication.instance()
    if qt_app is None:
        qt_app = QtWidgets.QApplication([os.getcwd()])
    qt_app.setQuitOnLastWindowClosed(False)
    qt_manager.init_gui()
    threading.Thread(
        target=app.run_server, kwargs=dict(debug=False), daemon=True,
    ).start()

    return qt_app.exec()


if __name__ == "__main__":
    main()