pytest_notebook.notebook module¶
Module for working with notebook.
-
class
pytest_notebook.notebook.
MetadataConfig
(diff_replace: tuple = (), diff_ignore: set = 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: Falseskip_reason (str) – Reason for skipping testing of this notebook. This value is accessible, after initialization, via the
skip_reason
attribute. Default:
-
diff_ignore
¶
-
diff_replace
¶
-
skip
¶
-
skip_reason
¶
-
exception
pytest_notebook.notebook.
NBConfigValidationError
[source]¶ Bases:
Exception
Exception to signal a validation error in the notebook metadata.
-
pytest_notebook.notebook.
config_from_metadata
(nb: nbformat.notebooknode.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) → nbformat.notebooknode.NotebookNode[source]¶ Create a new notebook cell.
-
pytest_notebook.notebook.
dump_notebook
(nb: nbformat.notebooknode.NotebookNode, as_version=nbformat.NO_CONVERT) → nbformat.notebooknode.NotebookNode[source]¶ Dump the notebook to a sting.
-
pytest_notebook.notebook.
gather_json_paths
(obj: Any, paths: list, types: Optional[Tuple] = 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: Union[TextIO, str], as_version=4) → nbformat.notebooknode.NotebookNode[source]¶ Load the notebook from file.
-
pytest_notebook.notebook.
load_notebook_with_config
(path: Union[TextIO, str], as_version=4) → Tuple[nbformat.notebooknode.NotebookNode, pytest_notebook.notebook.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: Optional[Callable] = None) → dict[source]¶ Recursively convert mappable objects to dicts, including in lists and tuples.
-
pytest_notebook.notebook.
prepare_cell
(cell: dict, as_version=4) → nbformat.notebooknode.NotebookNode[source]¶ Convert raw (disc-format) notebook cell to a
NotebookNode
.
-
pytest_notebook.notebook.
prepare_nb
(dct: dict, as_version=4) → nbformat.notebooknode.NotebookNode[source]¶ Convert raw (disc-format) notebook to a
NotebookNode
.
-
pytest_notebook.notebook.
regex_replace_nb
(notebook: nbformat.notebooknode.NotebookNode, replacements: Tuple[Tuple[str, str, str]]) → nbformat.notebooknode.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