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