Automations

Jupyter notebook output stripper and CI check

A standard-library Python script that clears outputs and execution counts from .ipynb files, plus a GitHub Actions check that fails pull requests containing notebooks with outputs, so API responses and sample data do not get committed.

Automation in Data by AISkillHub Library

What it does

Notebooks are where most AI experiments start, and their outputs are where data leaks: printed API responses, sample customer records, tokens echoed by a debug cell, or megabytes of embeddings that bloat the repository and make diffs unreadable. This script removes outputs before commit, and the workflow makes sure nobody forgets.

**Two modes**

- `--check` reports notebooks with outputs, execution counts or volatile cell metadata and exits 1. Nothing is changed. This is what CI runs. - `--fix` clears them and rewrites the files.

**What `--fix` changes**

For each code cell: `outputs` becomes `[]`, `execution_count` becomes `null`, and the volatile metadata keys `execution`, `collapsed`, `scrolled` and `ExecuteTime` are removed (keep them with `--keep-metadata`). Markdown cells, raw cells, cell sources and notebook-level metadata such as the kernel spec are untouched. Files are written with one-space JSON indentation, the same format Jupyter uses, so the diff shows only the removed outputs.

**Example**

``` $ python3 scripts/strip_notebooks.py --check experiments/rag-eval.ipynb: 7 code cell(s) with outputs or execution data

1 notebook(s) need stripping. Run: python3 scripts/strip_notebooks.py --fix $ python3 scripts/strip_notebooks.py --fix stripped experiments/rag-eval.ipynb (7 cell(s)) ```

**What it will not do**

- Remove secrets from cell **source**. A hardcoded key in code stays there; pair this with a secret scanner. - Rewrite git history. Outputs already pushed remain in earlier commits. - Handle notebook formats older than nbformat 4.

If your team needs outputs in some notebooks (for example, published tutorials), keep those in a separate folder and pass only the experiment folders to the check.

Exit codes: 0 clean or fixed, 1 `--check` found outputs, 2 unreadable file or not a notebook.

How to use it

1. Copy `scripts/strip_notebooks.py` and `.github/workflows/notebook-outputs.yml` into your repository. 2. Strip existing notebooks once and commit the result: ``` python3 scripts/strip_notebooks.py --fix python3 scripts/strip_notebooks.py --fix experiments/ notebooks/scratch.ipynb ``` 3. The workflow runs `--check` on pull requests that change `.ipynb` files. To limit it to certain folders, change the command to, for example, `python scripts/strip_notebooks.py --check experiments notebooks`. 4. Optional local hook so it happens automatically: put `python3 scripts/strip_notebooks.py --check` in `.git/hooks/pre-commit` and make the hook executable; run `--fix` when it fails.

Python 3.8+, no packages, no environment variables.

Files

Save each file at the path shown, relative to your project root.

  • .github/workflows/notebook-outputs.yml469 B
  • scripts/strip_notebooks.py4.8 KB

More automations

See all
  • Automation· EngineeringFree

    GitHub Actions check that JSON, JSONL and YAML files parse

    A CI workflow and a small Python script that fail a pull request when any JSON, JSONL or YAML file in the repo is malformed or a JSON object repeats a key. Built for repos full of prompt configs, eval sets and agent settings.

    Coding agents

    ALAISkillHub Library

Get AISkillHub updates in your inbox.

New features as they ship, AI live chat improvements, and the best new listings in the catalog.

Product updates, new features, and guides. Unsubscribe any time.