Skip to main content

CI PyPI

nb_helpers

A simple tool to clean, test and fix notebooks for your repo

Install

You can install from pypi:

pip install nb_helpers

or get latest:

pip install -e .

Usage

This little library gives you command line tools to clean, test and check your jupyter notebooks.

  • Clean: When you call clean_nbs it will strip notebooks from the metadata, this helps prevent git conflicts. You can also pass the flag --clear_outs and also remove cell outputs.
$ nb_helpers.clean_nbs --help                                                                                                                                   tcapelle at MBP14.local (-)(main)
usage: nb_helpers.clean_nbs [-h] [--path PATH] [--clear_outs] [--verbose]

Clean notebooks on `path` from useless metadata

options:
  -h, --help    show this help message and exit
  --path PATH   The path to notebooks (default: .)
  --clear_outs  Remove cell outputs (default: False)
  --verbose     Rnun on verbose mdoe (default: False)

You can run this comman on this repo:

$ nb_helpers.clean_nbs
> 
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┓                                                                     
┃ Notebook Path                                   ┃ Status ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━┩
│ tests/data/dummy_folder/fail_nb.ipynb           │ Ok✔    │
│ tests/data/dummy_folder/test_nb2.ipynb          │ Ok✔    │
│ tests/data/dummy_folder/test_nb_all_slow.ipynb  │ Ok✔    │
│ tests/data/dummy_folder/test_nb_some_slow.ipynb │ Ok✔    │
│ tests/data/features_nb.ipynb                    │ Ok✔    │
│ tests/data/test_nb.ipynb                        │ Ok✔    │
└─────────────────────────────────────────────────┴────────┘
  • Run: One can run the notebooks in path and get info about the execution.
$ nb_helpers.run_nbs --help                                                                                                                                tcapelle at MBP14 (--)(main)
usage: nb_helpers.run_nbs [-h] [--verbose] [--lib_name LIB_NAME] [--no_run] [--pip_install] [--github_issue] [--repo REPO] [--owner OWNER] [path]

positional arguments:
  path                 A path to nb files (default: /Users/tcapelle/wandb/nb_helpers)

options:
  -h, --help           show this help message and exit
  --verbose            Print errors along the way (default: False)
  --lib_name LIB_NAME  Python lib names to filter, eg: tensorflow
  --no_run             Do not run any notebook (default: False)
  --pip_install        Run cells with !pip install (default: False)
  --github_issue       Create a github issue if notebook fails (default: False)
  --repo REPO          Github repo to create issue in (default: nb_helpers)
  --owner OWNER        Github owner to create issue in (default: wandb)

You can now post github issues when running fails, the cool thing is that it can be posted to another repo than the one from the notebooks. Just pass the --repo name and the --owner (for example wandb/other_cool_repo)

You get the following output inside this repo:

$ nb_helpers.run_nbs
CONSOLE.is_terminal(): True
Writing output to run.csv
Notebook Path Status Run Time colab
dev_nbs/search.ipynb Fail 1 s open
tests/data/dummy_folder/fail_nb.ipynb Fail 1 s open
tests/data/dummy_folder/test_nb2.ipynb Ok 0 s open
tests/data/dummy_folder/test_nb_all_slow.ipynb Skipped 0 s open
tests/data/dummy_folder/test_nb_some_slow.ipynb Ok 0 s open
tests/data/features_nb.ipynb Ok 0 s open
tests/data/test_nb.ipynb Ok 0 s open
  • Summary: You can get a summary of the notebooks in your project with the nb_helpers.summary_nbs function.
$ nb_helpers.summary_nbs
CONSOLE.is_terminal(): True
Writing output to /Users/tcapelle/wandb/nb_helpers/logs/summary.csv
Reading 6 notebooks
┌───┬─────────────────────────────────────────────────┬────────────┬────────────────┬────────────────────────────────────────────────┬────────────┬───────┐
│ # │ nb name                                         │ tracker    │ wandb features │ python libs                                    │ colab_cell │ colab │
├───┼─────────────────────────────────────────────────┼────────────┼────────────────┼────────────────────────────────────────────────┼────────────┼───────┤
│ 1 │ tests/data/dummy_folder/fail_nb.ipynb           │            │                │                                                │            │ open  │
│ 2 │ tests/data/dummy_folder/test_nb2.ipynb          │            │                │                                                │            │ open  │
│ 3 │ tests/data/dummy_folder/test_nb_all_slow.ipynb  │            │                │ time                                           │            │ open  │
│ 4 │ tests/data/dummy_folder/test_nb_some_slow.ipynb │            │                │ time                                           │            │ open  │
│ 5 │ tests/data/features_nb.ipynb                    │            │                │ typing, itertools                              │            │ open  │
│ 6 │ tests/data/test_nb.ipynb                        │ 0: tracker │                │ os, sys, logging, pathlib, fastcore, itertools │ 1          │ open  │
└───┴─────────────────────────────────────────────────┴────────────┴────────────────┴────────────────────────────────────────────────┴────────────┴───────┘

Python Lib

All this functions can also be used inside python:

from pathlib import Path
from nb_helpers.run import run_nbs

examples_path = Path("examples/colabs")

errors = run_nbs(path=examples_path, verbose=True, timeout=600)

Also the library has many little functions to make your life easier inside the repo you are orchestrating:

from pathlib import Path
from nb_helpers.utils import *
from nb_helpers.colab import *

examples_path = Path("examples/colabs")

# get all nbs in the folder recursevely, filters hidden, non nb stuff
nb_files = find_nbs(example_path)

one_nb_path = nb_files[0]
notebook = read_nb(one_nb_path)

# get all libs imported
libs = detect_imported_libs(notebook)

# get remote github repo
github_repo = git_origin_repo(one_nb_path)

# detect if master is called main or master
master_name = git_main_name(one_nb_path)

# get colab link
colab_url = get_colab_url(one_nb_path, branch) 

Metadata

Release files for nb-helpers 0.6.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nb-helpers 0.6.2
File Size Uploaded Provenance Actions
nb_helpers-0.6.2.tar.gz 20.5 kB Without provenance Details for nb_helpers-0.6.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nb-helpers 0.6.2
File Interpreter ABI Platform Actions
nb_helpers-0.6.2-py3-none-any.whl Python 3 none any Details for nb_helpers-0.6.2-py3-none-any.whl

Total release size: 42.4 kB

Release files / nb_helpers-0.6.2.tar.gz

Download URL nb_helpers-0.6.2.tar.gz
Size 20.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1a99383e68f31cf12720386a76069b7afd273dc2f66c274a4eb4e81b94a11fe4
BLAKE2b-256 checksum
How to use checksums
3ea022b53253444c052a8b17ca750d4f1e370d98b9ffe13bae4ce4d75fcedaf9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / nb_helpers-0.6.2-py3-none-any.whl

Download URL nb_helpers-0.6.2-py3-none-any.whl
Size 21.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0b76640ef66a5d92b5ca4966c0620d3ec63d1087497b8e37804a09262ea75f44
BLAKE2b-256 checksum
How to use checksums
5f224b8b07c7922bd52eebc17b9727a590d6eb4fdc77128fa0eab97f72d34ef0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for nb-helpers 0.6.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.6.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page