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