Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/v2/nbbase.py: 10%
110 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
1"""The basic dict based notebook format.
3The Python representation of a notebook is a nested structure of
4dictionary subclasses that support attribute access.
5The functions in this module are merely
6helpers to build the structs in the right form.
8Authors:
10* Brian Granger
11"""
13# -----------------------------------------------------------------------------
14# Copyright (C) 2008-2011 The IPython Development Team
15#
16# Distributed under the terms of the BSD License. The full license is in
17# the file LICENSE, distributed as part of this software.
18# -----------------------------------------------------------------------------
20# -----------------------------------------------------------------------------
21# Imports
22# -----------------------------------------------------------------------------
24from nbformat._struct import Struct
26# -----------------------------------------------------------------------------
27# Code
28# -----------------------------------------------------------------------------
31class NotebookNode(Struct):
32 """A notebook node object."""
34 pass
37def from_dict(d):
38 """Create notebook node(s) from a value."""
39 if isinstance(d, dict):
40 newd = NotebookNode()
41 for k, v in d.items():
42 newd[k] = from_dict(v)
43 return newd
44 elif isinstance(d, (tuple, list)):
45 return [from_dict(i) for i in d]
46 else:
47 return d
50def new_output( # noqa
51 output_type=None,
52 output_text=None,
53 output_png=None,
54 output_html=None,
55 output_svg=None,
56 output_latex=None,
57 output_json=None,
58 output_javascript=None,
59 output_jpeg=None,
60 prompt_number=None,
61 etype=None,
62 evalue=None,
63 traceback=None,
64):
65 """Create a new code cell with input and output"""
66 output = NotebookNode()
67 if output_type is not None:
68 output.output_type = str(output_type)
70 if output_type != "pyerr":
71 if output_text is not None:
72 output.text = str(output_text)
73 if output_png is not None:
74 output.png = bytes(output_png)
75 if output_jpeg is not None:
76 output.jpeg = bytes(output_jpeg)
77 if output_html is not None:
78 output.html = str(output_html)
79 if output_svg is not None:
80 output.svg = str(output_svg)
81 if output_latex is not None:
82 output.latex = str(output_latex)
83 if output_json is not None:
84 output.json = str(output_json)
85 if output_javascript is not None:
86 output.javascript = str(output_javascript)
88 if output_type == "pyout" and prompt_number is not None:
89 output.prompt_number = int(prompt_number)
91 if output_type == "pyerr":
92 if etype is not None:
93 output.etype = str(etype)
94 if evalue is not None:
95 output.evalue = str(evalue)
96 if traceback is not None:
97 output.traceback = [str(frame) for frame in list(traceback)]
99 return output
102def new_code_cell(
103 input=None, prompt_number=None, outputs=None, language="python", collapsed=False # noqa
104):
105 """Create a new code cell with input and output"""
106 cell = NotebookNode()
107 cell.cell_type = "code"
108 if language is not None:
109 cell.language = str(language)
110 if input is not None:
111 cell.input = str(input)
112 if prompt_number is not None:
113 cell.prompt_number = int(prompt_number)
114 if outputs is None:
115 cell.outputs = []
116 else:
117 cell.outputs = outputs
118 if collapsed is not None:
119 cell.collapsed = bool(collapsed)
121 return cell
124def new_text_cell(cell_type, source=None, rendered=None):
125 """Create a new text cell."""
126 cell = NotebookNode()
127 if source is not None:
128 cell.source = str(source)
129 if rendered is not None:
130 cell.rendered = str(rendered)
131 cell.cell_type = cell_type
132 return cell
135def new_worksheet(name=None, cells=None):
136 """Create a worksheet by name with with a list of cells."""
137 ws = NotebookNode()
138 if name is not None:
139 ws.name = str(name)
140 if cells is None:
141 ws.cells = []
142 else:
143 ws.cells = list(cells)
144 return ws
147def new_notebook(metadata=None, worksheets=None):
148 """Create a notebook by name, id and a list of worksheets."""
149 nb = NotebookNode()
150 nb.nbformat = 2
151 if worksheets is None:
152 nb.worksheets = []
153 else:
154 nb.worksheets = list(worksheets)
155 if metadata is None:
156 nb.metadata = new_metadata()
157 else:
158 nb.metadata = NotebookNode(metadata)
159 return nb
162def new_metadata(
163 name=None, authors=None, license=None, created=None, modified=None, gistid=None # noqa
164):
165 """Create a new metadata node."""
166 metadata = NotebookNode()
167 if name is not None:
168 metadata.name = str(name)
169 if authors is not None:
170 metadata.authors = list(authors)
171 if created is not None:
172 metadata.created = str(created)
173 if modified is not None:
174 metadata.modified = str(modified)
175 if license is not None:
176 metadata.license = str(license)
177 if gistid is not None:
178 metadata.gistid = str(gistid)
179 return metadata
182def new_author(name=None, email=None, affiliation=None, url=None):
183 """Create a new author."""
184 author = NotebookNode()
185 if name is not None:
186 author.name = str(name)
187 if email is not None:
188 author.email = str(email)
189 if affiliation is not None:
190 author.affiliation = str(affiliation)
191 if url is not None:
192 author.url = str(url)
193 return author