Jupytext

From edwinhu/workflows · View interactive version

Jupytext is an AI agent skill from the GitHub repository edwinhu/workflows. ALWAYS use before touching a notebook as text or running one headlessly - "convert this notebook to a .py", "turn this script into a notebook", "get this ipynb into git", "the notebook diffs are unreadable", "keep the .py and .ipynb in sync", "run this notebook end to end", "execute the notebook wit

How to use this skill

Skill file

What this skill carries — grep references/ for any subject the names below miss:

!d=${CLAUDE_SKILL_DIR}; command -v skill-toc >/dev/null 2>&1 && exec skill-toc "$d"; s=$HOME/.claude/skills/plugin-utils/bin/skill-toc; [ -x "$s" ] && exec "$s" "$d"; echo "(skill-toc unavailable: references and scripts are NOT listed here — install the plugin-utils plugin, or start a new session so its bin/ reaches PATH)"

Contents

Jupytext Skill

Jupytext converts Jupyter notebooks to/from text formats (.py, .R, .md), enabling version control and multi-kernel workflows.

Execution Enforcement

IRON LAW: NO EXECUTION CLAIM WITHOUT OUTPUT VERIFICATION

Before claiming ANY jupytext script executed successfully, follow this sequence:

  1. EXECUTE using the papermill pipeline: jupytext --to notebook --output - script.py | papermill - output.ipynb
  2. CHECK for execution errors (papermill exit code and stderr)
  3. VERIFY output.ipynb exists and is non-empty
  4. INSPECT outputs using notebook-debug skill verification
  5. CLAIM success only after verification passes

This is non-negotiable. Skipping papermill execution is NOT HELPFUL — the user gets a notebook that fails on first run.

Jupytext Facts

Red Flags

Execution Verification Checklist

Before EVERY "notebook works" claim:

Conversion:

Execution (MANDATORY):

Output Verification:

Multi-Kernel Projects (if applicable):

Only after ALL checks pass:

Gate Function: Jupytext Execution

Follow this sequence for EVERY jupytext task involving execution:

1. CONVERT  → jupytext --to notebook --output -
2. EXECUTE  → papermill - output.ipynb (with params if needed)
3. CHECK    → Verify exit code and stderr
4. INSPECT  → Use notebook-debug verification
5. VERIFY   → Outputs match expectations
6. CLAIM    → "Notebook works" only after all gates passed

NEVER skip execution gate. Converting without executing proves nothing about correctness.

Core Concepts

Percent Format (Recommended)

Use percent format (py:percent) for all projects:

# %% [markdown]
# # Analysis Title

# %%
import pandas as pd
df = pd.read_csv("data.csv")

# %% tags=["parameters"]
input_file = "data.csv"

Cell markers: # %% for code, # %% [markdown] for markdown.

Markdown dollar signs: Always wrap $ in backticks to prevent LaTeX rendering - # Cost: $50` not # Cost: $50`

Project Configuration

Create jupytext.toml in project root:

formats = "ipynb,py:percent"
notebook_metadata_filter = "-all"
cell_metadata_filter = "-all"

Essential Commands

# Convert notebook to percent-format Python file
jupytext --to py:percent notebook.ipynb

# Convert Python script to Jupyter notebook format
jupytext --to notebook script.py

# Enable bidirectional pairing to keep formats synchronized
jupytext --set-formats ipynb,py:percent notebook.ipynb

# Synchronize paired notebook and text file
jupytext --sync notebook.ipynb

Execution (Recommended Pattern)

Always pipe to papermill for execution - no intermediate files:

# Convert script to notebook and execute in atomic operation
jupytext --to notebook --output - script.py | papermill - output.ipynb

# Convert and execute with parameter injection
jupytext --to notebook --output - script.py | papermill - output.ipynb -p start_date "2024-01-01" -p n_samples 1000

# Convert and execute with detailed logging output
jupytext --to notebook --output - script.py | papermill - output.ipynb --log-output

# Convert and execute in memory without saving intermediate files
jupytext --to notebook --output - script.py | papermill - -

Key flags:

Why this pattern:

  1. No intermediate .ipynb files cluttering the workspace
  2. Single atomic operation - convert and execute together
  3. Papermill handles parameters, logging, and error reporting
  4. Works in CI/CD pipelines without temp file cleanup

Debugging Runtime Errors

After execution, use notebook-debug skill to inspect tracebacks in the output ipynb.

Multi-Kernel Data Sharing

Share data between Python/R/Stata/SAS via files:

| Route | Format | Write | Read |

|-------|--------|-------|------|

| Python -> R | Parquet | df.to_parquet() | arrow::read_parquet() |

| Python -> Stata | DTA | df.to_stata() | use "file.dta" |

| Any -> Any | CSV | Native | Native |

| SQL queries | DuckDB | Query parquet directly | Query parquet directly |

Cross-Kernel Pipeline Pattern

Python (prep) -> Parquet -> R (stats) -> Parquet -> Python (report)
                    |
                    v
               Stata (.dta) -> Econometrics

Workflow Integration

Git Pre-commit Hook

Add the following to .pre-commit-config.yaml:

repos:
  - repo: https://github.com/mwouts/jupytext
    rev: v1.16.0
    hooks:
      - id: jupytext
        args: [--sync]  # Synchronize paired formats before commit

Version Control Strategy

Choose one approach:

Editor Integration

Configure editors for automatic synchronization:

Project Structure

Standard multi-kernel project layout:

project/
├── jupytext.toml          # Project-wide settings
├── environment.yml        # Conda env with all kernels
├── notebooks/
│   ├── 01_python_prep.py  # Python percent format
│   ├── 02_r_analysis.R    # R percent format
│   └── 03_stata_models.do # Stata script
├── data/
│   ├── raw/
│   └── processed/         # Parquet/DTA interchange files
└── results/

Kernel Specification

Specify kernel in file header:

# ---
# jupyter:
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# %% [markdown]
# # Python Analysis

Quick Troubleshooting

| Issue | Solution |

|-------|----------|

| Sync conflict | Delete .ipynb, regenerate from .py |

| Wrong kernel | Add kernelspec header to .py file |

| Metadata noise | Set notebook_metadata_filter = "-all" |

| Cell order lost | Use percent format (preserves structure) |

Additional Resources

Reference Files

Detailed patterns and configurations:

Example Files

Working code in examples/:

Scripts

Utility scripts in scripts/:

Best Practices

  1. Use percent format - Best balance of readability and cell preservation
  2. Strip metadata for git - Use metadata filters for cleaner diffs
  3. Use parquet for interchange - Type-safe, cross-language compatible format
  4. Document kernel requirements - Include in README or environment.yml
  5. Enable pre-commit hooks - Ensure synchronization before commits
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.