自从搬家后,树莓派闲置了好一段时间,最近打算将其利用起来。想来想去,搭个jupyter notebook用要靠谱的,毕竟经常要实验一些Python脚本。
具体过程参考以下链接:
https://www.techcoil.com/blog/how-to-setup-jupyter-notebook-on-raspberry-pi-3-with-raspbian-stretch-lite-supervisor-and-virtualenv-to-run-python-3-codes/
教程中给出了在virtualenv中启动Python3版本的jupyter notebook server的方案,这样做的好处是可以方便地单独管理jupyter的包。在这个教程中,还发现了个人折腾启动服务的神器,supervisor,好评。
tips: 如果想要让jupyter同时支持Python2和Python3,安装对应的python2-ipykernel即可,此时Python2为
/usr/bin/python2
最后附上,根据教程编写的自动化脚本:
# 安装supervisor和virtualenv
apt install supervisor python3-virtualenv -y
# 在virtualenv中安装jupyter
python3 -m virtualenv -p python3 /home/pi/jupyter-env
source /home/pi/jupyter-env/bin/activate
pip install jupyter
deactivate
# 在virtualenv环境中启动jupyter
mkdir /home/pi/jupyter-notebook
chmod 777 /home/pi/jupyter-notebook
# # 设置jupyter启动脚本
(
cat << EOF
#!/bin/bash
source /home/pi/jupyter-env/bin/activate
jupyter notebook --ip 0.0.0.0 --port 9999 --no-browser
deactivate
EOF
) > /home/pi/jupyter-notebook/run-jupyter-notebook.sh
sudo chmod +x /home/pi/jupyter-notebook/run-jupyter-notebook.sh
# supervisor配置
(
cat << EOF
#!/bin/bash
[program:jupyter-notebook]
directory=/home/pi/jupyter-notebook
command=/bin/bash -E -c ./run-jupyter-notebook.sh
autostart=true
autorestart=true
stopsignal=INT
stopasgroup=true
killasgroup=true
user=pi
EOF
) > /etc/supervisor/conf.d/jupyter-notebook.conf
# 重启supervisor以启动jupyter notebook
systemctl restart supervisor.service
sleep 5
# 输出用于登录验证的token
supervisorctl tail jupyter-notebook stdout