Jupyter Notebook是一个开源的Web应用程序,允许您创建和共享包含实时代码,方程式,可视化和叙述文本的文档。用途包括:数据清理和转换,数值模拟,统计建模,数据可视化,机器学习等等。
安装Anaconda
Anaconda 是一种Python语言的免费增值开源发行版,用于进行大规模数据处理, 预测分析, 和科学计算, 致力于简化包的管理和部署。Anaconda使用软件包管理系统Conda进行包管理。
到Anaconda官网获取对应操作系统安装脚本的下载链接。
1
2
3
| wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh
chmod u+x Anaconda3-5.0.1-Linux-x86_64.sh
./Anaconda3-5.0.1-Linux-x86_64.sh
|
根据提示完成安装。使用bash可以在安装过程中选择把环境变量加入到.bashrc
文件中;使用zsh的话,需要将环境变量手动加到.zshrc
文件中。
1
| export PATH=/home/chi/anaconda3/bin:$PATH
|
更新:
1
2
| conda update --prefix /home/chi/anaconda3 anaconda
conda update --all
|
安装Jupyter
安装好anaconda后,默认情况下jupyter已经安装在主机上。检查是否安装:
如果未安装,可使用下面的安装命令安装:
配置Jupyter
生成配置文件
1
| jupyter notebook --generate-config
|
会生成配置文件~/.jupyter/jupyter_notebook_config.py
,文件中所有的配置项都是注释掉的状态。
生成密码hash
如果Jupyter服务暴露在公网上,最好为其设置密码。Jupyter的访问密码用sha1加密后存储在配置文件中。
使用下面的命令在IPython中生成密码,是类似'sha1:XXXXXXX'
的字符串。
1
2
| from notebook.auth import passwd
passwd()
|
添加配置项
编辑~/.jupyter/jupyter_notebook_config.py
文件,在末尾添加:
1
2
3
4
5
6
7
| # c.NotebookApp.certfile = '/home/ipynb/cert.pem' # 证书文件,使用Nginx时不要在这里配置
# c.NotebookApp.keyfile = '/home/ipynb/privkey.pem' # 私钥文件,使用Nginx时不要在这里配置
c.NotebookApp.ip = 'localhost' # 监听IP,使用Nginx做反向代理只需监听localhost
c.NotebookApp.port = 8888 # 监听端口
c.NotebookApp.open_browser = False # 启动Jupyter时是否自动打开浏览器
c.NotebookApp.notebook_dir = '/home/chi/notes' # 笔记根目录地址,不要设置成用户的家目录
c.NotebookApp.password = "sha1:foo:bar" # 访问密码的hash
|
使用Nginx作反向代理
使用Nginx作为反向代理不是必须的,配置HTTPS访问也不是必须的。建议在公网上部署时使用Nginx并启用HTTPS。
安装Nginx
1
2
| sudo apt update
sudo apt install nginx
|
配置Nginx
在目录 /etc/nginx/sites-enabled
增加 jupyter.conf
配置文件(需要 root 权限):
1
2
3
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
| server {
listen 80;
server_name jupyter.mrchi.cc;
proxy_buffers 64 4k;
access_log /var/log/nginx/jupyter_access.log;
error_log /var/log/nginx/jupyter_error.log;
location / {
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ /api/kernels/ {
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
location ~ /terminals/ {
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
}
|
测试配置:
重新加载配置:
生成HTTPS证书
Certbot可以在您的网站上自动部署Let’s Encrypt的HTTPS证书。
安装certbot,以在Ubuntu16.04(Xenial)系统上使用Nginx为例。官网可查看更多安装方式。
1
2
3
4
5
| sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx
|
生成证书
1
| sudo certbot --nginx certonly
|
证书有效期为90天,如果要更新证书,只要执行
配置HTTPS
修改nginx配置文件/etc/nginx/nginx.conf
,主要改动:
- 添加HTTPS 443访问;
- 301重定向HTTP访问到HTTPS;
- 启用HSTS;
1
2
3
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
45
46
47
| ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
server {
listen 443 ssl;
server_name jupyter.mrchi.cc;
proxy_buffers 64 4k;
access_log /var/log/nginx/jupyter_access.log;
error_log /var/log/nginx/jupyter_error.log;
ssl_certificate /etc/letsencrypt/live/jupyter.mrchi.cc/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/jupyter.mrchi.cc/privkey.pem;
location / {
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ /api/kernels/ {
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
location ~ /terminals/ {
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
}
server {
listen 80;
server_name jupyter.mrchi.cc;
return 301 https://$server_name$request_uri;
}
|
测试配置:
重新加载配置:
设置default路由
修改/etc/nginx/sites-available/default
文件,设置default路由返回444,让Nginx直接断开连接。
default路由应同时设置80和443端口
如果listen了443端口,需要在server块中指定证书,任意证书包括自签名证书都可以。这里使用了之前生成的Let’s Encrypt证书。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl_certificate /etc/letsencrypt/live/jupyter.mrchi.cc/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/jupyter.mrchi.cc/privkey.pem;
return 444;
}
|
使用supervisor管理Jupyter进程
Jupyter命令没有提供以服务后台运行的方式,推荐使用supervisor管理主进程。
安装supervisor
supervisor可以通过pip安装,也可以使用apt安装。
1
2
| sudo apt update
sudo apt install supervisor
|
supervisord配置
一般情况下,使用默认配置就可以了。但为了能够支持中文,需要在/etc/supervisor/supervisord.conf
配置文件的[supervisord]
配置块下增加以下配置:
1
| environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8,LC_LANG=en_US.UTF-8
|
Jupyter程序配置
在/etc/supervisor/conf.d/
目录下新建配置文件jupyter.conf
。
1
2
3
4
5
6
7
8
9
10
11
12
| [program:jupyter]
environment=SHELL=/usr/bin/zsh
command=/home/chi/anaconda3/bin/jupyter notebook
autostart=true
autorestart=true
startsecs=5
startretries=3
stopasgroup=true
killasgroup=true
user=chi
redirect_stderr=true
stdout_logfile=/home/chi/logs/jupyter.log
|
加载配置
1
2
3
| sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status
|
为Jupyter安装kernel
Jupyter默认只有IPython kernel,你可以安装其他kernel以支持不同的语言,如R语言、Julia和JavaScript等等。
这里有一份可安装的 kernel 列表。详细的安装方法见不同kernel的repo文档。
安装ijavascript内核
1
2
3
| sudo add-apt-repository ppa:chronitis/jupyter
sudo apt-get update
sudo apt-get install ijavascript
|
为Jupyter安装扩展
安装Jupyter Notebook extensions:
1
| conda install -c conda-forge jupyter_contrib_nbextensions
|
安装完成后启动Jupyter,会有一个在Nbextensions的tab,其中可以对插件进行管理。
开启toc2插件
toc2插件可以为Jupyter笔记生成侧边栏目录,也可将目录插入到笔记开头。它还可以自动为标题编号。
使用技巧
装扮你的 Jupyter Notebook
[译]27 个Jupyter Notebook的小提示与技巧
参考链接