1"""The basic dict based notebook format.
2
3Authors:
4
5* Brian Granger
6"""
7
8# -----------------------------------------------------------------------------
9# Copyright (C) 2008-2011 The IPython Development Team
10#
11# Distributed under the terms of the BSD License. The full license is in
12# the file LICENSE, distributed as part of this software.
13# -----------------------------------------------------------------------------
14
15# -----------------------------------------------------------------------------
16# Imports
17# -----------------------------------------------------------------------------
18from __future__ import annotations
19
20from nbformat._struct import Struct
21
22# -----------------------------------------------------------------------------
23# Code
24# -----------------------------------------------------------------------------
25
26
27class NotebookNode(Struct):
28 """A notebook node object."""
29
30
31def from_dict(d):
32 """Create notebook node(s) from an object."""
33 if isinstance(d, dict):
34 newd = NotebookNode()
35 for k, v in d.items():
36 newd[k] = from_dict(v)
37 return newd
38 if isinstance(d, (tuple, list)):
39 return [from_dict(i) for i in d]
40 return d
41
42
43def new_code_cell(code=None, prompt_number=None):
44 """Create a new code cell with input and output"""
45 cell = NotebookNode()
46 cell.cell_type = "code"
47 if code is not None:
48 cell.code = str(code)
49 if prompt_number is not None:
50 cell.prompt_number = int(prompt_number)
51 return cell
52
53
54def new_text_cell(text=None):
55 """Create a new text cell."""
56 cell = NotebookNode()
57 if text is not None:
58 cell.text = str(text)
59 cell.cell_type = "text"
60 return cell
61
62
63def new_notebook(cells=None):
64 """Create a notebook by name, id and a list of worksheets."""
65 nb = NotebookNode()
66 if cells is not None:
67 nb.cells = cells
68 else:
69 nb.cells = []
70 return nb