Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/v4/nbbase.py: 20%
61 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""Python API for composing notebook elements
3The Python representation of a notebook is a nested structure of
4dictionary subclasses that support attribute access.
5The functions in this module are merely helpers to build the structs
6in the right form.
7"""
9# Copyright (c) IPython Development Team.
10# Distributed under the terms of the Modified BSD License.
12from nbformat.corpus.words import generate_corpus_id as random_cell_id
13from nbformat.notebooknode import NotebookNode
15# Change the nbformat_minor and nbformat_schema variables when incrementing the
16# nbformat version
18# current major version
19nbformat = 4
21# current minor version
22nbformat_minor = 5
24# schema files for (major, minor) version tuples. (None, None) means the current version
25nbformat_schema = {
26 (None, None): "nbformat.v4.schema.json",
27 (4, 0): "nbformat.v4.0.schema.json",
28 (4, 1): "nbformat.v4.1.schema.json",
29 (4, 2): "nbformat.v4.2.schema.json",
30 (4, 3): "nbformat.v4.3.schema.json",
31 (4, 4): "nbformat.v4.4.schema.json",
32 (4, 5): "nbformat.v4.5.schema.json",
33}
36def validate(node, ref=None):
37 """validate a v4 node"""
38 from nbformat import validate as validate_orig
40 return validate_orig(node, ref=ref, version=nbformat)
43def new_output(output_type, data=None, **kwargs):
44 """Create a new output, to go in the ``cell.outputs`` list of a code cell."""
45 output = NotebookNode(output_type=output_type)
47 # populate defaults:
48 if output_type == "stream":
49 output.name = "stdout"
50 output.text = ""
51 elif output_type == "display_data":
52 output.metadata = NotebookNode()
53 output.data = NotebookNode()
54 elif output_type == "execute_result":
55 output.metadata = NotebookNode()
56 output.data = NotebookNode()
57 output.execution_count = None
58 elif output_type == "error":
59 output.ename = "NotImplementedError"
60 output.evalue = ""
61 output.traceback = []
63 # load from args:
64 output.update(kwargs)
65 if data is not None:
66 output.data = data
67 # validate
68 validate(output, output_type)
69 return output
72def output_from_msg(msg):
73 """Create a NotebookNode for an output from a kernel's IOPub message.
75 Returns
76 -------
77 NotebookNode: the output as a notebook node.
79 Raises
80 ------
81 ValueError: if the message is not an output message.
83 """
84 msg_type = msg["header"]["msg_type"]
85 content = msg["content"]
87 if msg_type == "execute_result":
88 return new_output(
89 output_type=msg_type,
90 metadata=content["metadata"],
91 data=content["data"],
92 execution_count=content["execution_count"],
93 )
94 elif msg_type == "stream":
95 return new_output(
96 output_type=msg_type,
97 name=content["name"],
98 text=content["text"],
99 )
100 elif msg_type == "display_data":
101 return new_output(
102 output_type=msg_type,
103 metadata=content["metadata"],
104 data=content["data"],
105 )
106 elif msg_type == "error":
107 return new_output(
108 output_type=msg_type,
109 ename=content["ename"],
110 evalue=content["evalue"],
111 traceback=content["traceback"],
112 )
113 else:
114 raise ValueError("Unrecognized output msg type: %r" % msg_type)
117def new_code_cell(source="", **kwargs):
118 """Create a new code cell"""
119 cell = NotebookNode(
120 id=random_cell_id(),
121 cell_type="code",
122 metadata=NotebookNode(),
123 execution_count=None,
124 source=source,
125 outputs=[],
126 )
127 cell.update(kwargs)
129 validate(cell, "code_cell")
130 return cell
133def new_markdown_cell(source="", **kwargs):
134 """Create a new markdown cell"""
135 cell = NotebookNode(
136 id=random_cell_id(),
137 cell_type="markdown",
138 source=source,
139 metadata=NotebookNode(),
140 )
141 cell.update(kwargs)
143 validate(cell, "markdown_cell")
144 return cell
147def new_raw_cell(source="", **kwargs):
148 """Create a new raw cell"""
149 cell = NotebookNode(
150 id=random_cell_id(),
151 cell_type="raw",
152 source=source,
153 metadata=NotebookNode(),
154 )
155 cell.update(kwargs)
157 validate(cell, "raw_cell")
158 return cell
161def new_notebook(**kwargs):
162 """Create a new notebook"""
163 nb = NotebookNode(
164 nbformat=nbformat,
165 nbformat_minor=nbformat_minor,
166 metadata=NotebookNode(),
167 cells=[],
168 )
169 nb.update(kwargs)
170 validate(nb)
171 return nb