JupyterLab
date
Apr 22, 2022
slug
jupyter
status
Published
tags
Jupyter
summary
jupyter一些使用说明
type
Post
Docker安装方式构建docker启动和配置密码安装插件重新配置root密码和权限问题安装apt插件ubuntu下更改时区和时间配置condakernelLinux Conda安装JupyterLab启动JupyterLab进程守护安装debugger安装matplotlib安装jupyter-kite代码补全
Docker安装方式
构建docker
version: '3'
services:
jupyterlab:
image: jupyter/scipy-notebook
user: root # 需要root启动才可以自定义目录
restart: always
container_name: jupyterlab
ports:
- 8888:8888
environment:
- NB_USER=wangjiahao # 变成/home/wangjiahao
- CHOWN_HOME=yes # 是否改变工作目录
working_dir: /home/wangjiahao # 指定工作目录
volumes:
- ./data:/home/wangjiahao # 挂载目录
启动和配置密码
# 创建并启动
docker-compose up -d
# 查看日志,最后有token输出,这个token可以改控制台登录密码
docker-compose logs -f
进入web页面,输入token,并设置密码
安装插件
进入控制台,开启插件
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install jupyterlab-git
然后重启jupter,点击file - shutdown ,重新进入输入刚才修改的密码
重新配置root密码和权限问题
docker exec -u 0 -it mycontainer bash
# 设置新密码
passwd wangjiahao
# 加入sudoers清单:
visudo
# 加入:
wangjiahao ALL=(ALL:ALL) ALL
安装apt插件
# Update the system package index:
sudo apt-get update
# Install the missing ping command:
sudo apt-get install iputils-ping
ubuntu下更改时区和时间
# 查看时间
date
# 设置时区
tzselect
# 选择Asia,输入number
4
# 选择China 输入number
10
# 选择Beijing 输入number
1
# 选择yes 输入number
1
# 重新配置
sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 重新查看
date -R
配置conda
source activate bash
kernel
配置conda环境
# 配置conda环境
source activate base
# 创建一个mypytorch的环境
conda create -n mypytorch python=3.8
# 激活mypytorch
conda avtivate mypytorch
# 安装ipykernel
conda install ipykernel
# 查看jupyter的kernel
jupyter kernelspec list
# 配置jupyter的kernel
# python -m ipykernel install --user --name 环境名称 --display-name "Python (环境名称)"
python -m ipykernel install --user --name mypytorch --display-name "mypytorch"
# 去jupyter看看就可以了
查看所有核心
jupyter kernelspec list
卸载指定核心
jupyter kernelspec remove kernel_name
Linux Conda安装JupyterLab
JupyterLab Version 3.2.8 测试无问题
首先创建虚拟环境
conda create -n wang python=3.9
pip3 install jupyterlab
生成配置
jupyter lab --generate-config
生成密码
ipython
from jupyter_server.auth import passwd
passwd()
# 输入两次密码,会生成一串加密字符。请复制并保存这串字符。
# 退出ipython模式
exit()
创建工作文件夹
mkdir whan
这个文件夹将用于保存你使用JupyterLab产生的文件。
修改配置文件
vim /root/.jupyter/jupyter_lab_config.py
进入配置文件。
输入小写i进入编辑模式
c.ServerApp.allow_remote_access = True #远程访问
c.ServerApp.allow_root = True #使用root运行
c.ServerApp.ip = '*' #监听地址
c.ServerApp.port = 80 #端口,默认8888
c.ServerApp.root_dir = '/root/whan/' #jupyterlab工作目录
c.ServerApp.password = 'sha1:刚刚你获得字符串' #密码
c.ServerApp.open_browser = False #不打开浏览器
启动JupyterLab
现在,你可以启动JupyterLab了
jupyter lab
使用ctrl+C可以停止
进程守护
让JupyterLab在没有ssh登录的情况下能够继续运行,即成为系统服务。本文以Systemd为例
vim /usr/lib/systemd/system/myjupyter.service
输入小写i进入编辑模式
[UNIT]
#服务描述
Description=Helo JupyterLab Service
Documentation=https://www.ishelo.com
After=network.target
Wants=network.target
[Service]
ExecStart=/root/anaconda3/envs/wang/bin/jupyter lab
Restart=on-abnormal
RestartSec=5s
KillMode=mixed
#创建私有的内存临时空间
PrivateTmp=True
[Install]
#多用户
WantedBy=multi-user.target
- 先使用esc退出编辑模式
- 输入:wq保存文件
# 更新配置
systemctl daemon-reload
# 启动服务
systemctl start myjupyter
# 设置开机启动
systemctl enable myjupyter
以下为管理命令:
# 启动服务
systemctl start myjupyter
# 停止服务
systemctl stop myjupyter
# 重启服务
systemctl restart myjupyter
# 查看状态
systemctl status myjupyter
安装插件
安装debugger
pip install xeus-python # ==0.8.6
安装matplotlib
pip install ipympl
安装jupyter-kite代码补全
pip install "jupyterlab-kite>=2.0.2"