0%

使用QWebEngineView装载JupyterLab

使用生成jupyter notebook --generate-config配置jupyterlab的配置文件,修改配置文件code /Users/xx/.jupyter/jupyter_lab_config.py如下三项去掉jupyterlab的token认证和自动打开浏览器。

1
2
3
4
5
6
7
8
# c.ExtensionApp.open_browser = False
c.ExtensionApp.open_browser = False

# c.ServerApp.password_required = False
c.ServerApp.password_required = False

# c.ServerApp.token = '<generated>'
c.ServerApp.token = ''

配置QWebEngineView的无边框浏览器

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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/7/9 13:41
# @Author : 稻草人
# @contact : aidabloc@163.com
# @File : simple_browser.py
# @Desc :

import sys

from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile
from PyQt5.QtWidgets import QApplication, QMainWindow


class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.browser = QWebEngineView()

# set this browser as central widget or main window
self.setCentralWidget(self.browser)

# 设置窗口属性
self.setWindowTitle('Data Viewer')

# 强制禁止使用缓存
# https://stackoverflow.com/questions/34705849/clear-cookies-in-qtwebengine
self.browser.page().profile().setPersistentCookiesPolicy(
QWebEngineProfile.PersistentCookiesPolicy.NoPersistentCookies)

self.browser.load(QUrl("http://localhost:8888/lab"))
self.show()


def run_browser():
app = QApplication(sys.argv)
win = Window()
win.resize(1400, 900)
win.setFixedSize(1400, 900)
app.exit(app.exec_())
return 1


if __name__ == "__main__":
run_browser()

完整的启动器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import sys
from os import sep, getcwd, chdir
from subprocess import Popen

from simple_browser import run_browser

if __name__ == "__main__":
task_list = []
path = getcwd() + sep + r"notebook"
chdir(path)
task = Popen(r"jupyter lab", shell=True)
task_list.append(task)

browser = run_browser()
if browser:
task.terminate()
task.kill()
sys.exit()

最终效果,没有多个title标题栏清爽多了 jupyter client