欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Jupyter Notebook远程登录配置

程序员文章站 2022-05-28 23:46:22
...

方法如下:

1. 生成配置文件

jupyter notebook --generate-config

2. 设置密码、获取秘钥

from notebook.auth import passwd  
passwd()
[out]
Enter password: 
Verify password: 
‘sha1:ce23d945972f:34769685a7ccd3d08c84a18c63968a41f1140274
  • 复制代码最后产生的秘钥

3. 修改默认配置文件

vim ~/.jupyter/jupyter_notebook_config.py
  • 修改如下4处:
  • 行号分别对应:203,264,273,284
c.NotebookApp.ip=’*’ 
c.NotebookApp.password = 'sha:ce…刚才复制的那个密文'
c.NotebookApp.open_browser = False  
c.NotebookApp.port =8888 #随便指定一个端口

自动化脚本

python版本 >= 3.6

from notebook.auth import passwd
import os

home_path = os.path.expanduser('~')
cfg_path = f"{home_path}/.jupyter/jupyter_notebook_config.py"
if not os.path.exists(cfg_path):
    os.system("jupyter notebook --generate-config")
key = passwd()
port = input("port(default:8888): ") or "8888"
with open(cfg_path, "r") as f:
    data = f.readlines()
modify = {
    "c.NotebookApp.ip": "c.NotebookApp.ip= '*'\n",
    "c.NotebookApp.password": f"c.NotebookApp.password = '{key}'\n",
    "c.NotebookApp.open_browser": "c.NotebookApp.open_browser = False\n",
    "c.NotebookApp.port": f"c.NotebookApp.port = {port}\n"
}
for i, j in enumerate(data):
    for k in list(modify.keys()):
        if k in j:
            data[i] = modify[k]
            modify.pop(k)
with open(cfg_path, "w") as f:
    f.writelines(data)
相关标签: Jupyter