Library Usage
nbtoolbelt
can be used in Python 3 programs as a library.
Example
An example script using nbtoolbelt as library
:
#! /usr/bin/env python
"""
Example script that illustrates use of nbtoolbelt as a library
It expects one argument on the command-line, opens it as a Jupyter notebook, and
shows its global metadata and counts per cell type.
"""
import sys
import nbformat
import nbtoolbelt as nbtb
def main() -> int:
"""Main entry point.
:return: exit code
"""
try:
nb = nbformat.read(sys.argv[1], as_version=4)
except Exception as e:
print('{}: {}'.format(type(e).__name__, e), sys.stderr)
return 1
nbtb.print_dict(nbtb.nb_metadata(nb), 'Notebook metadata')
nbtb.print_dict(nbtb.nb_cell_stats(nb)['cell_types'], 'Cell types')
return 0
if __name__ == '__main__':
sys.exit(main())
Here is a sample run of this script:
$ example.py test.ipynb
Notebook metadata:
4.2 format version
python3 kernel
python 3.6.1 language
Cell types:
12 code
7 markdown
19 total cell count