Show More
@@ -6,9 +6,6 b'' | |||||
6 | from .nbbase import ( |
|
6 | from .nbbase import ( | |
7 | NotebookNode, |
|
7 | NotebookNode, | |
8 | nbformat, nbformat_minor, nbformat_schema, |
|
8 | nbformat, nbformat_minor, nbformat_schema, | |
9 | ) |
|
|||
10 |
|
||||
11 | from .compose import ( |
|
|||
12 | new_code_cell, new_heading_cell, new_markdown_cell, new_notebook, |
|
9 | new_code_cell, new_heading_cell, new_markdown_cell, new_notebook, | |
13 | new_output, |
|
10 | new_output, | |
14 | ) |
|
11 | ) |
@@ -1,4 +1,4 b'' | |||||
1 | """The basic dict based notebook format. |
|
1 | """Python API for composing notebook elements | |
2 |
|
2 | |||
3 | The Python representation of a notebook is a nested structure of |
|
3 | The Python representation of a notebook is a nested structure of | |
4 | dictionary subclasses that support attribute access |
|
4 | dictionary subclasses that support attribute access | |
@@ -9,22 +9,21 b' helpers to build the structs in the right form.' | |||||
9 | # Copyright (c) IPython Development Team. |
|
9 | # Copyright (c) IPython Development Team. | |
10 | # Distributed under the terms of the Modified BSD License. |
|
10 | # Distributed under the terms of the Modified BSD License. | |
11 |
|
11 | |||
12 | import pprint |
|
|||
13 | import uuid |
|
|||
14 |
|
||||
15 | from IPython.utils.ipstruct import Struct |
|
12 | from IPython.utils.ipstruct import Struct | |
16 | from IPython.utils.py3compat import cast_unicode, unicode_type |
|
|||
17 |
|
||||
18 |
|
13 | |||
19 | # Change this when incrementing the nbformat version |
|
14 | # Change this when incrementing the nbformat version | |
20 | nbformat = 4 |
|
15 | nbformat = 4 | |
21 | nbformat_minor = 0 |
|
16 | nbformat_minor = 0 | |
22 | nbformat_schema = 'nbformat.v4.schema.json' |
|
17 | nbformat_schema = 'nbformat.v4.schema.json' | |
23 |
|
18 | |||
|
19 | def validate(node, ref=None): | |||
|
20 | """validate a v4 node""" | |||
|
21 | from ..current import validate | |||
|
22 | return validate(node, ref=ref, version=nbformat) | |||
|
23 | ||||
24 | class NotebookNode(Struct): |
|
24 | class NotebookNode(Struct): | |
25 | pass |
|
25 | pass | |
26 |
|
26 | |||
27 |
|
||||
28 | def from_dict(d): |
|
27 | def from_dict(d): | |
29 | if isinstance(d, dict): |
|
28 | if isinstance(d, dict): | |
30 | newd = NotebookNode() |
|
29 | newd = NotebookNode() | |
@@ -36,3 +35,62 b' def from_dict(d):' | |||||
36 | else: |
|
35 | else: | |
37 | return d |
|
36 | return d | |
38 |
|
37 | |||
|
38 | ||||
|
39 | def new_output(output_type, mime_bundle=None, **kwargs): | |||
|
40 | """Create a new output, to go in the ``cell.outputs`` list of a code cell.""" | |||
|
41 | output = NotebookNode(output_type=output_type, **kwargs) | |||
|
42 | if mime_bundle: | |||
|
43 | output.update(mime_bundle) | |||
|
44 | # populate defaults: | |||
|
45 | output.setdefault('metadata', NotebookNode()) | |||
|
46 | if output_type == 'stream': | |||
|
47 | output.setdefault('stream', 'stdout') | |||
|
48 | output.setdefault('text', '') | |||
|
49 | validate(output, output_type) | |||
|
50 | return output | |||
|
51 | ||||
|
52 | def new_code_cell(source='', **kwargs): | |||
|
53 | """Create a new code cell""" | |||
|
54 | cell = NotebookNode(cell_type='code', source=source, **kwargs) | |||
|
55 | cell.setdefault('metadata', NotebookNode()) | |||
|
56 | cell.setdefault('source', '') | |||
|
57 | cell.setdefault('prompt_number', None) | |||
|
58 | cell.setdefault('outputs', []) | |||
|
59 | ||||
|
60 | validate(cell, 'code_cell') | |||
|
61 | return cell | |||
|
62 | ||||
|
63 | def new_markdown_cell(source='', **kwargs): | |||
|
64 | """Create a new markdown cell""" | |||
|
65 | cell = NotebookNode(cell_type='markdown', source=source, **kwargs) | |||
|
66 | cell.setdefault('metadata', NotebookNode()) | |||
|
67 | ||||
|
68 | validate(cell, 'markdown_cell') | |||
|
69 | return cell | |||
|
70 | ||||
|
71 | def new_heading_cell(source='', **kwargs): | |||
|
72 | """Create a new heading cell""" | |||
|
73 | cell = NotebookNode(cell_type='heading', source=source, **kwargs) | |||
|
74 | cell.setdefault('metadata', NotebookNode()) | |||
|
75 | cell.setdefault('level', 1) | |||
|
76 | ||||
|
77 | validate(cell, 'heading_cell') | |||
|
78 | return cell | |||
|
79 | ||||
|
80 | def new_raw_cell(source='', **kwargs): | |||
|
81 | """Create a new raw cell""" | |||
|
82 | cell = NotebookNode(cell_type='raw', source=source, **kwargs) | |||
|
83 | cell.setdefault('metadata', NotebookNode()) | |||
|
84 | ||||
|
85 | validate(cell, 'raw_cell') | |||
|
86 | return cell | |||
|
87 | ||||
|
88 | def new_notebook(**kwargs): | |||
|
89 | """Create a new notebook""" | |||
|
90 | nb = NotebookNode(**kwargs) | |||
|
91 | nb.nbformat = nbformat | |||
|
92 | nb.nbformat_minor = nbformat_minor | |||
|
93 | nb.setdefault('cells', []) | |||
|
94 | nb.setdefault('metadata', NotebookNode()) | |||
|
95 | validate(nb) | |||
|
96 | return nb |
@@ -3,7 +3,7 b'' | |||||
3 | import os |
|
3 | import os | |
4 | from base64 import encodestring |
|
4 | from base64 import encodestring | |
5 |
|
5 | |||
6 |
from .. |
|
6 | from ..nbbase import ( | |
7 | new_code_cell, new_heading_cell, new_markdown_cell, new_notebook, |
|
7 | new_code_cell, new_heading_cell, new_markdown_cell, new_notebook, | |
8 | new_output, new_raw_cell |
|
8 | new_output, new_raw_cell | |
9 | ) |
|
9 | ) |
@@ -3,8 +3,8 b'' | |||||
3 |
|
3 | |||
4 | import nose.tools as nt |
|
4 | import nose.tools as nt | |
5 |
|
5 | |||
6 | from ..validator import isvalid, validate, ValidationError |
|
6 | from IPython.nbformat.validator import isvalid, validate, ValidationError | |
7 |
from .. |
|
7 | from ..nbbase import ( | |
8 | NotebookNode, nbformat, |
|
8 | NotebookNode, nbformat, | |
9 | new_code_cell, new_heading_cell, new_markdown_cell, new_notebook, |
|
9 | new_code_cell, new_heading_cell, new_markdown_cell, new_notebook, | |
10 | new_output, new_raw_cell, |
|
10 | new_output, new_raw_cell, |
@@ -10,8 +10,8 b' import nose.tools as nt' | |||||
10 |
|
10 | |||
11 | from IPython.nbformat.validator import validate, ValidationError |
|
11 | from IPython.nbformat.validator import validate, ValidationError | |
12 | from ..nbjson import reads |
|
12 | from ..nbjson import reads | |
13 |
from ..nbbase import |
|
13 | from ..nbbase import ( | |
14 | from ..compose import ( |
|
14 | nbformat, | |
15 | new_code_cell, new_heading_cell, new_markdown_cell, new_notebook, |
|
15 | new_code_cell, new_heading_cell, new_markdown_cell, new_notebook, | |
16 | new_output, new_raw_cell, |
|
16 | new_output, new_raw_cell, | |
17 | ) |
|
17 | ) | |
@@ -125,6 +125,6 b' def test_invalid_raw_cell():' | |||||
125 |
|
125 | |||
126 | def test_sample_notebook(): |
|
126 | def test_sample_notebook(): | |
127 | here = os.path.dirname(__file__) |
|
127 | here = os.path.dirname(__file__) | |
128 |
with io.open(os.path.join(here, " |
|
128 | with io.open(os.path.join(here, os.pardir, os.pardir, 'tests', "test4.ipynb"), encoding='utf-8') as f: | |
129 | nb = reads(f.read()) |
|
129 | nb = reads(f.read()) | |
130 | validate4(nb) |
|
130 | validate4(nb) |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now