jupyter 使用
目录
jupyter介绍
jupyterhub 主要提供用户认证、多用户环境
前端jupyterhub 将请求转发 到notebook,notebook在将请求通过 zmq 与 ipykernel进行通信
客户端工具可以是:notebook、ipython、jupyter console
jupyterhub 、 jupyterlab、jupyter notebook的关系:
jupyterhub主要负责前端的请求转发、用户认证和多用户环境,每个用户会启动一个jupyterlab的环境
jupyterlab 是一个灵活、可扩展的交互式界面,替代notebook
jupyter notebook 没啥好说的,就是交互式界面..,由lab替代
jupyterhub安装
1 2 3 4 |
pip3 install jupyterhub pip3 install sudospawner npm install -g configurable-http-proxy |
jupyterhub配置文件
生成配置文件
1 2 3 4 5 |
jupyterhub --generate-config mkdir /etc/jupyter chown cloud.cloud /etc/jupyter mv jupyterhub_config.py /etc/jupyter |
配置修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# /etc/jupyter/jupyterhub_config.py # 设置管理员 c.JupyterHub.admin_users = {'xxx'} # 启用pam认证 c.JupyterHub.authenticator_class = 'jupyterhub.auth.PAMAuthenticator' c.JupyterHub.hub_port = 8999 c.JupyterHub.port = 9000 # jupyterhub通过sudo方式启动,使用sudospawner c.JupyterHub.spawner_class = 'sudospawner.SudoSpawner' c.PAMAuthenticator.open_sessions = False # 默认的notebook目录位置 c.Spawner.notebook_dir = '/foundry/bot/dev/{username}' |
修改shadow权限
配置/etc/shadow 文件,使用cloud用户可读取权限
sudo配置
1 2 3 4 5 6 |
# /etc/sudoers Runas_Alias JUPYTER_USERS = cloud Cmnd_Alias JUPYTER_CMD = /usr/local/qh-python37/bin/sudospawner cloud ALL=(JUPYTER_USERS) NOPASSWD:JUPYTER_CMD %cloud ALL=(ALL) NOPASSWD:JUPYTER_CMD |
systemd配置
1 2 3 4 5 6 7 8 9 10 11 12 |
[Unit] Description=JupyterHub After=syslog.target network.target [Service] User=cloud WorkingDirectory=/etc/jupyter/ Environment="PATH=/usr/local/qh-python37/bin:/usr/condabin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" ExecStart=/usr/local/qh-python37/bin/jupyterhub -f /etc/jupyter/jupyterhub_config.py [Install] WantedBy=multi-user.target |
服务启动或停止
1 2 3 4 5 6 7 8 9 10 11 |
# 当配置新生成或更新后,需要重新加载一下 systemctl daemon-reload # 启动服务 systemctl start jupyterhub # 停止服务 systemctl stop jupyterhub # 重启服务 systemctl restart jupyterhub |
其他客户端操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
查看可用内核 jupyter kernelspec list # 使用命令行console, 并启动指定的kernel jupyter console --kernel foundry # 查看插件列表 jupyter labextension list # 安装插件 jupyter labextension install <plugin_name> # 卸载插件 jupyter labextension uninstall <plugin_name> # 关闭插件 jupyter labextension disable <plugin_name> # 开启插件 jupyter labextension enable <plugin_name> |
使用alluxio的问题:
alluxio不支持修改接口,所以jupyter在底层保存文件操作时,需要先删除在写入,不能进行覆盖。
在文件 site-packages/jupyter_server/services/contents/filemanager.py L449行的位置,新增 450和451
1 2 3 4 5 6 |
447 if model["type"] == "notebook": 448 nb = nbformat.from_dict(model["content"]) 449 self.check_and_sign(nb, path) 450 if os.path.isfile(os_path): 451 os.remove(os_path) 452 self._save_notebook(os_path, nb) |
参考:
sudo:https://jupyterhub.readthedocs.io/en/stable/reference/config-sudo.html
插件开发文档:https://jupyterlab.readthedocs.io/en/latest/extension/extension_dev.html
插件样例代码:https://github.com/jupyterlab/extension-examples