安装JupyterLab

# 安装jupyterlab
pip install jupyterlab

# 安装中文语言包
pip install jupyterlab-language-pack-zh-CN

# 启动Jupyterlab,如果以root用户运行,需要加上 --allow-root 参数
jupyter-lab

配置远程访问

# 设置密码,生成登录密钥,复制生成的密钥内容
python3 -c 'from notebook.auth import passwd;print(passwd())'

# 生成jupyter配置文件
jupyter notebook --generate-config

# 更改配置文件名
mv ~/.jupyter/jupyter_notebook_config.py ~/.jupyter/jupyter_server_config.py

# 修改配置文件
vim ~/.jupyter/jupyter_server_config.py

# 在文件末尾添加以下内容
c.ServerApp.allow_remote_access = True # 允许远程访问
c.ServerApp.allow_root = True          # 允许root权限
c.ServerApp.ip = '*'                   # 允许任意IP访问
c.ServerApp.port = 8888                # 监听端口
c.ServerApp.password = 'argon2:$...'   # 粘贴上面生成的密钥
c.ServerApp.open_browser = False       # 启动时打开浏览器

# 启动jupyterlab,后台常驻
nohup jupyter-lab > /dev/null 2>&1 &

访问服务器地址+端口,例如127.0.0.1:8888,输入之前设置的密码进行登录

遇到的问题

matplotlib无法显示中文

# 安装中文字体
wget https://github.com/StellarCN/scp_zh/raw/master/fonts/SimHei.ttf

# 查看matplotlib配置文件路径
python3 -c 'import matplotlib;print(matplotlib.matplotlib_fname());'

# 复制字体至 mpl-data/fonts/ttf
cp SimHei.ttf /usr/local/lib/python3.8/dist-packages/matplotlib/mpl-data/fonts/ttf

# 修改matplotlib配置
vim /usr/local/lib/python3.8/dist-packages/matplotlib/mpl-data/matplotlibrc

font.family: sans-serif      # 取消注释
font.sans-serif: SimHei, ... # 添加SimHei字体
axes.unicode_minus: False    # 修复负号显示问题

# 删除缓存
rm -rf ~/.cache/matplotlib

重启Jupyterlab,此时应已经支持中文显示

无法显示plt图表

https://github.com/matplotlib/matplotlib/issues/14534#issuecomment-501086275

# 安装ipympl
!pip install ipympl

# 使用matplotlib Jupyter魔法命令
%matplotlib widget

重启jupyter内核,此时应可正常加载出图表

参考链接

  1. https://jupyter.org/install

  2. https://www.1024sou.com/article/358084.html

  3. https://blog.csdn.net/weixin_41103006/article/details/113363256

  4. https://blog.xinhaojin.top/2021/10/14/jupyter-matplotlib…

  5. https://github.com/matplotlib/ipympl