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