nbmanips
A collections of utilities to manipulate IPython/Jupyter Notebooks via a python script.
I - Usage/Examples
1 - Basic usage
A simple example of using nbmanips:
from nbmanips import Notebook
# Read ipynb file
nb = Notebook.read_ipynb("my_notebook.ipynb")
# delete empty cells
nb.select("empty").delete()
# save ipynb file
nb.to_ipynb("new_notebook.ipynb")
Examples of operations you can perform on a Notebook:
replace: Replace matching text in the selected cellstag: Add metadata to the selected cellserase: Erase the content of the selected cellsdelete: Delete the selected cellskeep: Kepp the selected cells
2 - Selectors
To select cells on which to apply the previous operations, you can use:
- The cell number
nb[0].show()
# OR
nb.select(0).show()
- A slice object
nb[1:6:2].show()
# OR
selected_cells = slice(1, 6, 2)
nb.select(selected_cells).show()
-
A predefined selector. Available predefined selectors are the following:
code_cells/markdown_cells/raw_cells: Selects cells with the given typecontains: Selects Cells containing a certain text.is_empty/empty: Selects empty cellshas_output: Checks if the cell has any outputhas_output_type: Select cells that have a given output_typehas_slide_type: Select cells that have a given slide typeis_new_slide: Selects cells where a new slide/subslide startshas_byte_size: Selects cells with byte size within a given range of values.
# Show Markdown Cells
nb.select('markdown_cells').show()
# Show Cells containing the equal sign
nb.select('contains', '=').show()
- A function that takes a Cell object and returns True if the cell should be selected
# Show Cells with length > 10
nb.select(lambda cell: len(cell.source) > 10).show()
- A list of Selectors
# Show Empty Markdown Cells
nb.select(['markdown_cells', 'is_empty']).show()
# Show Markdown or Code Cells
nb.select(['markdown_cells', 'code_cells'], type='or').show()
3 - Export Formats
You can export the notebooks to these formats:
- to_ipynb
- to_dbc
- to_html
- to_slides (using reveal.js)
- to_md (to markdown)
- to_py (to python)
- to_text (textual representation of the notebook)
4 - Slide manipulations
You can manipulate the slides by tagging which cells to keep and which to skip. The following actions are available:
- set_slide
- set_subslide
- set_skip
- set_fragment
- set_notes
A neat trick is to use auto_slide method to automatically create slides out of your notebook:
from nbmanips import Notebook
# Read ipynb file
nb = Notebook.read_ipynb("my_notebook.ipynb")
# Automatically create slides
nb.auto_slide()
# Export to Reveal.js slides (HTML)
nb.to_slides("new_slides.slides.html", theme='beige')
II - CLI
1 - Show a notebook
To get a readable representation of the notebook
nb show my_notebook.ipynb
Other options are available. For example, you can customize the style, weather to truncate the output of cells:
nb show -s double -t 100 my_notebook.ipynb
To show a subset of the notebook cells, you can perform a select operation:
nb select 0:3 | nb show my_notebook.ipynb
# Or if you're using negative indexes ( to show the last 3 cells )
nb select [-3:] | nb show my_notebook.ipynb
2 - Basic usage
A simple example of using nbmanips via the cli:
# delete empty cells
nb select empty | nb delete my_notebook.ipynb --output new_notebook.ipynb
# Or equivalently:
nbmanips select empty | nbmanips delete my_notebook.ipynb --output new_notebook.ipynb
You could also show the table of contents of a certain notebook:
nb toc nb.ipynb
Or split a notebook into multiple notebooks:
nb split nb.ipynb 5,9
3 - Export Formats
You can convert a notebook to the following formats:
- html:
nb convert html my_notebook.ipynb --output my_notebook.html - slides (using reveal.js):
nb convert slides my_notebook.ipynb --output my_notebook.slides.html - md (to markdown):
nb convert md my_notebook.ipynb --output my_notebook.md - py (to python):
nb convert py my_notebook.ipynb --output my_notebook.py
4 - Slide manipulations
# Automatically set slides
nb auto-slide -f my_notebook.ipynb
# Generate a my_notebook.slides.html file
nb convert slides my_notebook.ipynb
Or if you do not wish to modify your original notebook:
# Automatically set slides
nb auto-slide my_notebook.ipynb -o my_temp_notebook.ipynb
# Generate a my_notebook.slides.html file
nb convert slides my_temp_notebook.ipynb -o my_notebook.slides.html
If you need more details you can check the --help option:
nbmanips --help
III - Optional Requirements
There are optional requirements you may want to install to render images in the terminal. The results, however, are not always convincing. If you want to enable this feature, you can just run the following command:
pip install nbmanips[images]
Roadmap
- Add Custom Templates
Metadata
Release files for nbmanips 2.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| nbmanips-2.1.3.tar.gz | 33.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nbmanips-2.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 75.0 kB
Release files / nbmanips-2.1.3.tar.gz
| Download URL | nbmanips-2.1.3.tar.gz |
|---|---|
| Size | 33.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
413dcf330c022d3b9a33767ed9b130b1e2fce42eac08b22b2dd32cb54718d0ec
|
|
BLAKE2b-256 checksum How to use checksums |
a0015c6df54130ed41c8730fe554adf69fabdbf26b6bfb4f55aac97e16721ed4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 5, 2025.
Transparency logRelease files / nbmanips-2.1.3-py3-none-any.whl
| Download URL | nbmanips-2.1.3-py3-none-any.whl |
|---|---|
| Size | 41.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4b63ebd6a6564140510977ed0c6fafb7a090420e572f50f8acfa2bb6cd2b4a4d
|
|
BLAKE2b-256 checksum How to use checksums |
27bebf3b6a9ba62f380e28b87b6ce482d2fdb535f1da91ec87903355bc5f6584
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 5, 2025.
Transparency log