0%

Jupyter-lab 3.0 安装、配置并选择虚拟环境开机启动

Jupyter-Lab

1. 安装Jupyter-lab3.0

1
2
3
pip install jupyterlab==3
# 或者使用conda安装
# conda install -c conda-forge jupyterlab=3

2.生成配置文件并设置密码

1
2
jupyter lab --generate-config	
jupyter lab password

3. 编辑配置文件

1
vim ~/.jupyter/jupyter_lab_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 允许跨域访问,在用nginx反向代理jupyter-lab时需要打开
c.ServerApp.allow_origin = '*'

# 允许远程访问
c.ServerApp.allow_remote_access = True

# 监听来自所有ip的连接,默认时localhost只能本地访问
# 假设主机局域网ip为192.168.1.101,此处设置为局域网ip,则局域网内都可以访问
c.ServerApp.ip = '0.0.0.0'

# 设置默认打开路径
c.ServerApp.root_dir = "/home/username/code"

# 开启jupyter-lab时不自动打开浏览器
c.ServerApp.open_browser = False

# 默认监听端口
c.ServerApp.port = 58000

4. 安装插件

首先enable插件管理

4.1 安装中文语言包

1
pip install jupyterlab-language-pack-zh-CN

然后在settings→language中切换

4.2 debugger 语法检测

插件Github地址:https://github.com/jupyterlab/debugger

jupyter-lab3.0默认带有debugger,所以不需要额外安装。但缺少依赖包

安装依赖

1
2
3
conda install -c conda-forge xeus-python
# 或者
# pip install xeus-python

4.3 代码提示

插件Github地址:Language Server Protocol integration for Jupyter

安装方法

1
2
3
4
5
6
7
# 先安装Language Server Protocol integration for Jupyter-lab
conda install -c conda-forge jupyterlab-lsp
# 或者
#pip install jupyterlab-lsp

# 再安装语法库
pip install 'python-lsp-server[all]'

4.4 可交互 matplotlib

插件Github地址:ipympl

在代码开头插入即可

1
%matplotlib widget

安装方法:

1
2
3
conda install -c conda-forge ipympl
# 或者
# pip install ipympl

4.5 自动代码格式化

插件Github地址:Jupyterlab Code Formatter

配置参考链接:https://blog.csdn.net/mighty13/article/details/120024319

安装方法

1
2
pip install jupyterlab_code_formatter
pip install autopep8

重启jupyter lab

设置→高级编辑器

修改jupyterlab Code Formatter

输入内容:

1
2
3
4
5
6
7
8
{
"preferences": {
"default_formatter": {
"python": "autopep8",
"R": "styler"
}
}
}

通过点击状态栏或者右键即可使用

  • 对全局生效
  • 对当前代码块生效

5. 添加到开机启动项

1
sudo vim /etc/systemd/system/jupyter-lab.service

输入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[Unit]
Description=jupyter lab
After=network.target # 在网络启动后启动此服务

[Service]
Type=simple
User=username # 以此用户启动本程序
# 启动脚本
ExecStart=/home/marques/.conda/envs/torch-marques/bin/jupyter-lab --no-browser # jupyter-lab路径
# 因为我使用anaonda默认虚拟环境启动,所以选择执行脚本方式启动
# ExecStart=/home/marques/software/jupyterlabscript/lab.sh
ExecStop=/usr/bin/pkill jupyter-lab
KillMode=process
Restart=always # 程序崩溃后自动重启程序
RestartSec=30s # 当程序崩溃后30s重新启动

[Install]
WantedBy=multi-user.target

切换虚拟环境启动jupyter-lab脚本

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
#!/bin/bash -l
# 上面 -l 参数是指以会话方式打开脚本,否则无法切换anaconda虚拟环境

# 下面这段在~/.bashrc复制过来
# 其操作是将conda添加到环境变量
# 每台服务器安装位置不一定一致,要重新复制

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/dell/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/home/dell/anaconda3/etc/profile.d/conda.sh" ]; then
. "/home/dell/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/home/dell/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<

# 切换虚拟环境
conda activate torch-marques
# 启动jupyter-lab
/home/marques/.conda/envs/torch-marques/bin/jupyter-lab --no-browser

设置开机启动

1
2
3
sudo systemctl daemod-relaod
sudo systemctl enable jupyter-lab
sudo systemctl start jupyter-lab

TODO 使用Nignx反代理

挖个坑

参考链接

ubuntu20下anaconda中Jupyterlab3.0配置远程登录

在shell文件中启动anaconda的虚拟环境

欢迎关注我的其它发布渠道