-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash_qt_demo.py
165 lines (142 loc) · 4.54 KB
/
dash_qt_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import http.client
import os
import sys
import threading
import time
import dash
import numpy as np
from dash import dcc, html
from PyQt6.QtCore import QUrl
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtWidgets import QApplication, QFileDialog, QMainWindow
from dash_tooltip import tooltip
dash_port = 8050
class MyApp(QMainWindow):
"""
Main application window for the PyQt application.
This class integrates a Dash application inside a PyQt WebEngineView.
"""
def __init__(self):
"""
Initialize the application window.
"""
super().__init__()
self.browser = None
self.initUI()
def initUI(self):
"""
Set up the user interface of the app.
"""
self.browser = QWebEngineView(self)
self.browser.page().profile().downloadRequested.connect(self._on_download_fn)
self.browser.load(QUrl("http://127.0.0.1:8050/")) # Dash app default address
self.setCentralWidget(self.browser)
self.resize(800, 600) # Set a default size
self.show()
def _on_download_fn(self, download):
"""
Handle file download requests.
Args:
download: QWebEngineDownloadRequest object.
"""
fileName, _ = QFileDialog.getSaveFileName(
self,
"Save File",
"", # Starting path
"All Files (*);;PNG Files (*.png);;JPEG Files (*.jpeg)",
)
if fileName:
directory = os.path.dirname(fileName)
filename = os.path.basename(fileName)
download.setDownloadDirectory(directory)
download.setDownloadFileName(filename)
download.accept()
def create_dash_app():
"""
Create and configure the Dash application.
Returns:
A Dash app object.
"""
app = dash.Dash(__name__)
x = np.linspace(0, 10, 100)
y = np.sin(x)
y1 = np.cos(x)
app.layout = html.Div(
[
dcc.Graph(
id="example-graph",
figure={
"data": [
{
"x": x,
"y": y,
"type": "scatter",
"mode": "lines",
"name": "sin(x)",
"meta": ["some meta info"],
},
{
"x": x,
"y": y1,
"type": "scatter",
"mode": "lines",
"name": "cos(x)",
},
],
"layout": {
"title": "Dash by Plotly in PyQt",
"autosize": True, # Enable autosizing of the graph
},
},
config={
"editable": True,
"edits": {"shapePosition": True, "annotationPosition": True},
},
style={
"width": "100%",
"height": "100%",
}, # Ensure full width and height
)
],
style={
"width": "95vw",
"height": "95vh",
"overflow": "hidden",
}, # Full viewport width and height
)
template = "%{name}<br>%{meta[0]}<br>x: %{x:.2f}<br>y: %{y:.3f}"
tooltip(app, template=template)
return app
def run_dash():
"""
Run the Dash app server.
"""
app = create_dash_app()
app.run_server()
# Check server availability
def is_server_available(host="127.0.0.1", port=dash_port):
"""Check if the Dash server is available."""
conn = http.client.HTTPConnection(host, port)
try:
conn.request("GET", "/")
return conn.getresponse().status == 200
except (http.client.HTTPException, TimeoutError, ConnectionRefusedError):
return False
finally:
conn.close()
if __name__ == "__main__":
# Start Dash app in a separate process
dash_thread = threading.Thread(target=run_dash)
dash_thread.start()
# Wait for Dash server to be available
while not is_server_available(port=dash_port):
time.sleep(0.1)
# PyQt app
qt_app = QApplication(sys.argv)
window = MyApp()
window.setWindowTitle("Dash inside PyQt")
web_view = window.centralWidget()
assert isinstance(web_view, QWebEngineView)
web_view.load(QUrl(f"http://localhost:{dash_port}/"))
window.show()
sys.exit(qt_app.exec())