--- name: jupyter-notebook-writing description: Write Milvus application-level Jupyter notebook examples using a Markdown-first workflow with jupyter-switch for format conversion. --- # Skill: Jupyter Notebook Writing Write Milvus application-level Jupyter notebook examples as a DevRel workflow. Uses a Markdown-first approach — AI edits `.md` files, then converts to `.ipynb` via `jupyter-switch`. > **Prerequisites**: Python >= 3.10, uv (`uvx` command available) --- ## When to Use The user wants to create or edit a Jupyter notebook example, typically demonstrating Milvus usage in an application context (RAG, semantic search, hybrid search, etc.). --- ## Core Workflow: Markdown-First Editing Jupyter `.ipynb` files contain complex JSON with metadata, outputs, and execution counts — painful for AI to edit directly. Instead: 1. **Write/Edit the `.md` file** — AI works with clean Markdown 2. **Convert to `.ipynb`** — using `jupyter-switch` for runnable notebook 3. **Keep both files in sync** — the `.md` is the source of truth for editing ### Format Convention In the `.md` file: - **Python code blocks** (` ```python ... ``` `) become **code cells** in the notebook - **Everything else** becomes **markdown cells** - Cell outputs are not preserved in `.md` (they get generated when running the notebook) ### Conversion Commands ```bash # Markdown -> Jupyter Notebook uvx jupyter-switch example.md # produces example.ipynb # Jupyter Notebook -> Markdown uvx jupyter-switch example.ipynb # produces example.md ``` - The original input file is never modified or deleted - If the output file already exists, a `.bak` backup is created automatically --- ## Step-by-Step ### Creating a New Notebook 1. Create `example.md` with the content (see structure below) 2. Convert: `uvx jupyter-switch example.md` 3. Both `example.md` and `example.ipynb` now exist ### Editing an Existing Notebook 1. If only `.ipynb` exists, convert first: `uvx jupyter-switch example.ipynb` 2. Edit the `.md` file 3. Convert back: `uvx jupyter-switch example.md` ### Testing / Running #### 1. Resolve the Jupyter execution environment Before running any notebook, you must determine which Python environment to use. The system default `jupyter execute` may not have the required packages installed. **Step A — Detect available environments.** ```bash # Discover conda/mamba environments conda env list 2>/dev/null || mamba env list 2>/dev/null # Discover registered Jupyter kernels jupyter kernelspec list 2>/dev/null # Check system default Python which python3 2>/dev/null && python3 --version 2>/dev/null # Check for local virtual environment in the working directory ls -d .venv/ venv/ 2>/dev/null # Check if a uv-managed project (pyproject.toml + .venv) test -f pyproject.toml && test -d .venv && echo "uv/pip project venv detected" ``` **Step B — Ask the user which environment to use.** Present a numbered list of choices. Include all detected environments: 1. **System default** — run `jupyter execute` as-is, no `--kernel_name` 2. **Each detected conda/mamba environment** — show name and path 3. **Each registered Jupyter kernel** — show kernel name 4. **Local venv** (if `.venv/` or `venv/` found in working directory) — the Python inside that venv 5. **Custom** — let the user type a Python path or environment name > **Note on uv projects:** If the working directory has `pyproject.toml` + `.venv/` (a uv-managed project), the local venv option covers this case. The user can also run `uv run jupyter execute example.ipynb` directly if jupyter is a project dependency. Example prompt: ``` Which Python environment should I use to run this notebook? 1. System default (jupyter execute as-is) 2. conda: myenv (/path/to/envs/myenv) 3. Jupyter kernel: some-kernel 4. Local venv (.venv/) 5. Custom — enter a path or environment name ``` **Step C — Apply the chosen environment:** | Scenario | Action | |----------|--------| | Already a registered Jupyter kernel | Use `jupyter execute --kernel_name=` | | Conda env not yet registered as kernel | Register first: ` -m ipykernel install --user --name --display-name "