关于 jupyter
这项实用技能介绍如何将 Jupyter notebook 作为 JSON 文件处理,涵盖单元格编辑、执行、格式转换、代码搜索和清除输出。
主要功能
- 01以 JSON 编辑 notebook 单元格
- 02使用 nbconvert 执行 notebook
- 03转换为 HTML、脚本或 Markdown
- 04搜索 notebook 内容
- 05清除代码单元格输出
适用场景
- 01以编程方式编辑 notebook 源内容
- 02原地运行 notebook
- 03将 notebook 导出为其他格式
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ openhands/ extensions --skill jupyter安装后在 Claude Code 中输入/jupyter确认已加载。
附带文件.plugin/plugin.jsonREADME.md
详细介绍
Jupyter Notebook 指南
Notebook 是 JSON 文件。单元格位于 nb['cells'] 中,每个单元格都有 source(字符串列表)和 cell_type('code'、'markdown' 或 'raw')。
修改 Notebook
import json
with open('notebook.ipynb') as f:
nb = json.load(f)
# Modify nb['cells'][i]['source'], then:
with open('notebook.ipynb', 'w') as f:
json.dump(nb, f, indent=1)
执行与转换
jupyter nbconvert --to notebook --execute --inplace notebook.ipynb # Execute in place
jupyter nbconvert --to html notebook.ipynb # Convert to HTML
jupyter nbconvert --to script notebook.ipynb # Convert to Python
jupyter nbconvert --to markdown notebook.ipynb # Convert to Markdown
查找代码
grep -n "search_term" notebook.ipynb
PowerShell 等效命令:
Select-String -Path notebook.ipynb -Pattern "search_term"
单元格结构
# Code cell
{"cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": ["code\n"]}
# Markdown cell
{"cell_type": "markdown", "metadata": {}, "source": ["# Title\n"]}
清除输出
for cell in nb['cells']:
if cell['cell_type'] == 'code':
cell['outputs'] = []
cell['execution_count'] = None
评论
支持 Markdown