jupyterhub api使用
目录
API使用
创建Token
首先启动jupyterhub 服务,然后防护hub的接口 http://127.0.0.1:8001/ ,点击token,创建一个新的token
使用方法
1 2 3 4 5 6 7 |
# shell curl -s -H "Authorization: Token ff3bb7bb0a9b4483aca054f473b6d8b0" 'http://127.0.0.1:8000/hub/api/' # python import requests headers = {"Authorization": "Token bb210c695e684d97832029abb9236dcc"} res = requests.get('http://127.0.0.1:8000/hub/api/users/kevinhan', headers=headers) |
REST API
server
用户登录jupyerhub后,jupyterhub会调用 jupyterhub-singleuser,启动独立的进程作为用户的server,为用户提供独立的计算空间。
用户如果开启命名空间后,每个用户可以最多启动五个server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import requests headers = {"Authorization": "token 6bd14ec22f8c4956a89f2b87a3201333"} req = requests.session() # 启动服务 # 创建成功 返回:201,服务存在 返回:400 res = req.post('http://127.0.0.1:8000/hub/api/users/kevinhan/server', headers=headers) pprint.pprint(res.text) # 停止服务 # 服务删除成功 返回:204 res = req.delete('http://127.0.0.1:8000/hub/api/users/kevinhan/server', headers=headers) print(res.status_code) # 查看服务状态 curl -s -H "Authorization: token ff3bb7bb0a9b4483aca054f473b6d8b0" 'http://127.0.0.1:8000/hub/api/users/kevinhan' # 返回进度状态,返回eventstream,不以data: 开头的行,应该被忽略 curl -s -H "Authorization: token ff3bb7bb0a9b4483aca054f473b6d8b0" 'http://127.0.0.1:8000/hub/api/users/kevinhan/server/progress' |
contents
主要用于目录、文件管理,文件内容获取,检查点管理
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 28 29 30 31 |
import requests import pprint headers = {"Authorization": "token 6bd14ec22f8c4956a89f2b87a3201333"} req = requests.session() # 查看目录内容、指定文件内容 # 默认返回用户工作空间下的所有文件或目录,可以用作工作区的树形结构内容获取 data = { "type": "notebook", "path": "", "copy_from": "未命名6.ipynb"} res = req.get('http://127.0.0.1:8000/user/kevinhan/api/contents', headers=headers) pprint.pprint(res.json()) # 创建文件, 由服务器生成默认的文件名 data = { "type": "notebook", "path": ""} res = req.post('http://127.0.0.1:8000/user/kevinhan/api/contents', headers=headers, json=data) print(res.text) print(res.status_code) # 拷贝文件, 指定源文件:未命名6.ipynb 新文件名:未命名6-Copy1.ipynb data = { "type": "notebook", "path": "", "copy_from": "未命名6.ipynb"} res = req.post('http://127.0.0.1:8000/user/kevinhan/api/contents', headers=headers, json=data) print(res.text) print(res.status_code) # 文件重命名 # 将demo1.text 重命名并移动到新位置,Desktop/demo.text data = {"path": "Desktop/demo.text"} res = req.patch('http://127.0.0.1:8000/user/kevinhan/api/contents/demo1.text', headers=headers, json=data) pprint.pprint(res.json()) |
kernel
每启动一个kernel,在服务器上会多一个进程 ps aux|grep ipykernel_launcher,可以查看启动的kernel,每个kernel会有一个配置文件,在配置文件名是kernel的id,在配置文件中有mq各通道的端口
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import requests import pprint req = requests.session() # 获取已经支持的Kernel specs res = req.get('http://127.0.0.1:8000/user/kevinhan/api/kernelspecs', headers=headers) pprint.pprint(res.json()) # 查看Kernel res = req.get('http://127.0.0.1:8000/user/kevinhan/api/kernels', headers=headers) pprint.pprint(res.json()) # 启动一个新内核,会返回kerlnel的id data = {"name": "python"} res = req.post('http://127.0.0.1:8000/user/kevinhan/api/kernels', headers=headers) pprint.pprint(res.json()) # 查看指定内核的状态 data = {"name": "python"} res = req.get('http://127.0.0.1:8000/user/kevinhan/api/kernels/35f08db8-a740-4401-95a7-a1d4e817344a', headers=headers) pprint.pprint(res.json()) # 内核中断,会触发 KeyboardInterrupt res = req.post('http://127.0.0.1:8000/user/kevinhan/api/kernels/35f08db8-a740-4401-95a7-a1d4e817344a/interrupt', headers=headers) pprint.pprint(res.text) # 内核重启 res = req.post('http://127.0.0.1:8000/user/kevinhan/api/kernels/35f08db8-a740-4401-95a7-a1d4e817344a/restart', headers=headers) pprint.pprint(res.text) print(res.status_code) # 查看会话 res = req.get('http://127.0.0.1:8000/user/kevinhan/api/sessions', headers=headers) pprint.pprint(res.json()) # 创建一个会话 data = {"kernel": {"id": "1c647ec9-3e3d-4bfc-a94f-45c69c7e75da"}, "path": "", "name": "未命名4.ipynb", "type": "notebook"} res = req.post('http://127.0.0.1:8000/user/kevinhan/api/sessions', headers=headers, json=data) data = {"kernel": {"name": "python"}, "path": "", "name": "未命名5.ipynb", "type": "notebook"} res = req.post('http://127.0.0.1:8000/user/kevinhan/api/sessions', headers=headers, json=data) pprint.pprint(res.json()) # 删除指定会话 res = req.delete('http://127.0.0.1:8000/user/kevinhan/api/sessions/fb00ef48-a2af-41c3-9746-aabff90d2332', headers=headers, json=data) print(res.status_code) |