jupyter について
JupyterノートブックをJSONファイルとして扱うための実用ガイドです。セルの編集、実行、形式変換、コード検索、出力のクリア方法を説明します。
主な機能
- 01JSONとしてノートブックのセルを編集
- 02nbconvertによるノートブック実行
- 03HTML、スクリプト、Markdownへの変換
- 04ノートブック内容の検索
- 05コードセル出力のクリア
活用シーン
- 01ノートブックのソースをプログラムで編集
- 02ノートブックのインプレース実行
- 03ノートブックを別形式にエクスポート
インストール
コマンドをコピーしてターミナルで実行します。
$
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 対応