academic-jupyter
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAcademic Jupyter — Notebook Management
学术Jupyter——笔记本管理
Overview
概述
A headless Jupyter Server runs at inside the container.
Use its REST API and kernel WebSocket protocol to create notebooks, manage
kernels, and execute code cells. The Jupyter token is in .
http://localhost:8888$JUPYTER_TOKEN容器内的无头Jupyter Server运行在。使用其REST API和内核WebSocket协议来创建笔记本、管理内核并执行代码单元格。Jupyter令牌存储在环境变量中。
http://localhost:8888$JUPYTER_TOKENWhen To Use
使用场景
- User asks to create or run a Jupyter notebook
- User wants an interactive computing session with persistent state
- User needs to execute multi-step data analysis with shared variables
- User asks for a notebook-style workflow (code → output → iterate)
- 用户要求创建或运行Jupyter笔记本
- 用户需要带有持久化状态的交互式计算会话
- 用户需要执行多步骤数据分析并共享变量
- 用户要求采用笔记本风格的工作流(代码→输出→迭代)
Authentication
身份验证
All requests require the token:
bash
undefined所有请求都需要令牌:
bash
undefinedToken is available as environment variable
令牌可通过环境变量获取
echo $JUPYTER_TOKEN
echo $JUPYTER_TOKEN
Use in requests:
在请求中使用令牌:
curl -sf "http://localhost:8888/api/status?token=$JUPYTER_TOKEN" | jq .
undefinedcurl -sf "http://localhost:8888/api/status?token=$JUPYTER_TOKEN" | jq .
undefinedKernel Management
内核管理
List running kernels
列出运行中的内核
bash
curl -sf "http://localhost:8888/api/kernels?token=$JUPYTER_TOKEN" | jq .bash
curl -sf "http://localhost:8888/api/kernels?token=$JUPYTER_TOKEN" | jq .Start a new kernel
启动新内核
bash
curl -sf -X POST "http://localhost:8888/api/kernels?token=$JUPYTER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name": "python3"}' | jq .bash
curl -sf -X POST "http://localhost:8888/api/kernels?token=$JUPYTER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name": "python3"}' | jq .→ {"id": "<kernel-id>", "name": "python3", "state": "starting", ...}
→ {"id": "<kernel-id>", "name": "python3", "state": "starting", ...}
undefinedundefinedRestart a kernel (clears state)
重启内核(清除状态)
bash
curl -sf -X POST "http://localhost:8888/api/kernels/<kernel-id>/restart?token=$JUPYTER_TOKEN" | jq .bash
curl -sf -X POST "http://localhost:8888/api/kernels/<kernel-id>/restart?token=$JUPYTER_TOKEN" | jq .Shutdown a kernel
关闭内核
bash
curl -sf -X DELETE "http://localhost:8888/api/kernels/<kernel-id>?token=$JUPYTER_TOKEN"bash
curl -sf -X DELETE "http://localhost:8888/api/kernels/<kernel-id>?token=$JUPYTER_TOKEN"Notebook File Management (Contents API)
笔记本文件管理(Contents API)
List notebooks
列出笔记本
bash
curl -sf "http://localhost:8888/api/contents/notebooks?token=$JUPYTER_TOKEN" | jq '.content[] | {name, path, type}'bash
curl -sf "http://localhost:8888/api/contents/notebooks?token=$JUPYTER_TOKEN" | jq '.content[] | {name, path, type}'Create a new notebook
创建新笔记本
bash
curl -sf -X PUT "http://localhost:8888/api/contents/notebooks/analysis.ipynb?token=$JUPYTER_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"type": "notebook",
"content": {
"cells": [
{
"cell_type": "markdown",
"source": "# Analysis Notebook\nCreated by Prismer Academic Assistant.",
"metadata": {}
},
{
"cell_type": "code",
"source": "import numpy as np\nimport matplotlib.pyplot as plt\nprint(\"Ready!\")",
"metadata": {},
"outputs": [],
"execution_count": null
}
],
"metadata": {
"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"},
"language_info": {"name": "python", "version": "3.12.3"}
},
"nbformat": 4,
"nbformat_minor": 5
}
}' | jq '{name, path, created}'bash
curl -sf -X PUT "http://localhost:8888/api/contents/notebooks/analysis.ipynb?token=$JUPYTER_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"type": "notebook",
"content": {
"cells": [
{
"cell_type": "markdown",
"source": "# Analysis Notebook\nCreated by Prismer Academic Assistant.",
"metadata": {}
},
{
"cell_type": "code",
"source": "import numpy as np\nimport matplotlib.pyplot as plt\nprint(\"Ready!\")",
"metadata": {},
"outputs": [],
"execution_count": null
}
],
"metadata": {
"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"},
"language_info": {"name": "python", "version": "3.12.3"}
},
"nbformat": 4,
"nbformat_minor": 5
}
}' | jq '{name, path, created}'Read a notebook
读取笔记本
bash
curl -sf "http://localhost:8888/api/contents/notebooks/analysis.ipynb?token=$JUPYTER_TOKEN" | jq .bash
curl -sf "http://localhost:8888/api/contents/notebooks/analysis.ipynb?token=$JUPYTER_TOKEN" | jq .Delete a notebook
删除笔记本
bash
curl -sf -X DELETE "http://localhost:8888/api/contents/notebooks/analysis.ipynb?token=$JUPYTER_TOKEN"bash
curl -sf -X DELETE "http://localhost:8888/api/contents/notebooks/analysis.ipynb?token=$JUPYTER_TOKEN"Execute Code in a Kernel
在内核中执行代码
For executing code, use the Jupyter kernel's execute endpoint.
The simplest approach: write code to a file and run it, or use the
REST API to execute within a kernel session.
.py要执行代码,请使用Jupyter内核的执行端点。最简单的方法是将代码写入文件并运行,或者使用REST API在内核会话中执行代码。
.pyQuick execution via file
通过文件快速执行
bash
undefinedbash
undefinedWrite a cell's code to temp file and execute
将单元格代码写入临时文件并执行
cat > /tmp/cell.py << 'PYTHON'
import numpy as np
x = np.random.randn(1000)
print(f"Mean: {x.mean():.4f}, Std: {x.std():.4f}")
PYTHON
python3 /tmp/cell.py
undefinedcat > /tmp/cell.py << 'PYTHON'
import numpy as np
x = np.random.randn(1000)
print(f"Mean: {x.mean():.4f}, Std: {x.std():.4f}")
PYTHON
python3 /tmp/cell.py
undefinedFor persistent state across cells
用于跨单元格的持久化状态
When the user needs variables to persist between executions (true notebook
experience), start a kernel and use the Jupyter execute API:
bash
undefined当用户需要变量在多次执行之间保持持久(真正的笔记本体验)时,请启动一个内核并使用Jupyter执行API:
bash
undefined1. Start kernel
1. 启动内核
KERNEL_ID=$(curl -sf -X POST "http://localhost:8888/api/kernels?token=$JUPYTER_TOKEN"
-H "Content-Type: application/json" -d '{"name":"python3"}' | jq -r '.id')
-H "Content-Type: application/json" -d '{"name":"python3"}' | jq -r '.id')
KERNEL_ID=$(curl -sf -X POST "http://localhost:8888/api/kernels?token=$JUPYTER_TOKEN"
-H "Content-Type: application/json" -d '{"name":"python3"}' | jq -r '.id')
-H "Content-Type: application/json" -d '{"name":"python3"}' | jq -r '.id')
2. Execute code via the kernel (uses WebSocket internally)
2. 通过内核执行代码(内部使用WebSocket)
For simple cases, write the notebook with cells and outputs.
对于简单场景,编写包含单元格和输出的笔记本。
For complex interactive sessions, guide the user to open Jupyter in browser.
对于复杂的交互式会话,引导用户在浏览器中打开Jupyter。
undefinedundefinedAPI Reference
API参考
| Endpoint | Method | Purpose |
|---|---|---|
| GET | Server status |
| GET | List kernels |
| POST | Start kernel ( |
| DELETE | Shutdown kernel |
| POST | Restart kernel |
| GET | Read file/notebook |
| PUT | Create/update file/notebook |
| DELETE | Delete file/notebook |
All endpoints require query parameter.
?token=$JUPYTER_TOKEN| 端点 | 方法 | 用途 |
|---|---|---|
| GET | 服务器状态 |
| GET | 列出内核 |
| POST | 启动内核( |
| DELETE | 关闭内核 |
| POST | 重启内核 |
| GET | 读取文件/笔记本 |
| PUT | 创建/更新文件/笔记本 |
| DELETE | 删除文件/笔记本 |
所有端点都需要查询参数。
?token=$JUPYTER_TOKENNotebook Storage
笔记本存储
- Default notebook directory:
/workspace/notebooks/ - The Jupyter server serves files from
/workspace/ - Save notebooks to
/workspace/notebooks/<name>.ipynb
- 默认笔记本目录:
/workspace/notebooks/ - Jupyter服务器从提供文件服务
/workspace/ - 将笔记本保存到
/workspace/notebooks/<name>.ipynb
Tips
提示
- For simple one-off computations, use directly (academic-python skill).
python3 - Use Jupyter when the user needs persistent state across multiple code executions.
- Always include in visualization cells.
matplotlib.use('Agg') - Kernel state is lost on restart — save important results to files.
- The web frontend may have a Notebook Viewer for rendering files.
.ipynb
- 对于简单的一次性计算,直接使用(academic-python技能)。
python3 - 当用户需要跨多次代码执行的持久化状态时,使用Jupyter。
- 在可视化单元格中始终包含。
matplotlib.use('Agg') - 重启内核会丢失状态——将重要结果保存到文件中。
- Web前端可能有用于渲染文件的笔记本查看器。
.ipynb