Skip to main content

nbmanips allows you easily manipulate ipynb files

Project description

nbmanips

PyPI - License PyPI - Python Version PyPI - Wheel PyPI

A collections of utilities to manipulate IPython/Jupyter Notebooks via a python script.

Usage/Examples

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 cells
  • tag: Add metadata to the selected cells
  • erase: Erase the content of the selected cells
  • delete: Delete the selected cells
  • keep: Kepp the selected cells

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.show(selected_cells)
  • A predefined selector. Available predefined selectors are the following:

    • code_cells / markdown_cells / raw_cells: Selects cells with the given type
    • contains: Selects Cells containing a certain text.
    • is_empty / empty: Selects empty cells
    • has_output: Checks if the cell has any output
    • has_output_type: Select cells that have a given output_type
    • has_slide_type: Select cells that have a given slide type
    • is_new_slide: Selects cells where a new slide/subslide starts
# 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()

Export Formats

You can export the notebooks to these formats:

  • to_ipynb
  • to_html
  • to_slides (using reveal.js)
  • to_md (to markdown)
  • to_py (to python)
  • to_text (textual representation of the notebook)

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')

CLI

Show a notebook

To get a readable representation of the notebook

nb show 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

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

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

Slide manipulations

# Automatically set slides
nb auto-slide 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

Roadmap

  • Add Custom Templates

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nbmanips-1.1.0.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

nbmanips-1.1.0-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file nbmanips-1.1.0.tar.gz.

File metadata

  • Download URL: nbmanips-1.1.0.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for nbmanips-1.1.0.tar.gz
Algorithm Hash digest
SHA256 0c825a0b1b45c484bebdc55744ac83237837691ce0dde2be7efdc7d8b4f5cf61
MD5 12541135525446dbd0cbd81db8fd36bf
BLAKE2b-256 9ea00bfda1aee81efb3e86a74a4d577260d856a5cf61c0e7fe632a64a9e8e7f8

See more details on using hashes here.

File details

Details for the file nbmanips-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: nbmanips-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for nbmanips-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 573c0904ea382d68eb4d37e4e44323b35e66c4d8ec1460a4e15eb344e84f1f36
MD5 29896ca045ef9d7484956b5543fe02a3
BLAKE2b-256 df01bbbc2916f496757d9e531beb274fc5075fbb3ace963efd1c62613b697081

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page