Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言。 Jupyter Notebook 的本质是一个 Web 应用程序,便于创建和共享文学化程序文档,支持实时代码,数学方程,可视化和 markdown。 用途包括:数据清理和转换,数值模拟,统计建模,机器学习等等。
- 安装
pip install jupyter
- 生成密钥
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
3.生成密码
In [1]: from IPython.lib import passwdIn [2]: passwd()Enter password: Verify password: Out[2]: 'sha1:fd5ec44a0295:6ec3329a47cac96601c1e7c794890443cf145736'
4.生成配置文件
jupyter的工作空间在其配置文件jupyter_notebook_config.py中。jupyter_notebook_config.py文件在~/.jupyter/下,如果找不到那就在终端输入下面命令
jupyter notebook --generate-config
5.更改配置文件
#把生成的密钥文件拷贝的jupyter目录下面:cp /root/mycert.pem /root/.juptervim /root/.jupyter/jupyter_notebook_config.py # 密码c.NotebookApp.password = 'sha1:fd5ec44a0295:6ec3329a47cac96601c1e7c794890443cf145736'# 密钥c.NotebookApp.certfile = '/root/.jupyter/mycert.pem'# ip设置c.NotebookApp.ip = '*'# 端口c.NotebookApp.port = 443
6.启动jupyter
jupyter notebook --allow-root
7.配置supervisor
配合supervisor,把jupyter直接变成一个服务。可以在Linux上创建一个jupyter用户,然后,用supervisor启动并指定端口,这里默认用root账户。
supervisor默认使用分号;注释
pip install supervisormkdir -p /etc/supervisor/conf.d echo_supervisord_conf > /etc/supervisor/supervisord.conf编辑配置文件vim /etc/supervisor/supervisord.conf[include]files = /etc/supervisor/conf.d/*.ini增加jupyter程序vim /etc/supervisor/conf.d/jupyter.ini [program:jupyter]command=jupyter notebook --allow-rootuser=rootautostart=true # 在supervisord启动的时候也自动启动;autorestart=true # 程序异常退出时自动重启;startsecs=30 ;startretries=5stdout_logfile=/var/log/jupyter/jupyter-access.logstderr_logfile=/var/log/jupyter/jupyter-error.logmkdir /var/log/jupyter# 启动supervisor[root@compute2 ~]# supervisord# 查看jupyter 服务状态[root@compute2 ~]# supervisorctl jupyter RUNNING pid 22818, uptime 0:10:48
8.浏览器访问
tips:
部署jupyter 远程访问有个大坑,就是如果加入不配置密钥的话,用默认的http访问,会发现连接python一直出现错误:
A connection to the notebook server could not be established. The notebook will continue trying to reconnect. Check your network connection or notebook server configuration.
打开浏览器控制台发现websocket连接一直失败,出现如下错误:
kernel.js:461 WebSocket connection to 'ws://10.240.199.69/api/kernels/b152771e-944a-4936-9b01-98275ef62037/channels?session_id=36AF33D06BD54B9AA43D26B332C6D6FF' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET