Show More
@@ -0,0 +1,21 b'' | |||
|
1 | """NotebookNode - adding attribute access to dicts""" | |
|
2 | ||
|
3 | from IPython.utils.ipstruct import Struct | |
|
4 | ||
|
5 | class NotebookNode(Struct): | |
|
6 | """A dict-like node with attribute-access""" | |
|
7 | pass | |
|
8 | ||
|
9 | def from_dict(d): | |
|
10 | """Convert dict to dict-like NotebookNode | |
|
11 | ||
|
12 | Recursively converts any dict in the container to a NotebookNode | |
|
13 | """ | |
|
14 | if isinstance(d, dict): | |
|
15 | return NotebookNode({k:from_dict(v) for k,v in d.items()}) | |
|
16 | elif isinstance(d, (tuple, list)): | |
|
17 | return [from_dict(i) for i in d] | |
|
18 | else: | |
|
19 | return d | |
|
20 | ||
|
21 |
@@ -5,7 +5,7 b'' | |||
|
5 | 5 | |
|
6 | 6 | from functools import wraps |
|
7 | 7 | |
|
8 |
from IPython.nbformat |
|
|
8 | from IPython.nbformat import NotebookNode | |
|
9 | 9 | from IPython.utils.decorators import undoc |
|
10 | 10 | from IPython.utils.py3compat import string_types |
|
11 | 11 | |
@@ -29,7 +29,7 b' def DocDecorator(f):' | |||
|
29 | 29 | |
|
30 | 30 | #Set docstring of function |
|
31 | 31 | f.__doc__ = f.__doc__ + """ |
|
32 |
nb : :class:`~ |
|
|
32 | nb : :class:`~IPython.nbformat.NotebookNode` | |
|
33 | 33 | The notebook to export. |
|
34 | 34 | config : config (optional, keyword arg) |
|
35 | 35 | User configuration instance. |
@@ -52,7 +52,7 b' def DocDecorator(f):' | |||
|
52 | 52 | Notes |
|
53 | 53 | ----- |
|
54 | 54 | WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT |
|
55 | """.format(nbnode_mod=NotebookNode.__module__) | |
|
55 | """ | |
|
56 | 56 | |
|
57 | 57 | @wraps(f) |
|
58 | 58 | def decorator(*args, **kwargs): |
@@ -89,7 +89,7 b' class Exporter(LoggingConfigurable):' | |||
|
89 | 89 | |
|
90 | 90 | Parameters |
|
91 | 91 | ---------- |
|
92 |
nb : :class:`~IPython.nbformat. |
|
|
92 | nb : :class:`~IPython.nbformat.NotebookNode` | |
|
93 | 93 | Notebook node (dict-like with attr-access) |
|
94 | 94 | resources : dict |
|
95 | 95 | Additional resources that can be accessed read/write by |
@@ -199,7 +199,7 b' class TemplateExporter(Exporter):' | |||
|
199 | 199 | |
|
200 | 200 | Parameters |
|
201 | 201 | ---------- |
|
202 |
nb : :class:`~IPython.nbformat. |
|
|
202 | nb : :class:`~IPython.nbformat.NotebookNode` | |
|
203 | 203 | Notebook node |
|
204 | 204 | resources : dict |
|
205 | 205 | Additional resources that can be accessed read/write by |
@@ -23,6 +23,7 b' versions = {' | |||
|
23 | 23 | from .validator import validate, ValidationError |
|
24 | 24 | from .converter import convert |
|
25 | 25 | from . import reader |
|
26 | from .notebooknode import from_dict, NotebookNode | |
|
26 | 27 | |
|
27 | 28 | from .v4 import ( |
|
28 | 29 | nbformat as current_nbformat, |
@@ -4,15 +4,15 b'' | |||
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | 6 | from .nbbase import ( |
|
7 | NotebookNode, from_dict, | |
|
8 | 7 | nbformat, nbformat_minor, nbformat_schema, |
|
9 | 8 | new_code_cell, new_markdown_cell, new_notebook, |
|
10 | 9 | new_output, output_from_msg, |
|
11 | 10 | ) |
|
12 | 11 | |
|
13 |
from .nbjson import reads |
|
|
14 | from .nbjson import reads as read_json, writes as write_json | |
|
15 | from .nbjson import to_notebook as to_notebook_json | |
|
12 | from .nbjson import reads, writes, to_notebook | |
|
13 | reads_json = reads | |
|
14 | writes_json = writes | |
|
15 | to_notebook_json = to_notebook | |
|
16 | 16 | |
|
17 | 17 | from .convert import downgrade, upgrade |
|
18 | 18 |
@@ -9,7 +9,7 b' helpers to build the structs in the right form.' | |||
|
9 | 9 | # Copyright (c) IPython Development Team. |
|
10 | 10 | # Distributed under the terms of the Modified BSD License. |
|
11 | 11 | |
|
12 | from IPython.utils.ipstruct import Struct | |
|
12 | from ..notebooknode import from_dict, NotebookNode | |
|
13 | 13 | |
|
14 | 14 | # Change this when incrementing the nbformat version |
|
15 | 15 | nbformat = 4 |
@@ -23,18 +23,6 b' def validate(node, ref=None):' | |||
|
23 | 23 | return validate(node, ref=ref, version=nbformat) |
|
24 | 24 | |
|
25 | 25 | |
|
26 | class NotebookNode(Struct): | |
|
27 | pass | |
|
28 | ||
|
29 | def from_dict(d): | |
|
30 | if isinstance(d, dict): | |
|
31 | return NotebookNode({k:from_dict(v) for k,v in d.items()}) | |
|
32 | elif isinstance(d, (tuple, list)): | |
|
33 | return [from_dict(i) for i in d] | |
|
34 | else: | |
|
35 | return d | |
|
36 | ||
|
37 | ||
|
38 | 26 | def new_output(output_type, data=None, **kwargs): |
|
39 | 27 | """Create a new output, to go in the ``cell.outputs`` list of a code cell.""" |
|
40 | 28 | output = NotebookNode(output_type=output_type) |
@@ -25,12 +25,18 b' class BytesEncoder(json.JSONEncoder):' | |||
|
25 | 25 | class JSONReader(NotebookReader): |
|
26 | 26 | |
|
27 | 27 | def reads(self, s, **kwargs): |
|
28 | """Read a JSON string into a Notebook object""" | |
|
28 | 29 | nb = json.loads(s, **kwargs) |
|
29 | 30 | nb = self.to_notebook(nb, **kwargs) |
|
30 | 31 | return nb |
|
31 | 32 | |
|
32 | 33 | def to_notebook(self, d, **kwargs): |
|
33 | nb = rejoin_lines(from_dict(d)) | |
|
34 | """Convert a disk-format notebook dict to in-memory NotebookNode | |
|
35 | ||
|
36 | handles multi-line values as strings, scrubbing of transient values, etc. | |
|
37 | """ | |
|
38 | nb = from_dict(d) | |
|
39 | nb = rejoin_lines(nb) | |
|
34 | 40 | nb = strip_transient(nb) |
|
35 | 41 | return nb |
|
36 | 42 | |
@@ -38,6 +44,7 b' class JSONReader(NotebookReader):' | |||
|
38 | 44 | class JSONWriter(NotebookWriter): |
|
39 | 45 | |
|
40 | 46 | def writes(self, nb, **kwargs): |
|
47 | """Serialize a NotebookNode object as a JSON string""" | |
|
41 | 48 | kwargs['cls'] = BytesEncoder |
|
42 | 49 | kwargs['indent'] = 1 |
|
43 | 50 | kwargs['sort_keys'] = True |
General Comments 0
You need to be logged in to leave comments.
Login now