HOPStudio commited on
Commit
8b9d863
·
verified ·
1 Parent(s): a7b5b4e

Update start_jupyterlab.py

Browse files
Files changed (1) hide show
  1. start_jupyterlab.py +61 -89
start_jupyterlab.py CHANGED
@@ -1,89 +1,61 @@
1
- import os
2
- import subprocess
3
- import sys
4
-
5
- # 设置默认端口号
6
- DEFAULT_PORT = 8888
7
-
8
- # 检查 Python 环境
9
- def check_dependencies():
10
- print("检查 Python 和 pip...")
11
- try:
12
- subprocess.run(["python3", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
13
- subprocess.run(["pip3", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
14
- except subprocess.CalledProcessError:
15
- print("Python3 或 pip3 未正确安装,请安装后重试。")
16
- sys.exit(1)
17
-
18
- # 安装 JupyterLab
19
- def install_jupyterlab():
20
- print("检查并安装 JupyterLab...")
21
- try:
22
- subprocess.run(["pip3", "show", "jupyterlab"], check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
23
- except subprocess.CalledProcessError:
24
- print("安装 JupyterLab...")
25
- subprocess.run(["pip3", "install", "jupyterlab"], check=True)
26
- print("JupyterLab 安装成功!")
27
-
28
- # 检查端口是否被占用
29
- def check_port(port):
30
- print(f"检查端口 {port} 是否被占用...")
31
- try:
32
- result = subprocess.run(
33
- ["lsof", f"-i:{port}"],
34
- check=False,
35
- stdout=subprocess.PIPE,
36
- stderr=subprocess.PIPE,
37
- text=True
38
- )
39
- if result.stdout:
40
- print(f"端口 {port} 已被占用,请选择其他端口。")
41
- sys.exit(1)
42
- print(f"端口 {port} 可用。")
43
- except FileNotFoundError:
44
- print("lsof 命令未找到,请安装 lsof 或使用其他方法检查端口。")
45
- sys.exit(1)
46
-
47
- # JupyterLab
48
- def start_jupyterlab(port):
49
- print("启动 JupyterLab 服务...")
50
- try:
51
- command = [
52
- "jupyter-lab",
53
- "--ip=0.0.0.0", # 接受外部连接
54
- f"--port={port}", # 指定端口
55
- "--no-browser", # 禁止自动打开浏览器
56
- "--NotebookApp.token=''", # 禁用 token,方便外部访问
57
- "--NotebookApp.password=''" # 禁用密码
58
- ]
59
- process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
60
-
61
- # 检查是否启动成功
62
- for line in process.stdout:
63
- if "http://" in line or "https://" in line:
64
- print(f"JupyterLab 启动成功!访问链接:{line.strip()}")
65
- return
66
-
67
- print("JupyterLab 启动失败,请检查日志。")
68
- sys.exit(1)
69
- except Exception as e:
70
- print(f"启动 JupyterLab 时出错: {e}")
71
- sys.exit(1)
72
-
73
- # 主函数
74
- def main():
75
- port = DEFAULT_PORT
76
- if len(sys.argv) > 1:
77
- try:
78
- port = int(sys.argv[1])
79
- except ValueError:
80
- print("无效的端口号,请输入有效的整数。")
81
- sys.exit(1)
82
-
83
- check_dependencies()
84
- install_jupyterlab()
85
- check_port(port)
86
- start_jupyterlab(port)
87
-
88
- if __name__ == "__main__":
89
- main()
 
1
+ import os
2
+ import subprocess
3
+ import sys
4
+
5
+ def modify_jupyter_config():
6
+ """修改 Jupyter 配置文件以允许远程访问并禁用 token"""
7
+ config_dir = os.path.expanduser("~/.jupyter")
8
+ config_file = os.path.join(config_dir, "jupyter_notebook_config.py")
9
+
10
+ # 如果配置文件不存在,生成配置文件
11
+ if not os.path.exists(config_file):
12
+ print("生成 Jupyter 配置文件...")
13
+ try:
14
+ subprocess.check_call([sys.executable, "-m", "jupyter", "notebook", "--generate-config"])
15
+ print(f"配置文件已生成: {config_file}")
16
+ except subprocess.CalledProcessError as e:
17
+ print(f"生成配置文件时出错: {e}")
18
+ return
19
+
20
+ # 检查配置文件是否已经包含了所需设置
21
+ with open(config_file, "r") as f:
22
+ config_content = f.read()
23
+ if "c.ServerApp.allow_remote_access" in config_content:
24
+ print("Jupyter 配置文件已经包含必要的设置,无需修改。")
25
+ return
26
+
27
+ # 修改配置文件以支持远程访问
28
+ try:
29
+ print("修改 Jupyter 配置文件...")
30
+ with open(config_file, "a") as f:
31
+ f.write("\n# Allow remote access\n")
32
+ f.write("c.ServerApp.allow_remote_access = True\n")
33
+ f.write("c.ServerApp.ip = '0.0.0.0'\n")
34
+ f.write("c.ServerApp.port = 8888\n") # 设置端口
35
+ f.write("c.ServerApp.token = ''\n") # 禁用 token
36
+ f.write("c.ServerApp.allow_origin = '*'\n")
37
+ print("Jupyter 配置文件修改完成!")
38
+ except Exception as e:
39
+ print(f"修改配置文件时出错: {e}")
40
+
41
+ def start_jupyter_lab():
42
+ """启动 JupyterLab 服务"""
43
+ try:
44
+ print("启动 JupyterLab 服务...")
45
+ command = [
46
+ sys.executable, "-m", "jupyter", "lab",
47
+ "--no-browser", # 禁止自打开浏览器
48
+ "--ip=0.0.0.0", # 允许外部访问
49
+ "--port=8888", # 指定端口为 8888
50
+ ]
51
+ subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
52
+ print("JupyterLab 已启动,请通过 http://<服务器IP>:8888 访问。")
53
+ except Exception as e:
54
+ print(f"启动 JupyterLab 时出错: {e}")
55
+
56
+ if __name__ == "__main__":
57
+ # 1. 修改 Jupyter 配置
58
+ modify_jupyter_config()
59
+
60
+ # 2. 启动 JupyterLab
61
+ start_jupyter_lab()