Update start_jupyterlab.py
Browse files- 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 |
-
|
| 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 |
-
print("
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
command =
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|