-
Notifications
You must be signed in to change notification settings - Fork 0
/
jupyter_widget.py
151 lines (122 loc) · 4.55 KB
/
jupyter_widget.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
from PyQt6.QtCore import QUrl, QTimer
from PyQt6.QtWidgets import QPushButton, QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel, QVBoxLayout
from PyQt6.QtWebEngineWidgets import QWebEngineView
import subprocess
import socket
import sys
class JupyterWidget(QWidget):
def __init__(self, switch_to_main_callback=None, extra_param=None):
super().__init__()
self.browser = QWebEngineView()
# Add a startup page
self.browser.setHtml('''
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome to the Jupyter Notebook Interface</h1>
<p>Checking Jupyter Server status...</p>
</body>
</html>
''')
# Button to go back to main menu if callback provided
if switch_to_main_callback:
self.back_button = QPushButton('Back to Main Menu')
self.back_button.clicked.connect(switch_to_main_callback)
else:
self.back_button = None
# Create layout for the widget
layout = QVBoxLayout()
# Add back button if it exists
if self.back_button:
layout.addWidget(self.back_button)
# Add the web view
layout.addWidget(self.browser)
# Extra param usage example, add only if not None or empty
if extra_param:
self.extra_label = QLabel(f"Extra Param: {extra_param}")
self.extra_label.setFixedHeight(50)
layout.addWidget(self.extra_label)
self.setLayout(layout)
self.jupyter_process = None
self.check_server_and_start()
def check_server_and_start(self):
if not self.is_server_running():
self.start_jupyter()
else:
self.load_jupyter_lab()
def is_server_running(self):
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('localhost', 8888))
return result == 0
def start_jupyter(self):
import subprocess
self.jupyter_process = subprocess.Popen(['jupyter', 'lab', '--no-browser'])
QTimer.singleShot(2000, self.load_jupyter_lab) # Adjust delay as needed
def load_jupyter_lab(self):
self.browser.setUrl(QUrl("http://localhost:8888/lab"))
def stop_jupyter(self):
if self.jupyter_process:
self.jupyter_process.terminate()
self.jupyter_process = None
def closeEvent(self, event):
self.stop_jupyter() # Ensure the server is stopped
event.accept()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Jupyter Notebook in PyQt6')
self.resize(1600, 900)
# Create the QWebEngineView
self.browser = QWebEngineView()
# Add a startup page
self.browser.setHtml('''
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome to the Jupyter Notebook Interface</h1>
<p>Checking Jupyter Server status...</p>
</body>
</html>
''')
# Layout for the main window
layout = QVBoxLayout()
layout.addWidget(self.browser)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
self.jupyter_process = None
self.check_server_and_start()
def check_server_and_start(self):
if not self.is_server_running():
self.start_jupyter()
else:
self.load_jupyter_lab()
def is_server_running(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('localhost', 8888))
return result == 0
def start_jupyter(self):
self.jupyter_process = subprocess.Popen(['jupyter', 'lab', '--no-browser'])
QTimer.singleShot(2000, self.load_jupyter_lab) # Adjust delay as needed
def load_jupyter_lab(self):
self.browser.setUrl(QUrl("http://localhost:8888/lab"))
def stop_jupyter(self):
if self.jupyter_process:
self.jupyter_process.terminate()
self.jupyter_process = None
def closeEvent(self, event):
self.stop_jupyter() # Ensure the server is stopped
event.accept()
# Standalone example entry point
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())