Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/v1/nbbase.py: 22%
32 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.
3Authors:
5* Brian Granger
6"""
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# -----------------------------------------------------------------------------
15# -----------------------------------------------------------------------------
16# Imports
17# -----------------------------------------------------------------------------
19from nbformat._struct import Struct
21# -----------------------------------------------------------------------------
22# Code
23# -----------------------------------------------------------------------------
26class NotebookNode(Struct):
27 """A notebook node object."""
29 pass
32def from_dict(d):
33 """Create notebook node(s) from an object."""
34 if isinstance(d, dict):
35 newd = NotebookNode()
36 for k, v in d.items():
37 newd[k] = from_dict(v)
38 return newd
39 elif isinstance(d, (tuple, list)):
40 return [from_dict(i) for i in d]
41 else:
42 return d
45def new_code_cell(code=None, prompt_number=None):
46 """Create a new code cell with input and output"""
47 cell = NotebookNode()
48 cell.cell_type = "code"
49 if code is not None:
50 cell.code = str(code)
51 if prompt_number is not None:
52 cell.prompt_number = int(prompt_number)
53 return cell
56def new_text_cell(text=None):
57 """Create a new text cell."""
58 cell = NotebookNode()
59 if text is not None:
60 cell.text = str(text)
61 cell.cell_type = "text"
62 return cell
65def new_notebook(cells=None):
66 """Create a notebook by name, id and a list of worksheets."""
67 nb = NotebookNode()
68 if cells is not None:
69 nb.cells = cells
70 else:
71 nb.cells = []
72 return nb