简介

Jupyter是一个基于网页的用于交互计算的应用程序。其可被应用于全过程计算:开发、文档编写、运行代码和展示结果。

简而言之,Jupyter Notebook是以网页的形式打开,可以在网页页面中直接编写代码和运行代码,代码的运行结果也会直接在代码块下显示的程序。如在编程过程中需要编写说明文档,可在同一个页面中直接编写,便于作及时的说明和解释。

部署

以下基于RockyLinux8.7部署

  • 修改系统Python版本
# 将默认Python版本修改为3.8
alternatives --config python3

There are 3 programs which provide 'python3'.

Selection Command
-----------------------------------------------
* 1 /usr/bin/python3.6
2 /usr/bin/python3.11
+ 3 /usr/bin/python3.8

Enter to keep the current selection[+], or type selection number: 3
  • 执行部署
# 环境安装
sudo dnf install -y python3-pip nginx
sudo pip3 install --upgrade pip
sudo pip3 install ipython
sudo pip3 install autopep8
sudo pip3 install jupyterlab
sudo pip3 install jupyter
  • 建立服务脚本
# 建立Systemd的启动脚本
# sudo vim /usr/lib/systemd/system/jupyter.service
[Unit]
Description=Jupyter
After=syslog.target network.target

[Service]
# 指定执行用户
User=sujx
Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
# 指定浏览目录
WorkingDirectory=/home/sujx/projects/
# 配置运行参数,不启动浏览器,不限制访问IP
ExecStart=/usr/local/bin/jupyter notebook --no-browser --ip 0.0.0.0

Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target
  • 配置Nginx反向代理
# sudo vim /etc/nginx/conf.d/jupyter.conf
# 设置py.sujx.net为访问域名

server {
listen 80;
server_name py.sujx.net;
return 301 https://$host$request_uri;
# rewrite ^(.*)$ https://$host$1 permanent;
}

server {
listen 443 ssl;
server_name py.sujx.net;

ssl_certificate /opt/cert/py.sujx.net.pem;
ssl_certificate_key /opt/cert/py.sujx.net.key;

ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;

location / {
proxy_pass http://localhost:8888;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
}
}
  • 拉起服务
sudo systemctl daemon-reload
sudo systemctl enable --now jupyter.service
sudo systemctl restart nginx

插件

# 生成配置文件
jupyter notebook --generate-config
# 生成自动补全
sudo pip3 install jupyter_contrib_nbextensions jupyter_nbextensions_configurator
jupyter contrib nbextension install --user
jupyter nbextensions_configurator enable --user
# 配置主题
sudo pip3 install jupyterthemes
# 查看主题
jt -l
# 配置主题
jt -t monokai

使用

  • 访问Token
$jupyter notebook list
Currently running servers:
http://0.0.0.0:8888/?token=0dc58bfd73d2d966d31f9d3f4e :: /home/sujx/projects

index

  • 配置
    开启插件功能,依图打开相关插件功能
    extensions

  • 编写代码并运行
    newpyfile
    code