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