##// END OF EJS Templates
note about mime-bundle in nbformat doc
Min RK -
Show More
@@ -1,336 +1,338 b''
1 .. _nbformat:
1 .. _nbformat:
2
2
3 ===========================
3 ===========================
4 The Jupyter Notebook Format
4 The Jupyter Notebook Format
5 ===========================
5 ===========================
6
6
7 Introduction
7 Introduction
8 ============
8 ============
9
9
10 Jupyter (nΓ© IPython) notebook files are simple JSON documents,
10 Jupyter (nΓ© IPython) notebook files are simple JSON documents,
11 containing text, source code, rich media output, and metadata.
11 containing text, source code, rich media output, and metadata.
12 each segment of the document is stored in a cell.
12 each segment of the document is stored in a cell.
13
13
14 Some general points about the notebook format:
14 Some general points about the notebook format:
15
15
16 .. note::
16 .. note::
17
17
18 *All* metadata fields are optional.
18 *All* metadata fields are optional.
19 While the type and values of some metadata are defined,
19 While the type and values of some metadata are defined,
20 no metadata values are required to be defined.
20 no metadata values are required to be defined.
21
21
22
22
23 Top-level structure
23 Top-level structure
24 ===================
24 ===================
25
25
26 At the highest level, a Jupyter notebook is a dictionary with a few keys:
26 At the highest level, a Jupyter notebook is a dictionary with a few keys:
27
27
28 - metadata (dict)
28 - metadata (dict)
29 - nbformat (int)
29 - nbformat (int)
30 - nbformat_minor (int)
30 - nbformat_minor (int)
31 - cells (list)
31 - cells (list)
32
32
33 .. sourcecode:: python
33 .. sourcecode:: python
34
34
35 {
35 {
36 "metadata" : {
36 "metadata" : {
37 "signature": "hex-digest", # used for authenticating unsafe outputs on load
37 "signature": "hex-digest", # used for authenticating unsafe outputs on load
38 "kernel_info": {
38 "kernel_info": {
39 # if kernel_info is defined, its name and language fields are required.
39 # if kernel_info is defined, its name and language fields are required.
40 "name" : "the name of the kernel",
40 "name" : "the name of the kernel",
41 "language" : "the programming language of the kernel",
41 "language" : "the programming language of the kernel",
42 "codemirror_mode": "The name of the codemirror mode to use [optional]"
42 "codemirror_mode": "The name of the codemirror mode to use [optional]"
43 },
43 },
44 },
44 },
45 "nbformat": 4,
45 "nbformat": 4,
46 "nbformat_minor": 0,
46 "nbformat_minor": 0,
47 "cells" : [
47 "cells" : [
48 # list of cell dictionaries, see below
48 # list of cell dictionaries, see below
49 ],
49 ],
50 }
50 }
51
51
52 Some fields, such as code input and text output, are characteristically multi-line strings.
52 Some fields, such as code input and text output, are characteristically multi-line strings.
53 When these fields are written to disk, they **may** be written as a list of strings,
53 When these fields are written to disk, they **may** be written as a list of strings,
54 which should be joined with ``''`` when reading back into memory.
54 which should be joined with ``''`` when reading back into memory.
55 In programmatic APIs for working with notebooks (Python, Javascript),
55 In programmatic APIs for working with notebooks (Python, Javascript),
56 these are always re-joined into the original multi-line string.
56 these are always re-joined into the original multi-line string.
57 If you intend to work with notebook files directly,
57 If you intend to work with notebook files directly,
58 you must allow multi-line string fields to be either a string or list of strings.
58 you must allow multi-line string fields to be either a string or list of strings.
59
59
60
60
61 Cell Types
61 Cell Types
62 ==========
62 ==========
63
63
64 There are a few basic cell types for encapsulating code and text.
64 There are a few basic cell types for encapsulating code and text.
65 All cells have the following basic structure:
65 All cells have the following basic structure:
66
66
67 .. sourcecode:: python
67 .. sourcecode:: python
68
68
69 {
69 {
70 "cell_type" : "name",
70 "cell_type" : "name",
71 "metadata" : {},
71 "metadata" : {},
72 "source" : "single string or [list, of, strings]",
72 "source" : "single string or [list, of, strings]",
73 }
73 }
74
74
75
75
76 Markdown cells
76 Markdown cells
77 --------------
77 --------------
78
78
79 Markdown cells are used for body-text, and contain markdown,
79 Markdown cells are used for body-text, and contain markdown,
80 as defined in `GitHub-flavored markdown`_, and implemented in marked_.
80 as defined in `GitHub-flavored markdown`_, and implemented in marked_.
81
81
82 .. _GitHub-flavored markdown: https://help.github.com/articles/github-flavored-markdown
82 .. _GitHub-flavored markdown: https://help.github.com/articles/github-flavored-markdown
83 .. _marked: https://github.com/chjj/marked
83 .. _marked: https://github.com/chjj/marked
84
84
85 .. sourcecode:: python
85 .. sourcecode:: python
86
86
87 {
87 {
88 "cell_type" : "markdown",
88 "cell_type" : "markdown",
89 "metadata" : {},
89 "metadata" : {},
90 "source" : ["some *markdown*"],
90 "source" : ["some *markdown*"],
91 }
91 }
92
92
93 .. versionchanged:: nbformat 4.0
93 .. versionchanged:: nbformat 4.0
94
94
95 Heading cells have been removed, in favor of simple headings in markdown.
95 Heading cells have been removed, in favor of simple headings in markdown.
96
96
97
97
98 Code cells
98 Code cells
99 ----------
99 ----------
100
100
101 Code cells are the primary content of Jupyter notebooks.
101 Code cells are the primary content of Jupyter notebooks.
102 They contain source code in the language of the document's associated kernel,
102 They contain source code in the language of the document's associated kernel,
103 and a list of outputs associated with executing that code.
103 and a list of outputs associated with executing that code.
104 They also have an execution_count, which must be an integer or ``null``.
104 They also have an execution_count, which must be an integer or ``null``.
105
105
106 .. sourcecode:: python
106 .. sourcecode:: python
107
107
108 {
108 {
109 "cell_type" : "code",
109 "cell_type" : "code",
110 "execution_count": 1, # integer or null
110 "execution_count": 1, # integer or null
111 "metadata" : {
111 "metadata" : {
112 "collapsed" : True, # whether the output of the cell is collapsed
112 "collapsed" : True, # whether the output of the cell is collapsed
113 "autoscroll": False, # any of true, false or "auto"
113 "autoscroll": False, # any of true, false or "auto"
114 },
114 },
115 "source" : ["some code"],
115 "source" : ["some code"],
116 "outputs": [{
116 "outputs": [{
117 # list of output dicts (described below)
117 # list of output dicts (described below)
118 "output_type": "stream",
118 "output_type": "stream",
119 ...
119 ...
120 }],
120 }],
121 }
121 }
122
122
123 .. versionchanged:: nbformat 4.0
123 .. versionchanged:: nbformat 4.0
124
124
125 ``input`` was renamed to ``source``, for consistency among cell types.
125 ``input`` was renamed to ``source``, for consistency among cell types.
126
126
127 .. versionchanged:: nbformat 4.0
127 .. versionchanged:: nbformat 4.0
128
128
129 ``prompt_number`` renamed to ``execution_count``
129 ``prompt_number`` renamed to ``execution_count``
130
130
131 Code cell outputs
131 Code cell outputs
132 -----------------
132 -----------------
133
133
134 A code cell can have a variety of outputs (stream data or rich mime-type output).
134 A code cell can have a variety of outputs (stream data or rich mime-type output).
135 These correspond to :ref:`messages <messaging>` produced as a result of executing the cell.
135 These correspond to :ref:`messages <messaging>` produced as a result of executing the cell.
136
136
137 All outputs have an ``output_type`` field,
137 All outputs have an ``output_type`` field,
138 which is a string defining what type of output it is.
138 which is a string defining what type of output it is.
139
139
140
140
141 stream output
141 stream output
142 *************
142 *************
143
143
144 .. sourcecode:: python
144 .. sourcecode:: python
145
145
146 {
146 {
147 "output_type" : "stream",
147 "output_type" : "stream",
148 "name" : "stdout", # or stderr
148 "name" : "stdout", # or stderr
149 "data" : ["multiline stream text"],
149 "data" : ["multiline stream text"],
150 }
150 }
151
151
152 .. versionchanged:: nbformat 4.0
152 .. versionchanged:: nbformat 4.0
153
153
154 The keys ``stream`` and ``text`` were changed to ``name`` and ``data`` to match
154 The keys ``stream`` and ``text`` were changed to ``name`` and ``data`` to match
155 the stream message specification.
155 the stream message specification.
156
156
157
157
158 display_data
158 display_data
159 ************
159 ************
160
160
161 Rich display messages (as created by ``display_data`` messages)
161 Rich display outputs, as created by ``display_data`` messages,
162 contain data keyed by mime-type. All mime-type data should
162 contain data keyed by mime-type. This is often called a mime-bundle,
163 and shows up in various locations in the notebook format and message spec.
163 The metadata of these messages may be keyed by mime-type as well.
164 The metadata of these messages may be keyed by mime-type as well.
164
165
165
166
167
166 .. sourcecode:: python
168 .. sourcecode:: python
167
169
168 {
170 {
169 "output_type" : "display_data",
171 "output_type" : "display_data",
170 "data" : {
172 "data" : {
171 "text/plain" : ["multiline text data"],
173 "text/plain" : ["multiline text data"],
172 "image/png": ["base64-encoded-png-data"],
174 "image/png": ["base64-encoded-png-data"],
173 "application/json": {
175 "application/json": {
174 # JSON data is included as-is
176 # JSON data is included as-is
175 "json": "data",
177 "json": "data",
176 },
178 },
177 },
179 },
178 "metadata" : {
180 "metadata" : {
179 "image/png": {
181 "image/png": {
180 "width": 640,
182 "width": 640,
181 "height": 480,
183 "height": 480,
182 },
184 },
183 },
185 },
184 }
186 }
185
187
186
188
187 .. versionchanged:: nbformat 4.0
189 .. versionchanged:: nbformat 4.0
188
190
189 ``application/json`` output is no longer double-serialized into a string.
191 ``application/json`` output is no longer double-serialized into a string.
190
192
191 .. versionchanged:: nbformat 4.0
193 .. versionchanged:: nbformat 4.0
192
194
193 mime-types are used for keys, instead of a combination of short names (``text``)
195 mime-types are used for keys, instead of a combination of short names (``text``)
194 and mime-types, and are stored in a ``data`` key, rather than the top-level.
196 and mime-types, and are stored in a ``data`` key, rather than the top-level.
195 i.e. ``output.data['image/png']`` instead of ``output.png``.
197 i.e. ``output.data['image/png']`` instead of ``output.png``.
196
198
197
199
198 execute_result
200 execute_result
199 **************
201 **************
200
202
201 Results of executing a cell (as created by ``displayhook`` in Python)
203 Results of executing a cell (as created by ``displayhook`` in Python)
202 are stored in ``execute_result`` outputs.
204 are stored in ``execute_result`` outputs.
203 `execute_result` outputs are identical to ``display_data``,
205 `execute_result` outputs are identical to ``display_data``,
204 adding only a ``execution_count`` field, which must be an integer.
206 adding only a ``execution_count`` field, which must be an integer.
205
207
206 .. sourcecode:: python
208 .. sourcecode:: python
207
209
208 {
210 {
209 "output_type" : "execute_result",
211 "output_type" : "execute_result",
210 "execution_count": 42,
212 "execution_count": 42,
211 "data" : {
213 "data" : {
212 "text/plain" : ["multiline text data"],
214 "text/plain" : ["multiline text data"],
213 "image/png": ["base64-encoded-png-data"],
215 "image/png": ["base64-encoded-png-data"],
214 "application/json": {
216 "application/json": {
215 # JSON data is included as-is
217 # JSON data is included as-is
216 "json": "data",
218 "json": "data",
217 },
219 },
218 },
220 },
219 "metadata" : {
221 "metadata" : {
220 "image/png": {
222 "image/png": {
221 "width": 640,
223 "width": 640,
222 "height": 480,
224 "height": 480,
223 },
225 },
224 },
226 },
225 }
227 }
226
228
227 .. versionchanged:: nbformat 4.0
229 .. versionchanged:: nbformat 4.0
228
230
229 ``pyout`` renamed to ``execute_result``
231 ``pyout`` renamed to ``execute_result``
230
232
231 .. versionchanged:: nbformat 4.0
233 .. versionchanged:: nbformat 4.0
232
234
233 ``prompt_number`` renamed to ``execution_count``
235 ``prompt_number`` renamed to ``execution_count``
234
236
235
237
236 error
238 error
237 *****
239 *****
238
240
239 Failed execution may show a traceback
241 Failed execution may show a traceback
240
242
241 .. sourcecode:: python
243 .. sourcecode:: python
242
244
243 {
245 {
244 'ename' : str, # Exception name, as a string
246 'ename' : str, # Exception name, as a string
245 'evalue' : str, # Exception value, as a string
247 'evalue' : str, # Exception value, as a string
246
248
247 # The traceback will contain a list of frames,
249 # The traceback will contain a list of frames,
248 # represented each as a string.
250 # represented each as a string.
249 'traceback' : list,
251 'traceback' : list,
250 }
252 }
251
253
252 .. versionchanged:: nbformat 4.0
254 .. versionchanged:: nbformat 4.0
253
255
254 ``pyerr`` renamed to ``error``
256 ``pyerr`` renamed to ``error``
255
257
256
258
257 .. _raw nbconvert cells:
259 .. _raw nbconvert cells:
258
260
259 Raw NBConvert cells
261 Raw NBConvert cells
260 -------------------
262 -------------------
261
263
262 A raw cell is defined as content that should be included *unmodified* in :ref:`nbconvert <nbconvert>` output.
264 A raw cell is defined as content that should be included *unmodified* in :ref:`nbconvert <nbconvert>` output.
263 For example, this cell could include raw LaTeX for nbconvert to pdf via latex,
265 For example, this cell could include raw LaTeX for nbconvert to pdf via latex,
264 or restructured text for use in Sphinx documentation.
266 or restructured text for use in Sphinx documentation.
265
267
266 The notebook authoring environment does not render raw cells.
268 The notebook authoring environment does not render raw cells.
267
269
268 The only logic in a raw cell is the `format` metadata field.
270 The only logic in a raw cell is the `format` metadata field.
269 If defined, it specifies which nbconvert output format is the intended target
271 If defined, it specifies which nbconvert output format is the intended target
270 for the raw cell. When outputting to any other format,
272 for the raw cell. When outputting to any other format,
271 the raw cell's contents will be excluded.
273 the raw cell's contents will be excluded.
272 In the default case when this value is undefined,
274 In the default case when this value is undefined,
273 a raw cell's contents will be included in any nbconvert output,
275 a raw cell's contents will be included in any nbconvert output,
274 regardless of format.
276 regardless of format.
275
277
276 .. sourcecode:: python
278 .. sourcecode:: python
277
279
278 {
280 {
279 "cell_type" : "raw",
281 "cell_type" : "raw",
280 "metadata" : {
282 "metadata" : {
281 # the mime-type of the target nbconvert format.
283 # the mime-type of the target nbconvert format.
282 # nbconvert to formats other than this will exclude this cell.
284 # nbconvert to formats other than this will exclude this cell.
283 "format" : "mime/type"
285 "format" : "mime/type"
284 },
286 },
285 "source" : ["some nbformat mime-type data"]
287 "source" : ["some nbformat mime-type data"]
286 }
288 }
287
289
288 Metadata
290 Metadata
289 ========
291 ========
290
292
291 Metadata is a place that you can put arbitrary JSONable information about
293 Metadata is a place that you can put arbitrary JSONable information about
292 your notebook, cell, or output. Because it is a shared namespace,
294 your notebook, cell, or output. Because it is a shared namespace,
293 any custom metadata should use a sufficiently unique namespace,
295 any custom metadata should use a sufficiently unique namespace,
294 such as `metadata.kaylees_md.foo = "bar"`.
296 such as `metadata.kaylees_md.foo = "bar"`.
295
297
296 Metadata fields officially defined for Jupyter notebooks are listed here:
298 Metadata fields officially defined for Jupyter notebooks are listed here:
297
299
298 Notebook metadata
300 Notebook metadata
299 -----------------
301 -----------------
300
302
301 The following metadata keys are defined at the notebook level:
303 The following metadata keys are defined at the notebook level:
302
304
303 =========== =============== ==============
305 =========== =============== ==============
304 Key Value Interpretation
306 Key Value Interpretation
305 =========== =============== ==============
307 =========== =============== ==============
306 kernelspec dict A :ref:`kernel specification <kernelspecs>`
308 kernelspec dict A :ref:`kernel specification <kernelspecs>`
307 signature str A hashed :ref:`signature <notebook_security>` of the notebook
309 signature str A hashed :ref:`signature <notebook_security>` of the notebook
308 =========== =============== ==============
310 =========== =============== ==============
309
311
310
312
311 Cell metadata
313 Cell metadata
312 -------------
314 -------------
313
315
314 The following metadata keys are defined at the cell level:
316 The following metadata keys are defined at the cell level:
315
317
316 =========== =============== ==============
318 =========== =============== ==============
317 Key Value Interpretation
319 Key Value Interpretation
318 =========== =============== ==============
320 =========== =============== ==============
319 collapsed bool Whether the cell's output container should be collapsed
321 collapsed bool Whether the cell's output container should be collapsed
320 autoscroll bool or 'auto' Whether the cell's output is scrolled, unscrolled, or autoscrolled
322 autoscroll bool or 'auto' Whether the cell's output is scrolled, unscrolled, or autoscrolled
321 deletable bool If False, prevent deletion of the cell
323 deletable bool If False, prevent deletion of the cell
322 format 'mime/type' The mime-type of a :ref:`Raw NBConvert Cell <raw nbconvert cells>`
324 format 'mime/type' The mime-type of a :ref:`Raw NBConvert Cell <raw nbconvert cells>`
323 name str A name for the cell. Should be unique
325 name str A name for the cell. Should be unique
324 tags list of str A list of string tags on the cell. Commas are not allowed in a tag
326 tags list of str A list of string tags on the cell. Commas are not allowed in a tag
325 =========== =============== ==============
327 =========== =============== ==============
326
328
327 Output metadata
329 Output metadata
328 ---------------
330 ---------------
329
331
330 The following metadata keys are defined for code cell outputs:
332 The following metadata keys are defined for code cell outputs:
331
333
332 =========== =============== ==============
334 =========== =============== ==============
333 Key Value Interpretation
335 Key Value Interpretation
334 =========== =============== ==============
336 =========== =============== ==============
335 isolated bool Whether the output should be isolated into an IFrame
337 isolated bool Whether the output should be isolated into an IFrame
336 =========== =============== ==============
338 =========== =============== ==============
General Comments 0
You need to be logged in to leave comments. Login now