Show More
@@ -1,215 +1,215 b'' | |||
|
1 | 1 | """The basic dict based notebook format. |
|
2 | 2 | |
|
3 | 3 | The Python representation of a notebook is a nested structure of |
|
4 | 4 | dictionary subclasses that support attribute access |
|
5 | 5 | (IPython.utils.ipstruct.Struct). The functions in this module are merely |
|
6 | 6 | helpers to build the structs in the right form. |
|
7 | 7 | |
|
8 | 8 | Authors: |
|
9 | 9 | |
|
10 | 10 | * Brian Granger |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Copyright (C) 2008-2011 The IPython Development Team |
|
15 | 15 | # |
|
16 | 16 | # Distributed under the terms of the BSD License. The full license is in |
|
17 | 17 | # the file COPYING, distributed as part of this software. |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Imports |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | |
|
24 | 24 | import pprint |
|
25 | 25 | import uuid |
|
26 | 26 | |
|
27 | 27 | from IPython.utils.ipstruct import Struct |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Code |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | # Change this when incrementing the nbformat version |
|
34 | 34 | nbformat = 3 |
|
35 | 35 | nbformat_minor = 0 |
|
36 | 36 | |
|
37 | 37 | class NotebookNode(Struct): |
|
38 | 38 | pass |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | def from_dict(d): |
|
42 | 42 | if isinstance(d, dict): |
|
43 | 43 | newd = NotebookNode() |
|
44 | 44 | for k,v in d.items(): |
|
45 | 45 | newd[k] = from_dict(v) |
|
46 | 46 | return newd |
|
47 | 47 | elif isinstance(d, (tuple, list)): |
|
48 | 48 | return [from_dict(i) for i in d] |
|
49 | 49 | else: |
|
50 | 50 | return d |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | def new_output(output_type=None, output_text=None, output_png=None, |
|
54 | 54 | output_html=None, output_svg=None, output_latex=None, output_json=None, |
|
55 | 55 | output_javascript=None, output_jpeg=None, prompt_number=None, |
|
56 | 56 | ename=None, evalue=None, traceback=None, stream=None, metadata=None): |
|
57 | 57 | """Create a new code cell with input and output""" |
|
58 | 58 | output = NotebookNode() |
|
59 | 59 | if output_type is not None: |
|
60 | 60 | output.output_type = unicode(output_type) |
|
61 | 61 | |
|
62 | 62 | if metadata is None: |
|
63 | 63 | metadata = {} |
|
64 | 64 | if not isinstance(metadata, dict): |
|
65 | 65 | raise TypeError("metadata must be dict") |
|
66 | 66 | output.metadata = metadata |
|
67 | 67 | |
|
68 | 68 | if output_type != 'pyerr': |
|
69 | 69 | if output_text is not None: |
|
70 | 70 | output.text = unicode(output_text) |
|
71 | 71 | if output_png is not None: |
|
72 |
output.png = |
|
|
72 | output.png = unicode(output_png) | |
|
73 | 73 | if output_jpeg is not None: |
|
74 |
output.jpeg = |
|
|
74 | output.jpeg = unicode(output_jpeg) | |
|
75 | 75 | if output_html is not None: |
|
76 | 76 | output.html = unicode(output_html) |
|
77 | 77 | if output_svg is not None: |
|
78 | 78 | output.svg = unicode(output_svg) |
|
79 | 79 | if output_latex is not None: |
|
80 | 80 | output.latex = unicode(output_latex) |
|
81 | 81 | if output_json is not None: |
|
82 | 82 | output.json = unicode(output_json) |
|
83 | 83 | if output_javascript is not None: |
|
84 | 84 | output.javascript = unicode(output_javascript) |
|
85 | 85 | |
|
86 | 86 | if output_type == u'pyout': |
|
87 | 87 | if prompt_number is not None: |
|
88 | 88 | output.prompt_number = int(prompt_number) |
|
89 | 89 | |
|
90 | 90 | if output_type == u'pyerr': |
|
91 | 91 | if ename is not None: |
|
92 | 92 | output.ename = unicode(ename) |
|
93 | 93 | if evalue is not None: |
|
94 | 94 | output.evalue = unicode(evalue) |
|
95 | 95 | if traceback is not None: |
|
96 | 96 | output.traceback = [unicode(frame) for frame in list(traceback)] |
|
97 | 97 | |
|
98 | 98 | if output_type == u'stream': |
|
99 | 99 | output.stream = 'stdout' if stream is None else unicode(stream) |
|
100 | 100 | |
|
101 | 101 | return output |
|
102 | 102 | |
|
103 | 103 | |
|
104 | 104 | def new_code_cell(input=None, prompt_number=None, outputs=None, |
|
105 | 105 | language=u'python', collapsed=False, metadata=None): |
|
106 | 106 | """Create a new code cell with input and output""" |
|
107 | 107 | cell = NotebookNode() |
|
108 | 108 | cell.cell_type = u'code' |
|
109 | 109 | if language is not None: |
|
110 | 110 | cell.language = unicode(language) |
|
111 | 111 | if input is not None: |
|
112 | 112 | cell.input = unicode(input) |
|
113 | 113 | if prompt_number is not None: |
|
114 | 114 | cell.prompt_number = int(prompt_number) |
|
115 | 115 | if outputs is None: |
|
116 | 116 | cell.outputs = [] |
|
117 | 117 | else: |
|
118 | 118 | cell.outputs = outputs |
|
119 | 119 | if collapsed is not None: |
|
120 | 120 | cell.collapsed = bool(collapsed) |
|
121 | 121 | cell.metadata = NotebookNode(metadata or {}) |
|
122 | 122 | |
|
123 | 123 | return cell |
|
124 | 124 | |
|
125 | 125 | def new_text_cell(cell_type, source=None, rendered=None, metadata=None): |
|
126 | 126 | """Create a new text cell.""" |
|
127 | 127 | cell = NotebookNode() |
|
128 | 128 | # VERSIONHACK: plaintext -> raw |
|
129 | 129 | # handle never-released plaintext name for raw cells |
|
130 | 130 | if cell_type == 'plaintext': |
|
131 | 131 | cell_type = 'raw' |
|
132 | 132 | if source is not None: |
|
133 | 133 | cell.source = unicode(source) |
|
134 | 134 | if rendered is not None: |
|
135 | 135 | cell.rendered = unicode(rendered) |
|
136 | 136 | cell.metadata = NotebookNode(metadata or {}) |
|
137 | 137 | cell.cell_type = cell_type |
|
138 | 138 | return cell |
|
139 | 139 | |
|
140 | 140 | |
|
141 | 141 | def new_heading_cell(source=None, rendered=None, level=1, metadata=None): |
|
142 | 142 | """Create a new section cell with a given integer level.""" |
|
143 | 143 | cell = NotebookNode() |
|
144 | 144 | cell.cell_type = u'heading' |
|
145 | 145 | if source is not None: |
|
146 | 146 | cell.source = unicode(source) |
|
147 | 147 | if rendered is not None: |
|
148 | 148 | cell.rendered = unicode(rendered) |
|
149 | 149 | cell.level = int(level) |
|
150 | 150 | cell.metadata = NotebookNode(metadata or {}) |
|
151 | 151 | return cell |
|
152 | 152 | |
|
153 | 153 | |
|
154 | 154 | def new_worksheet(name=None, cells=None, metadata=None): |
|
155 | 155 | """Create a worksheet by name with with a list of cells.""" |
|
156 | 156 | ws = NotebookNode() |
|
157 | 157 | if name is not None: |
|
158 | 158 | ws.name = unicode(name) |
|
159 | 159 | if cells is None: |
|
160 | 160 | ws.cells = [] |
|
161 | 161 | else: |
|
162 | 162 | ws.cells = list(cells) |
|
163 | 163 | ws.metadata = NotebookNode(metadata or {}) |
|
164 | 164 | return ws |
|
165 | 165 | |
|
166 | 166 | |
|
167 | 167 | def new_notebook(name=None, metadata=None, worksheets=None): |
|
168 | 168 | """Create a notebook by name, id and a list of worksheets.""" |
|
169 | 169 | nb = NotebookNode() |
|
170 | 170 | nb.nbformat = nbformat |
|
171 | 171 | nb.nbformat_minor = nbformat_minor |
|
172 | 172 | if worksheets is None: |
|
173 | 173 | nb.worksheets = [] |
|
174 | 174 | else: |
|
175 | 175 | nb.worksheets = list(worksheets) |
|
176 | 176 | if metadata is None: |
|
177 | 177 | nb.metadata = new_metadata() |
|
178 | 178 | else: |
|
179 | 179 | nb.metadata = NotebookNode(metadata) |
|
180 | 180 | if name is not None: |
|
181 | 181 | nb.metadata.name = unicode(name) |
|
182 | 182 | return nb |
|
183 | 183 | |
|
184 | 184 | |
|
185 | 185 | def new_metadata(name=None, authors=None, license=None, created=None, |
|
186 | 186 | modified=None, gistid=None): |
|
187 | 187 | """Create a new metadata node.""" |
|
188 | 188 | metadata = NotebookNode() |
|
189 | 189 | if name is not None: |
|
190 | 190 | metadata.name = unicode(name) |
|
191 | 191 | if authors is not None: |
|
192 | 192 | metadata.authors = list(authors) |
|
193 | 193 | if created is not None: |
|
194 | 194 | metadata.created = unicode(created) |
|
195 | 195 | if modified is not None: |
|
196 | 196 | metadata.modified = unicode(modified) |
|
197 | 197 | if license is not None: |
|
198 | 198 | metadata.license = unicode(license) |
|
199 | 199 | if gistid is not None: |
|
200 | 200 | metadata.gistid = unicode(gistid) |
|
201 | 201 | return metadata |
|
202 | 202 | |
|
203 | 203 | def new_author(name=None, email=None, affiliation=None, url=None): |
|
204 | 204 | """Create a new author.""" |
|
205 | 205 | author = NotebookNode() |
|
206 | 206 | if name is not None: |
|
207 | 207 | author.name = unicode(name) |
|
208 | 208 | if email is not None: |
|
209 | 209 | author.email = unicode(email) |
|
210 | 210 | if affiliation is not None: |
|
211 | 211 | author.affiliation = unicode(affiliation) |
|
212 | 212 | if url is not None: |
|
213 | 213 | author.url = unicode(url) |
|
214 | 214 | return author |
|
215 | 215 |
@@ -1,72 +1,72 b'' | |||
|
1 | 1 | """Read and write notebooks in JSON format. |
|
2 | 2 | |
|
3 | 3 | Authors: |
|
4 | 4 | |
|
5 | 5 | * Brian Granger |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | import copy |
|
20 | 20 | import json |
|
21 | 21 | |
|
22 | 22 | from .nbbase import from_dict |
|
23 | 23 | from .rwbase import ( |
|
24 | 24 | NotebookReader, NotebookWriter, restore_bytes, rejoin_lines, split_lines |
|
25 | 25 | ) |
|
26 | 26 | |
|
27 | 27 | from IPython.utils import py3compat |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Code |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | class BytesEncoder(json.JSONEncoder): |
|
34 | 34 | """A JSON encoder that accepts b64 (and other *ascii*) bytestrings.""" |
|
35 | 35 | def default(self, obj): |
|
36 | 36 | if isinstance(obj, bytes): |
|
37 | 37 | return obj.decode('ascii') |
|
38 | 38 | return json.JSONEncoder.default(self, obj) |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | class JSONReader(NotebookReader): |
|
42 | 42 | |
|
43 | 43 | def reads(self, s, **kwargs): |
|
44 | 44 | nb = json.loads(s, **kwargs) |
|
45 | 45 | nb = self.to_notebook(nb, **kwargs) |
|
46 | 46 | return nb |
|
47 | 47 | |
|
48 | 48 | def to_notebook(self, d, **kwargs): |
|
49 |
return |
|
|
49 | return rejoin_lines(from_dict(d)) | |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | class JSONWriter(NotebookWriter): |
|
53 | 53 | |
|
54 | 54 | def writes(self, nb, **kwargs): |
|
55 | 55 | kwargs['cls'] = BytesEncoder |
|
56 | 56 | kwargs['indent'] = 1 |
|
57 | 57 | kwargs['sort_keys'] = True |
|
58 | 58 | kwargs['separators'] = (',',': ') |
|
59 | 59 | if kwargs.pop('split_lines', True): |
|
60 | 60 | nb = split_lines(copy.deepcopy(nb)) |
|
61 | 61 | return py3compat.str_to_unicode(json.dumps(nb, **kwargs), 'utf-8') |
|
62 | 62 | |
|
63 | 63 | |
|
64 | 64 | _reader = JSONReader() |
|
65 | 65 | _writer = JSONWriter() |
|
66 | 66 | |
|
67 | 67 | reads = _reader.reads |
|
68 | 68 | read = _reader.read |
|
69 | 69 | to_notebook = _reader.to_notebook |
|
70 | 70 | write = _writer.write |
|
71 | 71 | writes = _writer.writes |
|
72 | 72 |
General Comments 0
You need to be logged in to leave comments.
Login now