Clean Jupyter notebooks for LLMs and lightweight sharing.
Project description
ipynbcleaner
ipynbcleaner is a lightweight, zero-dependency Python utility designed to strip noise, outputs, and metadata from Jupyter Notebooks (.ipynb). It is optimized for preparing notebooks for Large Language Model (LLM) context windows, clean Git version control diffs, and lightweight sharing.
By removing unnecessary metadata and attachments, and optional cell outputs, ipynbcleaner reduces file sizes drastically (often by 70% to 90% or more), saving LLM tokens and keeping your repositories tidy.
Key Features
- ⚡ Zero-Dependency: Does not require
nbformator any other external library. It works purely with Python's built-injsonmodule. - 🧹 Noisy Metadata Stripping: Removes notebook-level, cell-level, and execution-level metadata by default.
- 📉 Rich CLI Metrics: Prints a clean, visual summary of the cleaning process, showing file size reduction percentage, a terminal progress bar, and notebook structure statistics.
- 💾 Smart Output Options: Keeps only the last output of code cells by default (allowing LLMs to see the result of execution), or strips all outputs entirely.
- 🐍 Flexible API: Easy to integrate directly into Python scripts or CI/CD pipelines.
Installation
Install directly from PyPI:
pip install ipynbcleaner
Usage
Command Line Interface (CLI)
# Clean a notebook (saves output to input_clean.ipynb by default)
ipynbcleaner notebook.ipynb
# Clean a notebook and specify the output destination
ipynbcleaner notebook.ipynb cleaned_output.ipynb
# Clean and strip all outputs entirely
ipynbcleaner notebook.ipynb --drop-outputs
CLI Options
input: Path to the.ipynbnotebook file to clean (Required).output: Optional path to save the cleaned notebook. Defaults to[input_stem]_clean.ipynb.--drop-outputs: Remove all code cell outputs entirely (replaces last-output-only default behavior).--keep-execution-count: Do not clear execution counts (e.g.[1],[2]).--keep-root-metadata: Preserve notebook-level metadata.--keep-cell-metadata: Preserve individual cell metadata.--keep-cell-ids: Preserve cell IDs.--keep-attachments: Preserve cell attachments (markdown images, etc.).--indent: Specify JSON indentation level (defaults to2).
Example CLI Output
Whenever you clean a notebook using the CLI, a beautiful summary is printed in the terminal:
Cleaning Summary:
--------------------------------------------------
File sizes:
Original: 1.24 MB
Cleaned: 143.52 KB
Reduction: [██████████████████░░] 88.67% (-1.10 MB)
Notebook structure:
Total cells: 32
Code cells: 18
Markdown cells: 14
Lines of code: 247
--------------------------------------------------
Cleaned notebook saved to: notebook_clean.ipynb
Python API
You can import and use ipynbcleaner in your own Python scripts:
from ipynbcleaner import CleanOptions, clean_notebook_file
# Clean file using default options (retains the last output per code cell)
clean_notebook_file("raw_notebook.ipynb", "clean_notebook.ipynb")
# Clean file and drop all outputs
options = CleanOptions(keep_last_output=False)
clean_notebook_file("raw_notebook.ipynb", "clean_notebook.ipynb", options=options)
CleanOptions Reference
| Option | Type | Default | Description |
|---|---|---|---|
keep_last_output |
bool |
True |
Retains only the final output of code cells. If False, all outputs are stripped. |
keep_execution_count |
bool |
False |
Preserves code execution counters. |
keep_root_metadata |
bool |
False |
Preserves notebook-level metadata. |
keep_cell_metadata |
bool |
False |
Preserves cell-level metadata. |
keep_cell_ids |
bool |
False |
Preserves cell IDs. |
keep_attachments |
bool |
False |
Preserves attachments (e.g., embedded markdown images). |
indent |
int |
2 |
JSON indentation level for output. |
Contributing
Contributions are welcome! Please feel free to open issues, submit pull requests, or request new features on our GitHub Repository.
- Fork the Project.
- Create your Feature Branch (
git checkout -b feature/AmazingFeature). - Commit your Changes (
git commit -m 'feat: add some AmazingFeature'). - Push to the Branch (
git push origin feature/AmazingFeature). - Open a Pull Request.
License
Distributed under the MIT License. See LICENSE for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ipynbcleaner-0.2.0.tar.gz.
File metadata
- Download URL: ipynbcleaner-0.2.0.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad270bf5af40ee4b5e4aa17bc30303bc3ed1df23d716fb9f4656d2a7eef091f5
|
|
| MD5 |
20569e11c8d7ec0c7fbbfda64d531d9e
|
|
| BLAKE2b-256 |
a737294a06e5e73f9d8f74a0bfe0bbfeeb6925b0de6bc7402ca44a3012966814
|
Provenance
The following attestation bundles were made for ipynbcleaner-0.2.0.tar.gz:
Publisher:
ci.yml on DanielDPereira/ipynbCleaner
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ipynbcleaner-0.2.0.tar.gz -
Subject digest:
ad270bf5af40ee4b5e4aa17bc30303bc3ed1df23d716fb9f4656d2a7eef091f5 - Sigstore transparency entry: 2088932267
- Sigstore integration time:
-
Permalink:
DanielDPereira/ipynbCleaner@94383ba0d5663d99a9d5b7d0911262ab9e300904 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/DanielDPereira
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@94383ba0d5663d99a9d5b7d0911262ab9e300904 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ipynbcleaner-0.2.0-py3-none-any.whl.
File metadata
- Download URL: ipynbcleaner-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5723f834044aec4adc82cc1509d445bf7901cd292b00d291e4fca3602522767
|
|
| MD5 |
bbbf2e8cd4b2479ef77808e0a35382b7
|
|
| BLAKE2b-256 |
8a427b4ebb0948b9044d8fb1154e4f288ee4eebc3b5801cfbfb915fd32c7dd08
|
Provenance
The following attestation bundles were made for ipynbcleaner-0.2.0-py3-none-any.whl:
Publisher:
ci.yml on DanielDPereira/ipynbCleaner
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ipynbcleaner-0.2.0-py3-none-any.whl -
Subject digest:
c5723f834044aec4adc82cc1509d445bf7901cd292b00d291e4fca3602522767 - Sigstore transparency entry: 2088932485
- Sigstore integration time:
-
Permalink:
DanielDPereira/ipynbCleaner@94383ba0d5663d99a9d5b7d0911262ab9e300904 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/DanielDPereira
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@94383ba0d5663d99a9d5b7d0911262ab9e300904 -
Trigger Event:
push
-
Statement type: