目录
  1. 方法如下
    1. 生成配置文件
    2. 设置密码、获取秘钥
    3. 修改默认配置文件
    4. 自动化脚本
Jupyter Notebook远程登录配置

方法如下

生成配置文件

1
jupyter notebook --generate-config

设置密码、获取秘钥

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

修改默认配置文件

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

自动化脚本

python 版本 >= 3.6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)
文章作者: Haibei
文章链接: http://www.haibei.online/posts/2921447308.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Haibei的博客
打赏
  • 微信
  • 支付宝

评论