pytest_notebook.notebook module#

Module for working with notebook.

class pytest_notebook.notebook.MetadataConfig(diff_replace: tuple = (), diff_ignore: set = _Nothing.NOTHING, skip: bool = False, skip_reason: str = '')[source]#

Bases: object

A class to store configuration data, obtained from the notebook metadata.

Parameters:
  • diff_replace (tuple) – Notebook paths to regex replace before diffing. This value is accessible, after initialization, via the diff_replace attribute. Default: ()

  • diff_ignore (set) – Notebook paths to ignore during diffing. This value is accessible, after initialization, via the diff_ignore attribute. Default: Factory(factory=<class ‘set’>, takes_self=False)

  • skip (bool) – Skip testing of this notebook. This value is accessible, after initialization, via the skip attribute. Default: False

  • skip_reason (str) – Reason for skipping testing of this notebook. This value is accessible, after initialization, via the skip_reason attribute. Default:

_validate_diff_ignore(attribute, values)[source]#
_validate_diff_replace(attribute, values)[source]#
diff_ignore: set#
diff_replace: tuple#
skip: bool#
skip_reason: str#
exception pytest_notebook.notebook.NBConfigValidationError[source]#

Bases: Exception

Exception to signal a validation error in the notebook metadata.

pytest_notebook.notebook._load_validator()[source]#
pytest_notebook.notebook.config_from_metadata(nb: NotebookNode) dict[source]#

Extract configuration data from notebook/cell metadata.

pytest_notebook.notebook.create_cell(source: str = '', cell_type: str = 'code', as_version: int = 4, **kwargs) NotebookNode[source]#

Create a new notebook cell.

pytest_notebook.notebook.create_notebook(as_version: int = 4, **kwargs)[source]#

Create a new notebook.

pytest_notebook.notebook.dump_notebook(nb: NotebookNode, as_version=nbformat.NO_CONVERT) NotebookNode[source]#

Dump the notebook to a string.

pytest_notebook.notebook.gather_json_paths(obj: Any, paths: list, types: Tuple | None = None, curr_path: tuple = ()) Any[source]#

Recursively gather paths to non dict/list elements of a json-like object.

Parameters:
  • paths – a mutable container for the paths

  • types – only return paths for these element types

Examples

>> dct = {“a”: [{“b”: 2}, {“c”: “x”}]} >> paths = [] >> gather_json_paths(dct, paths) >> paths [(‘a’, 0, ‘b’), (‘a’, 1, ‘c’)] >> paths = [] >> gather_json_paths(dct, paths, (str,)) >> paths [(‘a’, 1, ‘c’)]

pytest_notebook.notebook.load_notebook(path: TextIO | str, as_version=4) NotebookNode[source]#

Load the notebook from file.

pytest_notebook.notebook.load_notebook_with_config(path: TextIO | str, as_version=4) Tuple[NotebookNode, MetadataConfig][source]#

Load the notebook from file, and scan its metadata for config data.

pytest_notebook.notebook.mapping_to_dict(obj: Any, strip_keys: list = (), leaf_func: Callable | None = None) dict[source]#

Recursively convert mappable objects to dicts, including in lists and tuples.

Parameters:
  • strip_keys (list[str]) – list of keys to strip from the output

  • leaf_func – a function to apply to leaf values

pytest_notebook.notebook.prepare_cell(cell: dict, as_version=4) NotebookNode[source]#

Convert raw (disc-format) notebook cell to a NotebookNode.

pytest_notebook.notebook.prepare_nb(dct: dict, as_version=4) NotebookNode[source]#

Convert raw (disc-format) notebook to a NotebookNode.

pytest_notebook.notebook.regex_replace_nb(notebook: NotebookNode, replacements: Tuple[Tuple[str, str, str]]) NotebookNode[source]#

Return a new notebook with string regex replacements applied.

Parameters:

replacements – list of (path, regex, replacement), path is a string of form ‘/cells/0/outputs’, and can contain * wildcards for integer parts

pytest_notebook.notebook.validate_metadata(data, path)[source]#

Validate notebook and cell metadata against the required config schema.

Raises:

NBRegressionError – if validation fails

pytest_notebook.notebook.validate_regex_replace(args, index)[source]#

Validate a single regex replace item.

Should be of the form (<nb_path>, <regex_pattern>, <replacement>)