##// END OF EJS Templates
mv stream.stream > stream.name in nbformat.v4...
MinRK -
Show More
@@ -1,96 +1,96 b''
1 1 """Python API for composing notebook elements
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
9 9 # Copyright (c) IPython Development Team.
10 10 # Distributed under the terms of the Modified BSD License.
11 11
12 12 from IPython.utils.ipstruct import Struct
13 13
14 14 # Change this when incrementing the nbformat version
15 15 nbformat = 4
16 16 nbformat_minor = 0
17 17 nbformat_schema = 'nbformat.v4.schema.json'
18 18
19 19 def validate(node, ref=None):
20 20 """validate a v4 node"""
21 21 from ..current import validate
22 22 return validate(node, ref=ref, version=nbformat)
23 23
24 24 class NotebookNode(Struct):
25 25 pass
26 26
27 27 def from_dict(d):
28 28 if isinstance(d, dict):
29 29 newd = NotebookNode()
30 30 for k,v in d.items():
31 31 newd[k] = from_dict(v)
32 32 return newd
33 33 elif isinstance(d, (tuple, list)):
34 34 return [from_dict(i) for i in d]
35 35 else:
36 36 return d
37 37
38 38
39 39 def new_output(output_type, mime_bundle=None, **kwargs):
40 40 """Create a new output, to go in the ``cell.outputs`` list of a code cell."""
41 41 output = NotebookNode(output_type=output_type, **kwargs)
42 42 if mime_bundle:
43 43 output.update(mime_bundle)
44 44 # populate defaults:
45 45 output.setdefault('metadata', NotebookNode())
46 46 if output_type == 'stream':
47 output.setdefault('stream', 'stdout')
47 output.setdefault('name', 'stdout')
48 48 output.setdefault('text', '')
49 49 validate(output, output_type)
50 50 return output
51 51
52 52 def new_code_cell(source='', **kwargs):
53 53 """Create a new code cell"""
54 54 cell = NotebookNode(cell_type='code', source=source, **kwargs)
55 55 cell.setdefault('metadata', NotebookNode())
56 56 cell.setdefault('source', '')
57 57 cell.setdefault('prompt_number', None)
58 58 cell.setdefault('outputs', [])
59 59
60 60 validate(cell, 'code_cell')
61 61 return cell
62 62
63 63 def new_markdown_cell(source='', **kwargs):
64 64 """Create a new markdown cell"""
65 65 cell = NotebookNode(cell_type='markdown', source=source, **kwargs)
66 66 cell.setdefault('metadata', NotebookNode())
67 67
68 68 validate(cell, 'markdown_cell')
69 69 return cell
70 70
71 71 def new_heading_cell(source='', **kwargs):
72 72 """Create a new heading cell"""
73 73 cell = NotebookNode(cell_type='heading', source=source, **kwargs)
74 74 cell.setdefault('metadata', NotebookNode())
75 75 cell.setdefault('level', 1)
76 76
77 77 validate(cell, 'heading_cell')
78 78 return cell
79 79
80 80 def new_raw_cell(source='', **kwargs):
81 81 """Create a new raw cell"""
82 82 cell = NotebookNode(cell_type='raw', source=source, **kwargs)
83 83 cell.setdefault('metadata', NotebookNode())
84 84
85 85 validate(cell, 'raw_cell')
86 86 return cell
87 87
88 88 def new_notebook(**kwargs):
89 89 """Create a new notebook"""
90 90 nb = NotebookNode(**kwargs)
91 91 nb.nbformat = nbformat
92 92 nb.nbformat_minor = nbformat_minor
93 93 nb.setdefault('cells', [])
94 94 nb.setdefault('metadata', NotebookNode())
95 95 validate(nb)
96 96 return nb
@@ -1,346 +1,346 b''
1 1 {
2 2 "$schema": "http://json-schema.org/draft-04/schema#",
3 3 "description": "IPython Notebook v4.0 JSON schema.",
4 4 "type": "object",
5 5 "additionalProperties": false,
6 6 "required": ["metadata", "nbformat_minor", "nbformat", "cells"],
7 7 "properties": {
8 8 "metadata": {
9 9 "description": "Notebook root-level metadata.",
10 10 "type": "object",
11 11 "additionalProperties": true,
12 12 "properties": {
13 13 "kernel_info": {
14 14 "description": "Kernel information.",
15 15 "type": "object",
16 16 "required": ["name", "language"],
17 17 "properties": {
18 18 "name": {
19 19 "description": "Name of the kernel specification.",
20 20 "type": "string"
21 21 },
22 22 "language": {
23 23 "description": "The programming language which this kernel runs.",
24 24 "type": "string"
25 25 },
26 26 "codemirror_mode": {
27 27 "description": "The codemirror mode to use for code in this language.",
28 28 "type": "string"
29 29 }
30 30 }
31 31 },
32 32 "signature": {
33 33 "description": "Hash of the notebook.",
34 34 "type": "string"
35 35 },
36 36 "orig_nbformat": {
37 37 "description": "Original notebook format (major number) before converting the notebook between versions.",
38 38 "type": "integer",
39 39 "minimum": 1
40 40 }
41 41 }
42 42 },
43 43 "nbformat_minor": {
44 44 "description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.",
45 45 "type": "integer",
46 46 "minimum": 0
47 47 },
48 48 "nbformat": {
49 49 "description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.",
50 50 "type": "integer",
51 51 "minimum": 4,
52 52 "maximum": 4
53 53 },
54 54 "cells": {
55 55 "description": "Array of cells of the current notebook.",
56 56 "type": "array",
57 57 "items": {
58 58 "type": "object",
59 59 "oneOf": [
60 60 {"$ref": "#/definitions/raw_cell"},
61 61 {"$ref": "#/definitions/markdown_cell"},
62 62 {"$ref": "#/definitions/heading_cell"},
63 63 {"$ref": "#/definitions/code_cell"}
64 64 ]
65 65 }
66 66 }
67 67 },
68 68
69 69 "definitions": {
70 70
71 71 "raw_cell": {
72 72 "description": "Notebook raw nbconvert cell.",
73 73 "type": "object",
74 74 "additionalProperties": false,
75 75 "required": ["cell_type", "metadata", "source"],
76 76 "properties": {
77 77 "cell_type": {
78 78 "description": "String identifying the type of cell.",
79 79 "enum": ["raw"]
80 80 },
81 81 "metadata": {
82 82 "description": "Cell-level metadata.",
83 83 "type": "object",
84 84 "additionalProperties": true,
85 85 "properties": {
86 86 "format": {
87 87 "description": "Raw cell metadata format for nbconvert.",
88 88 "type": "string"
89 89 },
90 90 "name": {"$ref": "#/definitions/misc/metadata_name"},
91 91 "tags": {"$ref": "#/definitions/misc/metadata_tags"}
92 92 }
93 93 },
94 94 "source": {"$ref": "#/definitions/misc/source"}
95 95 }
96 96 },
97 97
98 98 "markdown_cell": {
99 99 "description": "Notebook markdown cell.",
100 100 "type": "object",
101 101 "additionalProperties": false,
102 102 "required": ["cell_type", "metadata", "source"],
103 103 "properties": {
104 104 "cell_type": {
105 105 "description": "String identifying the type of cell.",
106 106 "enum": ["markdown"]
107 107 },
108 108 "metadata": {
109 109 "description": "Cell-level metadata.",
110 110 "type": "object",
111 111 "properties": {
112 112 "name": {"$ref": "#/definitions/misc/metadata_name"},
113 113 "tags": {"$ref": "#/definitions/misc/metadata_tags"}
114 114 },
115 115 "additionalProperties": true
116 116 },
117 117 "source": {"$ref": "#/definitions/misc/source"}
118 118 }
119 119 },
120 120
121 121 "heading_cell": {
122 122 "description": "Notebook heading cell.",
123 123 "type": "object",
124 124 "additionalProperties": false,
125 125 "required": ["cell_type", "metadata", "source", "level"],
126 126 "properties": {
127 127 "cell_type": {
128 128 "description": "String identifying the type of cell.",
129 129 "enum": ["heading"]
130 130 },
131 131 "metadata": {
132 132 "description": "Cell-level metadata.",
133 133 "type": "object",
134 134 "properties": {
135 135 "name": {"$ref": "#/definitions/misc/metadata_name"},
136 136 "tags": {"$ref": "#/definitions/misc/metadata_tags"}
137 137 },
138 138 "additionalProperties": true
139 139 },
140 140 "source": {"$ref": "#/definitions/misc/source"},
141 141 "level": {
142 142 "description": "Level of heading cells.",
143 143 "type": "integer",
144 144 "minimum": 1
145 145 }
146 146 }
147 147 },
148 148
149 149 "code_cell": {
150 150 "description": "Notebook code cell.",
151 151 "type": "object",
152 152 "additionalProperties": false,
153 153 "required": ["cell_type", "metadata", "source", "outputs", "prompt_number"],
154 154 "properties": {
155 155 "cell_type": {
156 156 "description": "String identifying the type of cell.",
157 157 "enum": ["code"]
158 158 },
159 159 "metadata": {
160 160 "description": "Cell-level metadata.",
161 161 "type": "object",
162 162 "additionalProperties": true,
163 163 "properties": {
164 164 "collapsed": {
165 165 "description": "Whether the cell is collapsed/expanded.",
166 166 "type": "boolean"
167 167 },
168 168 "autoscroll": {
169 169 "description": "Whether the cell's output is scrolled, unscrolled, or autoscrolled.",
170 170 "enum": [true, false, "auto"]
171 171 },
172 172 "name": {"$ref": "#/definitions/misc/metadata_name"},
173 173 "tags": {"$ref": "#/definitions/misc/metadata_tags"}
174 174 }
175 175 },
176 176 "source": {"$ref": "#/definitions/misc/source"},
177 177 "outputs": {
178 178 "description": "Execution, display, or stream outputs.",
179 179 "type": "array",
180 180 "items": {"$ref": "#/definitions/output"}
181 181 },
182 182 "prompt_number": {
183 183 "description": "The code cell's prompt number. Will be null if the cell has not been run.",
184 184 "type": ["integer", "null"],
185 185 "minimum": 0
186 186 }
187 187 }
188 188 },
189 189 "output": {
190 190 "type": "object",
191 191 "oneOf": [
192 192 {"$ref": "#/definitions/execute_result"},
193 193 {"$ref": "#/definitions/display_data"},
194 194 {"$ref": "#/definitions/stream"},
195 195 {"$ref": "#/definitions/error"}
196 196 ]
197 197 },
198 198 "execute_result": {
199 199 "description": "Result of executing a code cell.",
200 200 "type": "object",
201 201 "additionalProperties": false,
202 202 "required": ["output_type", "metadata", "prompt_number"],
203 203 "properties": {
204 204 "output_type": {
205 205 "description": "Type of cell output.",
206 206 "enum": ["execute_result"]
207 207 },
208 208 "prompt_number": {
209 209 "description": "A result's prompt number.",
210 210 "type": ["integer"],
211 211 "minimum": 0
212 212 },
213 213 "application/json": {
214 214 "type": "object"
215 215 },
216 216 "metadata": {"$ref": "#/definitions/misc/output_metadata"}
217 217 },
218 218 "patternProperties": {
219 219 "^(?!application/json$)[a-zA-Z0-9]+/[a-zA-Z0-9\\-\\+\\.]+$": {
220 220 "description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
221 221 "$ref": "#/definitions/misc/multiline_string"
222 222 }
223 223 }
224 224 },
225 225
226 226 "display_data": {
227 227 "description": "Data displayed as a result of code cell execution.",
228 228 "type": "object",
229 229 "additionalProperties": false,
230 230 "required": ["output_type", "metadata"],
231 231 "properties": {
232 232 "output_type": {
233 233 "description": "Type of cell output.",
234 234 "enum": ["display_data"]
235 235 },
236 236 "application/json": {
237 237 "type": "object"
238 238 },
239 239 "metadata": {"$ref": "#/definitions/misc/output_metadata"}
240 240 },
241 241 "patternProperties": {
242 242 "^(?!application/json$)[a-zA-Z0-9]+/[a-zA-Z0-9\\-\\+\\.]+$": {
243 243 "description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
244 244 "$ref": "#/definitions/misc/multiline_string"
245 245 }
246 246 }
247 247 },
248 248
249 249 "stream": {
250 250 "description": "Stream output from a code cell.",
251 251 "type": "object",
252 252 "additionalProperties": false,
253 "required": ["output_type", "metadata", "stream", "text"],
253 "required": ["output_type", "metadata", "name", "text"],
254 254 "properties": {
255 255 "output_type": {
256 256 "description": "Type of cell output.",
257 257 "enum": ["stream"]
258 258 },
259 259 "metadata": {"$ref": "#/definitions/misc/output_metadata"},
260 "stream": {
261 "description": "The stream type/destination.",
260 "name": {
261 "description": "The name of the stream (stdout, stderr).",
262 262 "type": "string"
263 263 },
264 264 "text": {
265 265 "description": "The stream's text output, represented as an array of strings.",
266 266 "$ref": "#/definitions/misc/multiline_string"
267 267 }
268 268 }
269 269 },
270 270
271 271 "error": {
272 272 "description": "Output of an error that occurred during code cell execution.",
273 273 "type": "object",
274 274 "additionalProperties": false,
275 275 "required": ["output_type", "metadata", "ename", "evalue", "traceback"],
276 276 "properties": {
277 277 "output_type": {
278 278 "description": "Type of cell output.",
279 279 "enum": ["error"]
280 280 },
281 281 "metadata": {"$ref": "#/definitions/misc/output_metadata"},
282 282 "ename": {
283 283 "description": "The name of the error.",
284 284 "type": "string"
285 285 },
286 286 "evalue": {
287 287 "description": "The value, or message, of the error.",
288 288 "type": "string"
289 289 },
290 290 "traceback": {
291 291 "description": "The error's traceback, represented as an array of strings.",
292 292 "type": "array",
293 293 "items": {"type": "string"}
294 294 }
295 295 }
296 296 },
297 297
298 298 "misc": {
299 299 "metadata_name": {
300 300 "description": "The cell's name. If present, must be a non-empty string.",
301 301 "type": "string",
302 302 "pattern": "^.+$"
303 303 },
304 304 "metadata_tags": {
305 305 "description": "The cell's tags. Tags must be unique, and must not contain commas.",
306 306 "type": "array",
307 307 "uniqueItems": true,
308 308 "items": {
309 309 "type": "string",
310 310 "pattern": "^[^,]+$"
311 311 }
312 312 },
313 313 "source": {
314 314 "description": "Contents of the cell, represented as an array of lines.",
315 315 "$ref": "#/definitions/misc/multiline_string"
316 316 },
317 317 "prompt_number": {
318 318 "description": "The code cell's prompt number. Will be null if the cell has not been run.",
319 319 "type": ["integer", "null"],
320 320 "minimum": 0
321 321 },
322 322 "mimetype": {
323 323 "patternProperties": {
324 324 "^[a-zA-Z0-9\\-\\+]+/[a-zA-Z0-9\\-\\+]+": {
325 325 "description": "The cell's mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
326 326 "$ref": "#/definitions/misc/multiline_string"
327 327 }
328 328 }
329 329 },
330 330 "output_metadata": {
331 331 "description": "Cell output metadata.",
332 332 "type": "object",
333 333 "additionalProperties": true
334 334 },
335 335 "multiline_string": {
336 336 "oneOf" : [
337 337 {"type": "string"},
338 338 {
339 339 "type": "array",
340 340 "items": {"type": "string"}
341 341 }
342 342 ]
343 343 }
344 344 }
345 345 }
346 346 }
@@ -1,105 +1,105 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 import os
4 4 from base64 import encodestring
5 5
6 6 from ..nbbase import (
7 7 new_code_cell, new_heading_cell, new_markdown_cell, new_notebook,
8 8 new_output, new_raw_cell
9 9 )
10 10
11 11 # some random base64-encoded *text*
12 12 png = encodestring(os.urandom(5)).decode('ascii')
13 13 jpeg = encodestring(os.urandom(6)).decode('ascii')
14 14
15 15 cells = []
16 16 cells.append(new_markdown_cell(
17 17 source='Some NumPy Examples',
18 18 ))
19 19
20 20
21 21 cells.append(new_code_cell(
22 22 source='import numpy',
23 23 prompt_number=1,
24 24 ))
25 25
26 26 cells.append(new_markdown_cell(
27 27 source='A random array',
28 28 ))
29 29
30 30 cells.append(new_raw_cell(
31 31 source='A random array',
32 32 ))
33 33
34 34 cells.append(new_heading_cell(
35 35 source=u'My Heading',
36 36 level=2,
37 37 ))
38 38
39 39 cells.append(new_code_cell(
40 40 source='a = numpy.random.rand(100)',
41 41 prompt_number=2,
42 42 ))
43 43 cells.append(new_code_cell(
44 44 source='a = 10\nb = 5\n',
45 45 prompt_number=3,
46 46 ))
47 47 cells.append(new_code_cell(
48 48 source='a = 10\nb = 5',
49 49 prompt_number=4,
50 50 ))
51 51
52 52 cells.append(new_code_cell(
53 53 source=u'print "ünîcødé"',
54 54 prompt_number=3,
55 55 outputs=[new_output(
56 56 output_type=u'execute_result',
57 57 mime_bundle={
58 58 'text/plain': u'<array a>',
59 59 'text/html': u'The HTML rep',
60 60 'text/latex': u'$a$',
61 61 'image/png': png,
62 62 'image/jpeg': jpeg,
63 63 'image/svg+xml': u'<svg>',
64 64 'application/json': {
65 65 'key': 'value'
66 66 },
67 67 'application/javascript': u'var i=0;'
68 68 },
69 69 prompt_number=3
70 70 ),new_output(
71 71 output_type=u'display_data',
72 72 mime_bundle={
73 73 'text/plain': u'<array a>',
74 74 'text/html': u'The HTML rep',
75 75 'text/latex': u'$a$',
76 76 'image/png': png,
77 77 'image/jpeg': jpeg,
78 78 'image/svg+xml': u'<svg>',
79 79 'application/json': {
80 80 'key': 'value'
81 81 },
82 82 'application/javascript': u'var i=0;'
83 83 },
84 84 ),new_output(
85 85 output_type=u'error',
86 86 ename=u'NameError',
87 87 evalue=u'NameError was here',
88 88 traceback=[u'frame 0', u'frame 1', u'frame 2']
89 89 ),new_output(
90 90 output_type=u'stream',
91 91 text='foo\rbar\r\n'
92 92 ),new_output(
93 93 output_type=u'stream',
94 stream='stderr',
94 name='stderr',
95 95 text='\rfoo\rbar\n'
96 96 )]
97 97 ))
98 98
99 99 nb0 = new_notebook(cells=cells,
100 100 metadata={
101 101 'language': 'python',
102 102 }
103 103 )
104 104
105 105
@@ -1,105 +1,112 b''
1 1 # coding: utf-8
2 2 """Tests for the Python API for composing notebook elements"""
3 3
4 4 import nose.tools as nt
5 5
6 6 from IPython.nbformat.validator import isvalid, validate, ValidationError
7 7 from ..nbbase import (
8 8 NotebookNode, nbformat,
9 9 new_code_cell, new_heading_cell, new_markdown_cell, new_notebook,
10 10 new_output, new_raw_cell,
11 11 )
12 12
13 13 def test_empty_notebook():
14 14 nb = new_notebook()
15 15 nt.assert_equal(nb.cells, [])
16 16 nt.assert_equal(nb.metadata, NotebookNode())
17 17 nt.assert_equal(nb.nbformat, nbformat)
18 18
19 19 def test_empty_markdown_cell():
20 20 cell = new_markdown_cell()
21 21 nt.assert_equal(cell.cell_type, 'markdown')
22 22 nt.assert_equal(cell.source, '')
23 23
24 24 def test_markdown_cell():
25 25 cell = new_markdown_cell(u'* Søme markdown')
26 26 nt.assert_equal(cell.source, u'* Søme markdown')
27 27
28 28 def test_empty_raw_cell():
29 29 cell = new_raw_cell()
30 30 nt.assert_equal(cell.cell_type, u'raw')
31 31 nt.assert_equal(cell.source, '')
32 32
33 33 def test_raw_cell():
34 34 cell = new_raw_cell('hi')
35 35 nt.assert_equal(cell.source, u'hi')
36 36
37 37 def test_empty_heading_cell():
38 38 cell = new_heading_cell()
39 39 nt.assert_equal(cell.cell_type, u'heading')
40 40 nt.assert_equal(cell.source, '')
41 41 nt.assert_equal(cell.level, 1)
42 42
43 43 def test_heading_cell():
44 44 cell = new_heading_cell(u'hi', level=2)
45 45 nt.assert_equal(cell.source, u'hi')
46 46 nt.assert_equal(cell.level, 2)
47 47
48 48 def test_empty_code_cell():
49 49 cell = new_code_cell('hi')
50 50 nt.assert_equal(cell.cell_type, 'code')
51 51 nt.assert_equal(cell.source, u'hi')
52 52
53 53 def test_empty_display_data():
54 54 output = new_output('display_data')
55 55 nt.assert_equal(output.output_type, 'display_data')
56 56
57 57 def test_empty_stream():
58 output = new_output('stream', stream='stdout', text='')
58 output = new_output('stream')
59 59 nt.assert_equal(output.output_type, 'stream')
60 nt.assert_equal(output.name, 'stdout')
61 nt.assert_equal(output.text, '')
60 62
61 63 def test_empty_execute_result():
62 64 output = new_output('execute_result', prompt_number=1)
63 65 nt.assert_equal(output.output_type, 'execute_result')
64 66
65 67 mimebundle = {
66 68 'text/plain': "some text",
67 69 "application/json": {
68 70 "key": "value"
69 71 },
70 72 "image/svg+xml": 'ABCDEF',
71 73 "application/octet-stream": 'ABC-123',
72 74 "application/vnd.foo+bar": "Some other stuff",
73 75 }
74 76
75 77 def test_display_data():
76 78 output = new_output('display_data', mimebundle)
77 79 for key, expected in mimebundle.items():
78 80 nt.assert_equal(output[key], expected)
79 81
80 82 def test_execute_result():
81 83 output = new_output('execute_result', mimebundle, prompt_number=10)
82 84 nt.assert_equal(output.prompt_number, 10)
83 85 for key, expected in mimebundle.items():
84 86 nt.assert_equal(output[key], expected)
85 87
86 88 def test_error():
87 89 o = new_output(output_type=u'error', ename=u'NameError',
88 90 evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2']
89 91 )
90 92 nt.assert_equal(o.output_type, u'error')
91 93 nt.assert_equal(o.ename, u'NameError')
92 94 nt.assert_equal(o.evalue, u'Name not found')
93 95 nt.assert_equal(o.traceback, [u'frame 0', u'frame 1', u'frame 2'])
94 96
95 97 def test_code_cell_with_outputs():
96 98 cell = new_code_cell(prompt_number=10, outputs=[
97 99 new_output('display_data', mimebundle),
98 100 new_output('stream', text='hello'),
99 101 new_output('execute_result', mimebundle, prompt_number=10),
100 102 ])
101 103 nt.assert_equal(cell.prompt_number, 10)
102 104 nt.assert_equal(len(cell.outputs), 3)
103 105 er = cell.outputs[-1]
104 106 nt.assert_equal(er.prompt_number, 10)
105 107 nt.assert_equal(er['output_type'], 'execute_result')
108
109 def test_stream():
110 output = new_output('stream', name='stderr', text='hello there')
111 nt.assert_equal(output.name, 'stderr')
112 nt.assert_equal(output.text, 'hello there')
General Comments 0
You need to be logged in to leave comments. Login now