1"""The basic dict based notebook format.
2
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.
7
8Authors:
9
10* Brian Granger
11"""
12
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# -----------------------------------------------------------------------------
19
20# -----------------------------------------------------------------------------
21# Imports
22# -----------------------------------------------------------------------------
23from __future__ import annotations
24
25from nbformat._struct import Struct
26
27# -----------------------------------------------------------------------------
28# Code
29# -----------------------------------------------------------------------------
30
31
32class NotebookNode(Struct):
33 """A notebook node object."""
34
35
36def from_dict(d):
37 """Create notebook node(s) from a value."""
38 if isinstance(d, dict):
39 newd = NotebookNode()
40 for k, v in d.items():
41 newd[k] = from_dict(v)
42 return newd
43 if isinstance(d, (tuple, list)):
44 return [from_dict(i) for i in d]
45 return d
46
47
48def new_output(
49 output_type=None,
50 output_text=None,
51 output_png=None,
52 output_html=None,
53 output_svg=None,
54 output_latex=None,
55 output_json=None,
56 output_javascript=None,
57 output_jpeg=None,
58 prompt_number=None,
59 etype=None,
60 evalue=None,
61 traceback=None,
62):
63 """Create a new code cell with input and output"""
64 output = NotebookNode()
65 if output_type is not None:
66 output.output_type = str(output_type)
67
68 if output_type != "pyerr":
69 if output_text is not None:
70 output.text = str(output_text)
71 if output_png is not None:
72 output.png = bytes(output_png)
73 if output_jpeg is not None:
74 output.jpeg = bytes(output_jpeg)
75 if output_html is not None:
76 output.html = str(output_html)
77 if output_svg is not None:
78 output.svg = str(output_svg)
79 if output_latex is not None:
80 output.latex = str(output_latex)
81 if output_json is not None:
82 output.json = str(output_json)
83 if output_javascript is not None:
84 output.javascript = str(output_javascript)
85
86 if output_type == "pyout" and prompt_number is not None:
87 output.prompt_number = int(prompt_number)
88
89 if output_type == "pyerr":
90 if etype is not None:
91 output.etype = str(etype)
92 if evalue is not None:
93 output.evalue = str(evalue)
94 if traceback is not None:
95 output.traceback = [str(frame) for frame in list(traceback)]
96
97 return output
98
99
100def new_code_cell(
101 input=None,
102 prompt_number=None,
103 outputs=None,
104 language="python",
105 collapsed=False,
106):
107 """Create a new code cell with input and output"""
108 cell = NotebookNode()
109 cell.cell_type = "code"
110 if language is not None:
111 cell.language = str(language)
112 if input is not None:
113 cell.input = str(input)
114 if prompt_number is not None:
115 cell.prompt_number = int(prompt_number)
116 if outputs is None:
117 cell.outputs = []
118 else:
119 cell.outputs = outputs
120 if collapsed is not None:
121 cell.collapsed = bool(collapsed)
122
123 return cell
124
125
126def new_text_cell(cell_type, source=None, rendered=None):
127 """Create a new text cell."""
128 cell = NotebookNode()
129 if source is not None:
130 cell.source = str(source)
131 if rendered is not None:
132 cell.rendered = str(rendered)
133 cell.cell_type = cell_type
134 return cell
135
136
137def new_worksheet(name=None, cells=None):
138 """Create a worksheet by name with with a list of cells."""
139 ws = NotebookNode()
140 if name is not None:
141 ws.name = str(name)
142 if cells is None:
143 ws.cells = []
144 else:
145 ws.cells = list(cells)
146 return ws
147
148
149def new_notebook(metadata=None, worksheets=None):
150 """Create a notebook by name, id and a list of worksheets."""
151 nb = NotebookNode()
152 nb.nbformat = 2
153 if worksheets is None:
154 nb.worksheets = []
155 else:
156 nb.worksheets = list(worksheets)
157 if metadata is None:
158 nb.metadata = new_metadata()
159 else:
160 nb.metadata = NotebookNode(metadata)
161 return nb
162
163
164def new_metadata(
165 name=None,
166 authors=None,
167 license=None,
168 created=None,
169 modified=None,
170 gistid=None,
171):
172 """Create a new metadata node."""
173 metadata = NotebookNode()
174 if name is not None:
175 metadata.name = str(name)
176 if authors is not None:
177 metadata.authors = list(authors)
178 if created is not None:
179 metadata.created = str(created)
180 if modified is not None:
181 metadata.modified = str(modified)
182 if license is not None:
183 metadata.license = str(license)
184 if gistid is not None:
185 metadata.gistid = str(gistid)
186 return metadata
187
188
189def new_author(name=None, email=None, affiliation=None, url=None):
190 """Create a new author."""
191 author = NotebookNode()
192 if name is not None:
193 author.name = str(name)
194 if email is not None:
195 author.email = str(email)
196 if affiliation is not None:
197 author.affiliation = str(affiliation)
198 if url is not None:
199 author.url = str(url)
200 return author