Python API for working with notebook files#

Reading and writing#

The reading functions require you to pass the as_version parameter. Your code should specify the notebook format that it knows how to work with: for instance, if your code handles version 4 notebooks:

nb = nbformat.read('path/to/notebook.ipynb', as_version=4)

This will automatically upgrade or downgrade notebooks in other versions of the notebook format to the structure your code knows about.

nbformat.NO_CONVERT#

This special value can be passed to the reading and writing functions, to indicate that the notebook should be loaded/saved in the format it’s supplied.

nbformat.current_nbformat#
nbformat.current_nbformat_minor#

These integers represent the current notebook format version that the nbformat module knows about.

NotebookNode objects#

The functions in this module work with NotebookNode objects, which are like dictionaries, but allow attribute access (nb.cells). The structure of these objects matches the notebook format described in The Notebook file format.

Other functions#

Constructing notebooks programmatically#

These functions return NotebookNode objects with the necessary fields.

nbformat.v4.new_notebook(**kwargs)#

Create a new notebook

nbformat.v4.new_code_cell(source='', **kwargs)#

Create a new code cell

nbformat.v4.new_markdown_cell(source='', **kwargs)#

Create a new markdown cell

nbformat.v4.new_raw_cell(source='', **kwargs)#

Create a new raw cell

nbformat.v4.new_output(output_type, data=None, **kwargs)#

Create a new output, to go in the cell.outputs list of a code cell.

nbformat.v4.output_from_msg(msg)#

Create a NotebookNode for an output from a kernel’s IOPub message.

Returns:

NotebookNode

Return type:

the output as a notebook node.

Raises:

ValueError – if the message is not an output message.:

Notebook signatures#

This machinery is used by the notebook web application to record which notebooks are trusted, and may show dynamic output as soon as they’re loaded. See server:server_security for more information.

Signature storage#

Signatures are stored using a pluggable SignatureStore subclass. To implement your own, override the methods below and configure NotebookNotary.store_factory.

By default, NotebookNotary will use an SQLite based store if SQLite bindings are available, and an in-memory store otherwise.