Show More
@@ -1,278 +1,291 | |||||
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 |
|
85 | |||
86 | Heading cells |
|
86 | Heading cells | |
87 | ------------- |
|
87 | ------------- | |
88 |
|
88 | |||
89 | Heading cells are single lines describing a section header (mapping onto h1-h6 tags in HTML). |
|
89 | Heading cells are single lines describing a section header (mapping onto h1-h6 tags in HTML). | |
90 | These cells indicate structure of the document, |
|
90 | These cells indicate structure of the document, | |
91 | and are used for things like outline-views and automatically generating HTML anchors |
|
91 | and are used for things like outline-views and automatically generating HTML anchors | |
92 | within the page for quick navigation. |
|
92 | within the page for quick navigation. | |
93 | They have a ``level`` field, with an integer value from 1-6 (inclusive). |
|
93 | They have a ``level`` field, with an integer value from 1-6 (inclusive). | |
94 |
|
94 | |||
95 | .. sourcecode:: python |
|
95 | .. sourcecode:: python | |
96 |
|
96 | |||
97 | { |
|
97 | { | |
98 | "cell_type" : "markdown", |
|
98 | "cell_type" : "markdown", | |
99 | "metadata" : {}, |
|
99 | "metadata" : {}, | |
100 | "level" : 1, # An integer on [1-6] |
|
100 | "level" : 1, # An integer on [1-6] | |
101 | "source" : ["A simple heading"], |
|
101 | "source" : ["A simple heading"], | |
102 | } |
|
102 | } | |
103 |
|
103 | |||
104 |
|
104 | |||
105 | Code cells |
|
105 | Code cells | |
106 | ---------- |
|
106 | ---------- | |
107 |
|
107 | |||
108 | Code cells are the primary content of Jupyter notebooks. |
|
108 | Code cells are the primary content of Jupyter notebooks. | |
109 | They contain source code int e language of the document's associated kernel, |
|
109 | They contain source code int e language of the document's associated kernel, | |
110 | and a list of outputs associated with executing. |
|
110 | and a list of outputs associated with executing. | |
111 |
They also have a |
|
111 | They also have an execution_count, which must be an integer or ``null``. | |
112 |
|
112 | |||
113 | .. sourcecode:: python |
|
113 | .. sourcecode:: python | |
114 |
|
114 | |||
115 | { |
|
115 | { | |
116 | "cell_type" : "code", |
|
116 | "cell_type" : "code", | |
117 |
" |
|
117 | "execution_count": 1, # integer or null | |
118 | "metadata" : { |
|
118 | "metadata" : { | |
119 | "collapsed" : True, # whether the output of the cell is collapsed |
|
119 | "collapsed" : True, # whether the output of the cell is collapsed | |
120 | "autoscroll": False, # any of true, false or "auto" |
|
120 | "autoscroll": False, # any of true, false or "auto" | |
121 | }, |
|
121 | }, | |
122 | "source" : ["some code"], |
|
122 | "source" : ["some code"], | |
123 | "outputs": [{ |
|
123 | "outputs": [{ | |
124 | # list of output dicts (described below) |
|
124 | # list of output dicts (described below) | |
125 | "output_type": "stream", |
|
125 | "output_type": "stream", | |
126 | ... |
|
126 | ... | |
127 | }], |
|
127 | }], | |
128 | } |
|
128 | } | |
129 |
|
129 | |||
130 | .. versionchanged:: 4.0 |
|
130 | .. versionchanged:: 4.0 | |
131 |
|
131 | |||
132 | ``input`` was renamed to ``source``, for consistency among cell types. |
|
132 | ``input`` was renamed to ``source``, for consistency among cell types. | |
133 |
|
133 | |||
|
134 | .. versionchanged:: 4.0 | |||
|
135 | ||||
|
136 | ``prompt_number`` renamed to ``execution_count`` | |||
|
137 | ||||
134 | Code cell outputs |
|
138 | Code cell outputs | |
135 | ----------------- |
|
139 | ----------------- | |
136 |
|
140 | |||
137 | A code cell can have a variety of outputs (stream data or rich mime-type output). |
|
141 | A code cell can have a variety of outputs (stream data or rich mime-type output). | |
138 | These correspond to :ref:`messages <messaging>` produced as a result of executing the cell. |
|
142 | These correspond to :ref:`messages <messaging>` produced as a result of executing the cell. | |
139 |
|
143 | |||
140 | All outputs have an ``output_type`` field, |
|
144 | All outputs have an ``output_type`` field, | |
141 | which is a string defining what type of output it is. |
|
145 | which is a string defining what type of output it is. | |
142 |
|
146 | |||
143 |
|
147 | |||
144 | stream output |
|
148 | stream output | |
145 | ************* |
|
149 | ************* | |
146 |
|
150 | |||
147 | .. sourcecode:: python |
|
151 | .. sourcecode:: python | |
148 |
|
152 | |||
149 | { |
|
153 | { | |
150 | "output_type" : "stream", |
|
154 | "output_type" : "stream", | |
151 | "name" : "stdout", # or stderr |
|
155 | "name" : "stdout", # or stderr | |
152 | "data" : ["multiline stream text"], |
|
156 | "data" : ["multiline stream text"], | |
153 | } |
|
157 | } | |
154 |
|
158 | |||
155 | .. versionchanged:: 4.0 |
|
159 | .. versionchanged:: 4.0 | |
156 |
|
160 | |||
157 | The keys ``stream`` and ``text`` were changed to ``name`` and ``data`` to match |
|
161 | The keys ``stream`` and ``text`` were changed to ``name`` and ``data`` to match | |
158 | the stream message specification. |
|
162 | the stream message specification. | |
159 |
|
163 | |||
160 |
|
164 | |||
161 | display_data |
|
165 | display_data | |
162 | ************ |
|
166 | ************ | |
163 |
|
167 | |||
164 | Rich display messages (as created by ``display_data`` messages) |
|
168 | Rich display messages (as created by ``display_data`` messages) | |
165 | contain data keyed by mime-type. All mime-type data should |
|
169 | contain data keyed by mime-type. All mime-type data should | |
166 | The metadata of these messages may be keyed by mime-type as well. |
|
170 | The metadata of these messages may be keyed by mime-type as well. | |
167 |
|
171 | |||
168 |
|
172 | |||
169 | .. sourcecode:: python |
|
173 | .. sourcecode:: python | |
170 |
|
174 | |||
171 | { |
|
175 | { | |
172 | "output_type" : "display_data", |
|
176 | "output_type" : "display_data", | |
173 | "text/plain" : ["multiline text data"], |
|
177 | "data" : { | |
174 | "image/png": ["base64-encoded-png-data"], |
|
178 | "text/plain" : ["multiline text data"], | |
175 | "application/json": { |
|
179 | "image/png": ["base64-encoded-png-data"], | |
176 | # JSON data is included as-is |
|
180 | "application/json": { | |
177 | "json": "data", |
|
181 | # JSON data is included as-is | |
|
182 | "json": "data", | |||
|
183 | }, | |||
178 | }, |
|
184 | }, | |
179 | "metadata" : { |
|
185 | "metadata" : { | |
180 | "image/png": { |
|
186 | "image/png": { | |
181 | "width": 640, |
|
187 | "width": 640, | |
182 | "height": 480, |
|
188 | "height": 480, | |
183 | }, |
|
189 | }, | |
184 | }, |
|
190 | }, | |
185 | } |
|
191 | } | |
186 |
|
192 | |||
187 |
|
193 | |||
188 | .. versionchanged:: 4.0 |
|
194 | .. versionchanged:: 4.0 | |
189 |
|
195 | |||
190 | ``application/json`` output is no longer double-serialized into a string. |
|
196 | ``application/json`` output is no longer double-serialized into a string. | |
191 |
|
197 | |||
192 | .. versionchanged:: 4.0 |
|
198 | .. versionchanged:: 4.0 | |
193 |
|
199 | |||
194 | mime-types are used for keys, instead of a combination of short names (``text``) |
|
200 | mime-types are used for keys, instead of a combination of short names (``text``) | |
195 | and mime-types. |
|
201 | and mime-types, and are stored in a ``data`` key, rather than the top-level. | |
|
202 | i.e. ``output.data['image/png']`` instead of ``output.png``. | |||
196 |
|
203 | |||
197 |
|
204 | |||
198 | execute_result |
|
205 | execute_result | |
199 | ************** |
|
206 | ************** | |
200 |
|
207 | |||
201 | Results of executing a cell (as created by ``displayhook`` in Python) |
|
208 | Results of executing a cell (as created by ``displayhook`` in Python) | |
202 | are stored in ``execute_result`` outputs. |
|
209 | are stored in ``execute_result`` outputs. | |
203 | `execute_result` outputs are identical to ``display_data``, |
|
210 | `execute_result` outputs are identical to ``display_data``, | |
204 | adding only a ``prompt_number`` field, which must be an integer. |
|
211 | adding only a ``prompt_number`` field, which must be an integer. | |
205 |
|
212 | |||
206 | .. sourcecode:: python |
|
213 | .. sourcecode:: python | |
207 |
|
214 | |||
208 | { |
|
215 | { | |
209 | "output_type" : "execute_result", |
|
216 | "output_type" : "execute_result", | |
210 | "prompt_number": 42, |
|
217 | "execute_result": 42, | |
211 | "text/plain" : ["multiline text data"], |
|
218 | "data" : { | |
212 | "image/png": ["base64-encoded-png-data"], |
|
219 | "text/plain" : ["multiline text data"], | |
213 | "application/json": { |
|
220 | "image/png": ["base64-encoded-png-data"], | |
214 | # JSON data is included as-is |
|
221 | "application/json": { | |
215 | "json": "data", |
|
222 | # JSON data is included as-is | |
|
223 | "json": "data", | |||
|
224 | }, | |||
216 | }, |
|
225 | }, | |
217 | "metadata" : { |
|
226 | "metadata" : { | |
218 | "image/png": { |
|
227 | "image/png": { | |
219 | "width": 640, |
|
228 | "width": 640, | |
220 | "height": 480, |
|
229 | "height": 480, | |
221 | }, |
|
230 | }, | |
222 | }, |
|
231 | }, | |
223 | } |
|
232 | } | |
224 |
|
233 | |||
225 | .. versionchanged:: 4.0 |
|
234 | .. versionchanged:: 4.0 | |
226 |
|
235 | |||
227 | ``pyout`` renamed to ``execute_result`` |
|
236 | ``pyout`` renamed to ``execute_result`` | |
228 |
|
237 | |||
|
238 | .. versionchanged:: 4.0 | |||
|
239 | ||||
|
240 | ``prompt_number`` renamed to ``execution_count`` | |||
|
241 | ||||
229 |
|
242 | |||
230 | error |
|
243 | error | |
231 | ***** |
|
244 | ***** | |
232 |
|
245 | |||
233 | Failed execution may show a traceback |
|
246 | Failed execution may show a traceback | |
234 |
|
247 | |||
235 | .. sourcecode:: python |
|
248 | .. sourcecode:: python | |
236 |
|
249 | |||
237 | { |
|
250 | { | |
238 | 'ename' : str, # Exception name, as a string |
|
251 | 'ename' : str, # Exception name, as a string | |
239 | 'evalue' : str, # Exception value, as a string |
|
252 | 'evalue' : str, # Exception value, as a string | |
240 |
|
253 | |||
241 | # The traceback will contain a list of frames, |
|
254 | # The traceback will contain a list of frames, | |
242 | # represented each as a string. |
|
255 | # represented each as a string. | |
243 | 'traceback' : list, |
|
256 | 'traceback' : list, | |
244 | } |
|
257 | } | |
245 |
|
258 | |||
246 | .. versionchanged:: 4.0 |
|
259 | .. versionchanged:: 4.0 | |
247 |
|
260 | |||
248 | ``pyerr`` renamed to ``error`` |
|
261 | ``pyerr`` renamed to ``error`` | |
249 |
|
262 | |||
250 |
|
263 | |||
251 | Raw NBConvert cells |
|
264 | Raw NBConvert cells | |
252 | ------------------- |
|
265 | ------------------- | |
253 |
|
266 | |||
254 | A raw cell is defined as content that should be included *unmodified* in :ref:`nbconvert <nbconvert>` output. |
|
267 | A raw cell is defined as content that should be included *unmodified* in :ref:`nbconvert <nbconvert>` output. | |
255 | For example, this cell could include raw LaTeX for nbconvert to pdf via latex, |
|
268 | For example, this cell could include raw LaTeX for nbconvert to pdf via latex, | |
256 | or restructured text for use in Sphinx documentation. |
|
269 | or restructured text for use in Sphinx documentation. | |
257 |
|
270 | |||
258 | The notebook authoring environment does not render raw cells. |
|
271 | The notebook authoring environment does not render raw cells. | |
259 |
|
272 | |||
260 | The only logic in a raw cell is the `format` metadata field. |
|
273 | The only logic in a raw cell is the `format` metadata field. | |
261 | If defined, it specifies which nbconvert output format is the intended target |
|
274 | If defined, it specifies which nbconvert output format is the intended target | |
262 | for the raw cell. When outputting to any other format, |
|
275 | for the raw cell. When outputting to any other format, | |
263 | the raw cell's contents will be excluded. |
|
276 | the raw cell's contents will be excluded. | |
264 | In the default case when this value is undefined, |
|
277 | In the default case when this value is undefined, | |
265 | a raw cell's contents will be included in any nbconvert output, |
|
278 | a raw cell's contents will be included in any nbconvert output, | |
266 | regardless of format. |
|
279 | regardless of format. | |
267 |
|
280 | |||
268 | .. sourcecode:: python |
|
281 | .. sourcecode:: python | |
269 |
|
282 | |||
270 | { |
|
283 | { | |
271 | "cell_type" : "raw", |
|
284 | "cell_type" : "raw", | |
272 | "metadata" : { |
|
285 | "metadata" : { | |
273 | # the mime-type of the target nbconvert format. |
|
286 | # the mime-type of the target nbconvert format. | |
274 | # nbconvert to formats other than this will exclude this cell. |
|
287 | # nbconvert to formats other than this will exclude this cell. | |
275 | "format" : "mime/type" |
|
288 | "format" : "mime/type" | |
276 | }, |
|
289 | }, | |
277 | "source" : ["some nbformat mime-type data"] |
|
290 | "source" : ["some nbformat mime-type data"] | |
278 | } |
|
291 | } |
General Comments 0
You need to be logged in to leave comments.
Login now