HOPStudio commited on
Commit
b9c79de
·
verified ·
1 Parent(s): c40dfca

Update start_jupyterlab.py

Browse files
Files changed (1) hide show
  1. start_jupyterlab.py +27 -5
start_jupyterlab.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import subprocess
3
  import sys
 
4
 
5
  def modify_jupyter_config():
6
  """修改 Jupyter 配置文件以允许远程访问并禁用 token"""
@@ -41,17 +42,38 @@ def modify_jupyter_config():
41
  print(f"修改配置文件时出错: {e}")
42
 
43
  def start_jupyter_lab():
44
- """启动 JupyterLab 服务"""
45
  try:
46
  print("启动 JupyterLab 服务...")
 
47
  command = [
48
  sys.executable, "-m", "jupyter", "lab",
49
  "--no-browser", # 禁止自动打开浏览器
50
  "--ip=0.0.0.0", # 允许外部访问
51
- "--port=18888", # 指定端口为 18888
52
  ]
53
- subprocess.Popen(command, stdout=sys.stdout, stderr=sys.stderr)
54
- print("JupyterLab 已启动,请通过 http://<服务器IP>:8888 访问。")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  except Exception as e:
56
  print(f"启动 JupyterLab 时出错: {e}")
57
 
@@ -60,4 +82,4 @@ if __name__ == "__main__":
60
  modify_jupyter_config()
61
 
62
  # 2. 启动 JupyterLab
63
- start_jupyter_lab()
 
1
  import os
2
  import subprocess
3
  import sys
4
+ import threading
5
 
6
  def modify_jupyter_config():
7
  """修改 Jupyter 配置文件以允许远程访问并禁用 token"""
 
42
  print(f"修改配置文件时出错: {e}")
43
 
44
  def start_jupyter_lab():
45
+ """启动 JupyterLab 服务并将输出重定向到主窗口"""
46
  try:
47
  print("启动 JupyterLab 服务...")
48
+
49
  command = [
50
  sys.executable, "-m", "jupyter", "lab",
51
  "--no-browser", # 禁止自动打开浏览器
52
  "--ip=0.0.0.0", # 允许外部访问
53
+ "--port=18888", # 指定端口
54
  ]
55
+
56
+ process = subprocess.Popen(
57
+ command,
58
+ stdout=subprocess.PIPE,
59
+ stderr=subprocess.PIPE,
60
+ text=True,
61
+ bufsize=1
62
+ )
63
+
64
+ def stream_output(pipe, pipe_name):
65
+ """实时打印子进程的输出"""
66
+ with pipe:
67
+ for line in pipe:
68
+ print(f"[{pipe_name}] {line.strip()}")
69
+
70
+ # 分别启动两个线程,读取标准输出和标准错误
71
+ threading.Thread(target=stream_output, args=(process.stdout, "STDOUT"), daemon=True).start()
72
+ threading.Thread(target=stream_output, args=(process.stderr, "STDERR"), daemon=True).start()
73
+
74
+ # 等待子进程结束
75
+ process.wait()
76
+ print("JupyterLab 服务已停止。")
77
  except Exception as e:
78
  print(f"启动 JupyterLab 时出错: {e}")
79
 
 
82
  modify_jupyter_config()
83
 
84
  # 2. 启动 JupyterLab
85
+ start_jupyter_lab()