Debian Codegraph @

docs/using/api.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "(use/api)=\n",
8 "\n",
9 "# Python API"
10 ]
11 },
12 {
13 "cell_type": "markdown",
14 "metadata": {},
15 "source": [
16 "This page outlines how to utilise the cache programatically.\n",
17 "We step throught the three aspects illustrated in the diagram below:\n",
18 "[cacheing](use/api/cache), [staging](use/api/project) and [executing](use/api/execute).\n",
19 "\n",
20 "```{figure} images/execution_process.svg\n",
21 ":width: 500 px\n",
22 "\n",
23 "Illustration of the execution process.\n",
24 "```"
25 ]
26 },
27 {
28 "cell_type": "markdown",
29 "metadata": {},
30 "source": [
31 "```{note}\n",
32 "The full Jupyter notebook for this page can accessed here; {nb-download}`api.ipynb`.\n",
33 "Try it for yourself!\n",
34 "```"
35 ]
36 },
37 {
38 "cell_type": "markdown",
39 "metadata": {},
40 "source": [
41 "## Initialisation"
42 ]
43 },
44 {
45 "cell_type": "code",
46 "execution_count": 1,
47 "metadata": {},
48 "outputs": [],
49 "source": [
50 "from pathlib import Path\n",
51 "import nbformat as nbf\n",
52 "from jupyter_cache import get_cache\n",
53 "from jupyter_cache.base import CacheBundleIn\n",
54 "from jupyter_cache.executors import load_executor, list_executors\n",
55 "from jupyter_cache.utils import (\n",
56 " tabulate_cache_records, \n",
57 " tabulate_project_records\n",
58 ")"
59 ]
60 },
61 {
62 "cell_type": "markdown",
63 "metadata": {},
64 "source": [
65 "First we setup a cache and ensure that it is cleared.\n",
66 "\n",
67 "```{important}\n",
68 "Clearing a cache wipes its entire content, including any settings (such as cache limit).\n",
69 "```"
70 ]
71 },
72 {
73 "cell_type": "code",
74 "execution_count": 2,
75 "metadata": {},
76 "outputs": [
77 {
78 "data": {
79 "text/plain": [
80 "JupyterCacheBase('/Users/chrisjsewell/Documents/GitHub/jupyter-cache/docs/using/.jupyter_cache')"
81 ]
82 },
83 "execution_count": 2,
84 "metadata": {},
85 "output_type": "execute_result"
86 }
87 ],
88 "source": [
89 "cache = get_cache(\".jupyter_cache\")\n",
90 "cache.clear_cache()\n",
91 "cache"
92 ]
93 },
94 {
95 "cell_type": "code",
96 "execution_count": 3,
97 "metadata": {},
98 "outputs": [
99 {
100 "name": "stdout",
101 "output_type": "stream",
102 "text": [
103 "[]\n",
104 "[]\n"
105 ]
106 }
107 ],
108 "source": [
109 "print(cache.list_cache_records())\n",
110 "print(cache.list_project_records())"
111 ]
112 },
113 {
114 "cell_type": "markdown",
115 "metadata": {},
116 "source": [
117 "(use/api/cache)=\n",
118 "\n",
119 "## Cacheing Notebooks"
120 ]
121 },
122 {
123 "cell_type": "markdown",
124 "metadata": {},
125 "source": [
126 "To directly cache a notebook:"
127 ]
128 },
129 {
130 "cell_type": "code",
131 "execution_count": 4,
132 "metadata": {},
133 "outputs": [
134 {
135 "data": {
136 "text/plain": [
137 "NbCacheRecord(pk=1)"
138 ]
139 },
140 "execution_count": 4,
141 "metadata": {},
142 "output_type": "execute_result"
143 }
144 ],
145 "source": [
146 "record = cache.cache_notebook_file(\n",
147 " path=Path(\"example_nbs\", \"basic.ipynb\")\n",
148 ")\n",
149 "record"
150 ]
151 },
152 {
153 "cell_type": "markdown",
154 "metadata": {},
155 "source": [
156 "This will add a physical copy of the notebook to tha cache (stripped of any text cells) and return the record that has been added to the cache database.\n",
157 "\n",
158 "```{important}\n",
159 "The returned record is static, as in it will not update if the database is updated.\n",
160 "```\n",
161 "\n",
162 "The record stores metadata for the notebook:"
163 ]
164 },
165 {
166 "cell_type": "code",
167 "execution_count": 5,
168 "metadata": {},
169 "outputs": [
170 {
171 "data": {
172 "text/plain": [
173 "{'description': '',\n",
174 " 'hashkey': '94c17138f782c75df59e989fffa64e3a',\n",
175 " 'created': datetime.datetime(2022, 1, 12, 15, 15, 27, 255299),\n",
176 " 'accessed': datetime.datetime(2022, 1, 12, 15, 15, 27, 255312),\n",
177 " 'data': {},\n",
178 " 'uri': 'example_nbs/basic.ipynb',\n",
179 " 'pk': 1}"
180 ]
181 },
182 "execution_count": 5,
183 "metadata": {},
184 "output_type": "execute_result"
185 }
186 ],
187 "source": [
188 "record.to_dict()"
189 ]
190 },
191 {
192 "cell_type": "markdown",
193 "metadata": {},
194 "source": [
195 "```{important}\n",
196 "The URI that the notebook is read from is stored, but does not have an impact on later comparison of notebooks. They are only compared by their internal content.\n",
197 "```"
198 ]
199 },
200 {
201 "cell_type": "markdown",
202 "metadata": {},
203 "source": [
204 "We can retrive cache records by their Primary Key (pk): "
205 ]
206 },
207 {
208 "cell_type": "code",
209 "execution_count": 6,
210 "metadata": {},
211 "outputs": [
212 {
213 "data": {
214 "text/plain": [
215 "[NbCacheRecord(pk=1)]"
216 ]
217 },
218 "execution_count": 6,
219 "metadata": {},
220 "output_type": "execute_result"
221 }
222 ],
223 "source": [
224 "cache.list_cache_records()"
225 ]
226 },
227 {
228 "cell_type": "code",
229 "execution_count": 7,
230 "metadata": {},
231 "outputs": [
232 {
233 "data": {
234 "text/plain": [
235 "NbCacheRecord(pk=1)"
236 ]
237 },
238 "execution_count": 7,
239 "metadata": {},
240 "output_type": "execute_result"
241 }
242 ],
243 "source": [
244 "cache.get_cache_record(1)"
245 ]
246 },
247 {
248 "cell_type": "markdown",
249 "metadata": {},
250 "source": [
251 "To load the entire notebook that is related to a pk:"
252 ]
253 },
254 {
255 "cell_type": "code",
256 "execution_count": 8,
257 "metadata": {},
258 "outputs": [
259 {
260 "data": {
261 "text/plain": [
262 "CacheBundleOut(nb=Notebook(cells=1), record=NbCacheRecord(pk=1), artifacts=NbArtifacts(paths=0))"
263 ]
264 },
265 "execution_count": 8,
266 "metadata": {},
267 "output_type": "execute_result"
268 }
269 ],
270 "source": [
271 "nb_bundle = cache.get_cache_bundle(1)\n",
272 "nb_bundle"
273 ]
274 },
275 {
276 "cell_type": "code",
277 "execution_count": 9,
278 "metadata": {},
279 "outputs": [
280 {
281 "data": {
282 "text/plain": [
283 "{'cells': [{'cell_type': 'code',\n",
284 " 'execution_count': 1,\n",
285 " 'metadata': {},\n",
286 " 'outputs': [{'name': 'stdout', 'output_type': 'stream', 'text': '1\\n'}],\n",
287 " 'source': 'a=1\\nprint(a)'}],\n",
288 " 'metadata': {'kernelspec': {'display_name': 'Python 3',\n",
289 " 'language': 'python',\n",
290 " 'name': 'python3'},\n",
291 " 'language_info': {'codemirror_mode': {'name': 'ipython', 'version': 3},\n",
292 " 'file_extension': '.py',\n",
293 " 'mimetype': 'text/x-python',\n",
294 " 'name': 'python',\n",
295 " 'nbconvert_exporter': 'python',\n",
296 " 'pygments_lexer': 'ipython3',\n",
297 " 'version': '3.6.1'},\n",
298 " 'test_name': 'notebook1'},\n",
299 " 'nbformat': 4,\n",
300 " 'nbformat_minor': 2}"
301 ]
302 },
303 "execution_count": 9,
304 "metadata": {},
305 "output_type": "execute_result"
306 }
307 ],
308 "source": [
309 "nb_bundle.nb"
310 ]
311 },
312 {
313 "cell_type": "markdown",
314 "metadata": {},
315 "source": [
316 "Trying to add a notebook to the cache that matches an existing one will result in a error, since the cache ensures that all notebook hashes are unique:"
317 ]
318 },
319 {
320 "cell_type": "code",
321 "execution_count": 10,
322 "metadata": {
323 "tags": [
324 "raises-exception"
325 ]
326 },
327 "outputs": [
328 {
329 "ename": "CachingError",
330 "evalue": "Notebook already exists in cache and overwrite=False.",
331 "output_type": "error",
332 "traceback": [
333 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
334 "\u001b[0;31mCachingError\u001b[0m Traceback (most recent call last)",
335 "\u001b[0;32m/var/folders/t2/xbl15_3n4tsb1vr_ccmmtmbr0000gn/T/ipykernel_99993/3576020660.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m record = cache.cache_notebook_file(\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mpath\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mPath\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"example_nbs\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"basic.ipynb\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m )\n",
336 "\u001b[0;32m~/Documents/GitHub/jupyter-cache/jupyter_cache/cache/main.py\u001b[0m in \u001b[0;36mcache_notebook_file\u001b[0;34m(self, path, uri, artifacts, data, check_validity, overwrite)\u001b[0m\n\u001b[1;32m 268\u001b[0m \"\"\"\n\u001b[1;32m 269\u001b[0m \u001b[0mnotebook\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnbf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnbf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mNO_CONVERT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 270\u001b[0;31m return self.cache_notebook_bundle(\n\u001b[0m\u001b[1;32m 271\u001b[0m CacheBundleIn(\n\u001b[1;32m 272\u001b[0m \u001b[0mnotebook\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
337 "\u001b[0;32m~/Documents/GitHub/jupyter-cache/jupyter_cache/cache/main.py\u001b[0m in \u001b[0;36mcache_notebook_bundle\u001b[0;34m(self, bundle, check_validity, overwrite, description)\u001b[0m\n\u001b[1;32m 213\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexists\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 214\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0moverwrite\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 215\u001b[0;31m raise CachingError(\n\u001b[0m\u001b[1;32m 216\u001b[0m \u001b[0;34m\"Notebook already exists in cache and overwrite=False.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 217\u001b[0m )\n",
338 "\u001b[0;31mCachingError\u001b[0m: Notebook already exists in cache and overwrite=False."
339 ]
340 }
341 ],
342 "source": [
343 "record = cache.cache_notebook_file(\n",
344 " path=Path(\"example_nbs\", \"basic.ipynb\")\n",
345 ")"
346 ]
347 },
348 {
349 "cell_type": "markdown",
350 "metadata": {},
351 "source": [
352 "If we load a notebook external to the cache, then we can try to match it to one\n",
353 "stored inside the cache:"
354 ]
355 },
356 {
357 "cell_type": "code",
358 "execution_count": 11,
359 "metadata": {},
360 "outputs": [
361 {
362 "data": {
363 "text/plain": [
364 "{'cells': [{'cell_type': 'markdown',\n",
365 " 'metadata': {},\n",
366 " 'source': '# a title\\n\\nsome text\\n'},\n",
367 " {'cell_type': 'code',\n",
368 " 'execution_count': 1,\n",
369 " 'metadata': {},\n",
370 " 'source': 'a=1\\nprint(a)',\n",
371 " 'outputs': [{'name': 'stdout', 'output_type': 'stream', 'text': '1\\n'}]}],\n",
372 " 'metadata': {'test_name': 'notebook1',\n",
373 " 'kernelspec': {'display_name': 'Python 3',\n",
374 " 'language': 'python',\n",
375 " 'name': 'python3'},\n",
376 " 'language_info': {'codemirror_mode': {'name': 'ipython', 'version': 3},\n",
377 " 'file_extension': '.py',\n",
378 " 'mimetype': 'text/x-python',\n",
379 " 'name': 'python',\n",
380 " 'nbconvert_exporter': 'python',\n",
381 " 'pygments_lexer': 'ipython3',\n",
382 " 'version': '3.6.1'}},\n",
383 " 'nbformat': 4,\n",
384 " 'nbformat_minor': 2}"
385 ]
386 },
387 "execution_count": 11,
388 "metadata": {},
389 "output_type": "execute_result"
390 }
391 ],
392 "source": [
393 "notebook = nbf.read(str(Path(\"example_nbs\", \"basic.ipynb\")), 4)\n",
394 "notebook"
395 ]
396 },
397 {
398 "cell_type": "code",
399 "execution_count": 12,
400 "metadata": {},
401 "outputs": [
402 {
403 "data": {
404 "text/plain": [
405 "NbCacheRecord(pk=1)"
406 ]
407 },
408 "execution_count": 12,
409 "metadata": {},
410 "output_type": "execute_result"
411 }
412 ],
413 "source": [
414 "cache.match_cache_notebook(notebook)"
415 ]
416 },
417 {
418 "cell_type": "markdown",
419 "metadata": {},
420 "source": [
421 "Notebooks are matched by a hash based only on aspects of the notebook that will affect its execution (and hence outputs). So changing text cells will match the cached notebook:"
422 ]
423 },
424 {
425 "cell_type": "code",
426 "execution_count": 13,
427 "metadata": {},
428 "outputs": [],
429 "source": [
430 "notebook.cells[0].source = \"change some text\""
431 ]
432 },
433 {
434 "cell_type": "code",
435 "execution_count": 14,
436 "metadata": {},
437 "outputs": [
438 {
439 "data": {
440 "text/plain": [
441 "NbCacheRecord(pk=1)"
442 ]
443 },
444 "execution_count": 14,
445 "metadata": {},
446 "output_type": "execute_result"
447 }
448 ],
449 "source": [
450 "cache.match_cache_notebook(notebook)"
451 ]
452 },
453 {
454 "cell_type": "markdown",
455 "metadata": {},
456 "source": [
457 "But changing code cells will result in a different hash, and so will not be matched:"
458 ]
459 },
460 {
461 "cell_type": "code",
462 "execution_count": 15,
463 "metadata": {},
464 "outputs": [],
465 "source": [
466 "notebook.cells[1].source = \"change some source code\""
467 ]
468 },
469 {
470 "cell_type": "code",
471 "execution_count": 16,
472 "metadata": {
473 "tags": [
474 "raises-exception"
475 ]
476 },
477 "outputs": [
478 {
479 "ename": "KeyError",
480 "evalue": "'Cache record not found for NB with hashkey: 07e6a47c8c180cb7851ede6dbb088769'",
481 "output_type": "error",
482 "traceback": [
483 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
484 "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
485 "\u001b[0;32m/var/folders/t2/xbl15_3n4tsb1vr_ccmmtmbr0000gn/T/ipykernel_99993/941642554.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcache\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatch_cache_notebook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnotebook\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
486 "\u001b[0;32m~/Documents/GitHub/jupyter-cache/jupyter_cache/cache/main.py\u001b[0m in \u001b[0;36mmatch_cache_notebook\u001b[0;34m(self, nb)\u001b[0m\n\u001b[1;32m 333\u001b[0m \"\"\"\n\u001b[1;32m 334\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhashkey\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcreate_hashed_notebook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 335\u001b[0;31m \u001b[0mcache_record\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mNbCacheRecord\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecord_from_hashkey\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhashkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 336\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcache_record\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 337\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
487 "\u001b[0;32m~/Documents/GitHub/jupyter-cache/jupyter_cache/cache/db.py\u001b[0m in \u001b[0;36mrecord_from_hashkey\u001b[0;34m(hashkey, db)\u001b[0m\n\u001b[1;32m 158\u001b[0m )\n\u001b[1;32m 159\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 160\u001b[0;31m raise KeyError(\n\u001b[0m\u001b[1;32m 161\u001b[0m \u001b[0;34m\"Cache record not found for NB with hashkey: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhashkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 162\u001b[0m )\n",
488 "\u001b[0;31mKeyError\u001b[0m: 'Cache record not found for NB with hashkey: 07e6a47c8c180cb7851ede6dbb088769'"
489 ]
490 }
491 ],
492 "source": [
493 "cache.match_cache_notebook(notebook)"
494 ]
495 },
496 {
497 "cell_type": "markdown",
498 "metadata": {},
499 "source": [
500 "To understand the difference between an external notebook, and one stored in the cache, we can 'diff' them:"
501 ]
502 },
503 {
504 "cell_type": "code",
505 "execution_count": 17,
506 "metadata": {},
507 "outputs": [
508 {
509 "name": "stdout",
510 "output_type": "stream",
511 "text": [
512 "nbdiff\n",
513 "--- cached pk=1\n",
514 "+++ other: \n",
515 "\u001b[34m## inserted before nb/cells/0:\u001b[0m\n",
516 "\u001b[32m+ code cell:\n",
517 "\u001b[32m+ execution_count: 1\n",
518 "\u001b[32m+ source:\n",
519 "\u001b[32m+ change some source code\n",
520 "\u001b[32m+ outputs:\n",
521 "\u001b[32m+ output 0:\n",
522 "\u001b[32m+ output_type: stream\n",
523 "\u001b[32m+ name: stdout\n",
524 "\u001b[32m+ text:\n",
525 "\u001b[32m+ 1\n",
526 "\n",
527 "\u001b[0m\u001b[34m## deleted nb/cells/0:\u001b[0m\n",
528 "\u001b[31m- code cell:\n",
529 "\u001b[31m- execution_count: 1\n",
530 "\u001b[31m- source:\n",
531 "\u001b[31m- a=1\n",
532 "\u001b[31m- print(a)\n",
533 "\u001b[31m- outputs:\n",
534 "\u001b[31m- output 0:\n",
535 "\u001b[31m- output_type: stream\n",
536 "\u001b[31m- name: stdout\n",
537 "\u001b[31m- text:\n",
538 "\u001b[31m- 1\n",
539 "\n",
540 "\u001b[0m\n"
541 ]
542 }
543 ],
544 "source": [
545 "print(cache.diff_nbnode_with_cache(1, notebook, as_str=True))"
546 ]
547 },
548 {
549 "cell_type": "markdown",
550 "metadata": {},
551 "source": [
552 "If we cache this altered notebook, note that this will not remove the previously cached notebook:"
553 ]
554 },
555 {
556 "cell_type": "code",
557 "execution_count": 18,
558 "metadata": {},
559 "outputs": [
560 {
561 "data": {
562 "text/plain": [
563 "NbCacheRecord(pk=2)"
564 ]
565 },
566 "execution_count": 18,
567 "metadata": {},
568 "output_type": "execute_result"
569 }
570 ],
571 "source": [
572 "nb_bundle = CacheBundleIn(\n",
573 " nb=notebook,\n",
574 " uri=Path(\"example_nbs\", \"basic.ipynb\"),\n",
575 " data={\"tag\": \"mytag\"}\n",
576 ")\n",
577 "cache.cache_notebook_bundle(nb_bundle)"
578 ]
579 },
580 {
581 "cell_type": "code",
582 "execution_count": 19,
583 "metadata": {},
584 "outputs": [
585 {
586 "name": "stdout",
587 "output_type": "stream",
588 "text": [
589 " ID Origin URI Created Accessed Hashkey\n",
590 "---- ------------ ---------------- ---------------- --------------------------------\n",
591 " 2 basic.ipynb 2022-01-12 15:16 2022-01-12 15:16 07e6a47c8c180cb7851ede6dbb088769\n",
592 " 1 basic.ipynb 2022-01-12 15:15 2022-01-12 15:16 94c17138f782c75df59e989fffa64e3a\n"
593 ]
594 }
595 ],
596 "source": [
597 "print(tabulate_cache_records(\n",
598 " cache.list_cache_records(), path_length=1, hashkeys=True\n",
599 "))"
600 ]
601 },
602 {
603 "cell_type": "markdown",
604 "metadata": {},
605 "source": [
606 "Notebooks are retained in the cache, until the cache limit is reached,\n",
607 "at which point the oldest notebooks are removed."
608 ]
609 },
610 {
611 "cell_type": "code",
612 "execution_count": 20,
613 "metadata": {},
614 "outputs": [
615 {
616 "data": {
617 "text/plain": [
618 "1000"
619 ]
620 },
621 "execution_count": 20,
622 "metadata": {},
623 "output_type": "execute_result"
624 }
625 ],
626 "source": [
627 "cache.get_cache_limit()"
628 ]
629 },
630 {
631 "cell_type": "code",
632 "execution_count": 21,
633 "metadata": {},
634 "outputs": [],
635 "source": [
636 "cache.change_cache_limit(100)"
637 ]
638 },
639 {
640 "cell_type": "markdown",
641 "metadata": {},
642 "source": [
643 "(use/api/project)=\n",
644 "\n",
645 "## Staging Notebooks for Execution"
646 ]
647 },
648 {
649 "cell_type": "markdown",
650 "metadata": {},
651 "source": [
652 "Notebooks can be staged, by adding the path as a stage record.\n",
653 "\n",
654 "```{important}\n",
655 "This does not physically add the notebook to the cache,\n",
656 "merely store its URI, for later use.\n",
657 "```"
658 ]
659 },
660 {
661 "cell_type": "code",
662 "execution_count": 22,
663 "metadata": {},
664 "outputs": [
665 {
666 "data": {
667 "text/plain": [
668 "NbProjectRecord(pk=1)"
669 ]
670 },
671 "execution_count": 22,
672 "metadata": {},
673 "output_type": "execute_result"
674 }
675 ],
676 "source": [
677 "record = cache.add_nb_to_project(Path(\"example_nbs\", \"basic.ipynb\"))\n",
678 "record"
679 ]
680 },
681 {
682 "cell_type": "code",
683 "execution_count": 23,
684 "metadata": {},
685 "outputs": [
686 {
687 "data": {
688 "text/plain": [
689 "{'uri': '/Users/chrisjsewell/Documents/GitHub/jupyter-cache/docs/using/example_nbs/basic.ipynb',\n",
690 " 'assets': [],\n",
691 " 'created': datetime.datetime(2022, 1, 12, 15, 16, 27, 64960),\n",
692 " 'traceback': '',\n",
693 " 'read_data': {'name': 'nbformat', 'type': 'plugin'},\n",
694 " 'pk': 1}"
695 ]
696 },
697 "execution_count": 23,
698 "metadata": {},
699 "output_type": "execute_result"
700 }
701 ],
702 "source": [
703 "record.to_dict()"
704 ]
705 },
706 {
707 "cell_type": "markdown",
708 "metadata": {},
709 "source": [
710 "If the staged notbook relates to one in the cache, we will be able to retrieve the cache record:"
711 ]
712 },
713 {
714 "cell_type": "code",
715 "execution_count": 24,
716 "metadata": {},
717 "outputs": [
718 {
719 "data": {
720 "text/plain": [
721 "NbCacheRecord(pk=1)"
722 ]
723 },
724 "execution_count": 24,
725 "metadata": {},
726 "output_type": "execute_result"
727 }
728 ],
729 "source": [
730 "cache.get_cached_project_nb(1)"
731 ]
732 },
733 {
734 "cell_type": "code",
735 "execution_count": 25,
736 "metadata": {},
737 "outputs": [
738 {
739 "name": "stdout",
740 "output_type": "stream",
741 "text": [
742 " ID URI Reader Added Status\n",
743 "---- ----------------------- -------- ---------------- --------\n",
744 " 1 example_nbs/basic.ipynb nbformat 2022-01-12 15:16 ✅ [1]\n"
745 ]
746 }
747 ],
748 "source": [
749 "print(tabulate_project_records(\n",
750 " cache.list_project_records(), path_length=2, cache=cache\n",
751 "))"
752 ]
753 },
754 {
755 "cell_type": "markdown",
756 "metadata": {},
757 "source": [
758 "We can also retrieve a *merged* notebook.\n",
759 "This is a copy of the source notebook with the following added to it from the cached notebook:\n",
760 "\n",
761 "- Selected notebook metadata keys (generally only those keys that affect its execution)\n",
762 "- All code cells, with their outputs and metadata \n",
763 " (only selected metadata can be merged if `cell_meta` is not `None`)\n",
764 " \n",
765 "In this way we create a notebook that is *fully* up-to-date for both its code and textual content:"
766 ]
767 },
768 {
769 "cell_type": "code",
770 "execution_count": 26,
771 "metadata": {},
772 "outputs": [
773 {
774 "data": {
775 "text/plain": [
776 "(1,\n",
777 " {'cells': [{'cell_type': 'markdown',\n",
778 " 'metadata': {},\n",
779 " 'source': '# a title\\n\\nsome text\\n'},\n",
780 " {'cell_type': 'code',\n",
781 " 'execution_count': 1,\n",
782 " 'metadata': {},\n",
783 " 'outputs': [{'name': 'stdout', 'output_type': 'stream', 'text': '1\\n'}],\n",
784 " 'source': 'a=1\\nprint(a)'}],\n",
785 " 'metadata': {'test_name': 'notebook1',\n",
786 " 'kernelspec': {'display_name': 'Python 3',\n",
787 " 'language': 'python',\n",
788 " 'name': 'python3'},\n",
789 " 'language_info': {'codemirror_mode': {'name': 'ipython', 'version': 3},\n",
790 " 'file_extension': '.py',\n",
791 " 'mimetype': 'text/x-python',\n",
792 " 'name': 'python',\n",
793 " 'nbconvert_exporter': 'python',\n",
794 " 'pygments_lexer': 'ipython3',\n",
795 " 'version': '3.6.1'}},\n",
796 " 'nbformat': 4,\n",
797 " 'nbformat_minor': 2})"
798 ]
799 },
800 "execution_count": 26,
801 "metadata": {},
802 "output_type": "execute_result"
803 }
804 ],
805 "source": [
806 "cache.merge_match_into_file(\n",
807 " cache.get_project_record(1).uri,\n",
808 " nb_meta=('kernelspec', 'language_info', 'widgets'),\n",
809 " cell_meta=None\n",
810 ")"
811 ]
812 },
813 {
814 "cell_type": "markdown",
815 "metadata": {},
816 "source": [
817 "If we add a notebook that cannot be found in the cache, it will be listed for execution:"
818 ]
819 },
820 {
821 "cell_type": "code",
822 "execution_count": 27,
823 "metadata": {},
824 "outputs": [
825 {
826 "data": {
827 "text/plain": [
828 "NbProjectRecord(pk=2)"
829 ]
830 },
831 "execution_count": 27,
832 "metadata": {},
833 "output_type": "execute_result"
834 }
835 ],
836 "source": [
837 "record = cache.add_nb_to_project(Path(\"example_nbs\", \"basic_failing.ipynb\"))\n",
838 "record"
839 ]
840 },
841 {
842 "cell_type": "code",
843 "execution_count": 28,
844 "metadata": {},
845 "outputs": [],
846 "source": [
847 "cache.get_cached_project_nb(2) # returns None"
848 ]
849 },
850 {
851 "cell_type": "code",
852 "execution_count": 29,
853 "metadata": {},
854 "outputs": [
855 {
856 "data": {
857 "text/plain": [
858 "[NbProjectRecord(pk=2)]"
859 ]
860 },
861 "execution_count": 29,
862 "metadata": {},
863 "output_type": "execute_result"
864 }
865 ],
866 "source": [
867 "cache.list_unexecuted()"
868 ]
869 },
870 {
871 "cell_type": "code",
872 "execution_count": 30,
873 "metadata": {},
874 "outputs": [
875 {
876 "name": "stdout",
877 "output_type": "stream",
878 "text": [
879 " ID URI Reader Added Status\n",
880 "---- ------------------------------- -------- ---------------- --------\n",
881 " 1 example_nbs/basic.ipynb nbformat 2022-01-12 15:16 ✅ [1]\n",
882 " 2 example_nbs/basic_failing.ipynb nbformat 2022-01-12 15:17 -\n"
883 ]
884 }
885 ],
886 "source": [
887 "print(tabulate_project_records(\n",
888 " cache.list_project_records(), path_length=2, cache=cache\n",
889 "))"
890 ]
891 },
892 {
893 "cell_type": "markdown",
894 "metadata": {},
895 "source": [
896 "To remove a notebook from the staging area:"
897 ]
898 },
899 {
900 "cell_type": "code",
901 "execution_count": 31,
902 "metadata": {},
903 "outputs": [],
904 "source": [
905 "cache.remove_nb_from_project(1)"
906 ]
907 },
908 {
909 "cell_type": "code",
910 "execution_count": 32,
911 "metadata": {},
912 "outputs": [
913 {
914 "name": "stdout",
915 "output_type": "stream",
916 "text": [
917 " ID URI Reader Added Status\n",
918 "---- ------------------------------- -------- ---------------- --------\n",
919 " 2 example_nbs/basic_failing.ipynb nbformat 2022-01-12 15:17 -\n"
920 ]
921 }
922 ],
923 "source": [
924 "print(tabulate_project_records(\n",
925 " cache.list_project_records(), path_length=2, cache=cache\n",
926 "))"
927 ]
928 },
929 {
930 "cell_type": "markdown",
931 "metadata": {},
932 "source": [
933 "(use/api/execute)=\n",
934 "\n",
935 "## Execution"
936 ]
937 },
938 {
939 "cell_type": "markdown",
940 "metadata": {},
941 "source": [
942 "If we have some staged notebooks:"
943 ]
944 },
945 {
946 "cell_type": "code",
947 "execution_count": 33,
948 "metadata": {},
949 "outputs": [
950 {
951 "data": {
952 "text/plain": [
953 "NbProjectRecord(pk=2)"
954 ]
955 },
956 "execution_count": 33,
957 "metadata": {},
958 "output_type": "execute_result"
959 }
960 ],
961 "source": [
962 "cache.clear_cache()\n",
963 "cache.add_nb_to_project(Path(\"example_nbs\", \"basic.ipynb\"))\n",
964 "cache.add_nb_to_project(Path(\"example_nbs\", \"basic_failing.ipynb\"))"
965 ]
966 },
967 {
968 "cell_type": "code",
969 "execution_count": 34,
970 "metadata": {},
971 "outputs": [
972 {
973 "name": "stdout",
974 "output_type": "stream",
975 "text": [
976 " ID URI Reader Added Status\n",
977 "---- ------------------------------- -------- ---------------- --------\n",
978 " 1 example_nbs/basic.ipynb nbformat 2022-01-12 15:17 -\n",
979 " 2 example_nbs/basic_failing.ipynb nbformat 2022-01-12 15:17 -\n"
980 ]
981 }
982 ],
983 "source": [
984 "print(tabulate_project_records(\n",
985 " cache.list_project_records(), path_length=2, cache=cache\n",
986 "))"
987 ]
988 },
989 {
990 "cell_type": "markdown",
991 "metadata": {},
992 "source": [
993 "Then we can select an executor (specified as entry points) to execute the notebook.\n",
994 "\n",
995 "```{tip}\n",
996 "To view the executors log, make sure logging is enabled,\n",
997 "or you can parse a logger directly to `load_executor()`.\n",
998 "```"
999 ]
1000 },
1001 {
1002 "cell_type": "code",
1003 "execution_count": 35,
1004 "metadata": {},
1005 "outputs": [
1006 {
1007 "data": {
1008 "text/plain": [
1009 "{'local-parallel', 'local-serial', 'temp-parallel', 'temp-serial'}"
1010 ]
1011 },
1012 "execution_count": 35,
1013 "metadata": {},
1014 "output_type": "execute_result"
1015 }
1016 ],
1017 "source": [
1018 "list_executors()"
1019 ]
1020 },
1021 {
1022 "cell_type": "code",
1023 "execution_count": 36,
1024 "metadata": {},
1025 "outputs": [
1026 {
1027 "data": {
1028 "text/plain": [
1029 "JupyterExecutorLocalSerial(cache=JupyterCacheBase('/Users/chrisjsewell/Documents/GitHub/jupyter-cache/docs/using/.jupyter_cache'))"
1030 ]
1031 },
1032 "execution_count": 36,
1033 "metadata": {},
1034 "output_type": "execute_result"
1035 }
1036 ],
1037 "source": [
1038 "from logging import basicConfig, INFO\n",
1039 "basicConfig(level=INFO)\n",
1040 "\n",
1041 "executor = load_executor(\"local-serial\", cache=cache)\n",
1042 "executor"
1043 ]
1044 },
1045 {
1046 "cell_type": "markdown",
1047 "metadata": {},
1048 "source": [
1049 "Calling `run_and_cache()` will run all staged notebooks that do not already have matches in the cache.\n",
1050 "It will return a dictionary with lists for:\n",
1051 "\n",
1052 "\n",
1053 "- **succeeded**: The notebook was executed successfully with no (or only expected) exceptions\n",
1054 "- **excepted**: A notebook cell was encountered that raised an unexpected exception\n",
1055 "- **errored**: An exception occured before/after the actual notebook execution\n",
1056 "\n",
1057 "```{tip}\n",
1058 "Code cells can be tagged with `raises-exception` to let the executor known that\n",
1059 "a cell *may* raise an exception (see [this issue on its behaviour](https://github.com/jupyter/nbconvert/issues/730)).\n",
1060 "```\n",
1061 "\n",
1062 "```{note}\n",
1063 "You can use the `filter_uris` and/or `filter_pks` options to only run selected staged notebooks.\n",
1064 "You can also specify the timeout for execution in seconds using the `timeout` option.\n",
1065 "```"
1066 ]
1067 },
1068 {
1069 "cell_type": "code",
1070 "execution_count": 37,
1071 "metadata": {},
1072 "outputs": [
1073 {
1074 "name": "stderr",
1075 "output_type": "stream",
1076 "text": [
1077 "INFO:jupyter_cache.executors.base:Executing 2 notebook(s) in serial\n",
1078 "INFO:jupyter_cache.executors.base:Executing: /Users/chrisjsewell/Documents/GitHub/jupyter-cache/docs/using/example_nbs/basic.ipynb\n",
1079 "INFO:jupyter_cache.executors.base:Execution Successful: /Users/chrisjsewell/Documents/GitHub/jupyter-cache/docs/using/example_nbs/basic.ipynb\n",
1080 "INFO:jupyter_cache.executors.base:Executing: /Users/chrisjsewell/Documents/GitHub/jupyter-cache/docs/using/example_nbs/basic_failing.ipynb\n",
1081 "WARNING:jupyter_cache.executors.base:Execution Excepted: /Users/chrisjsewell/Documents/GitHub/jupyter-cache/docs/using/example_nbs/basic_failing.ipynb\n",
1082 "CellExecutionError: An error occurred while executing the following cell:\n",
1083 "------------------\n",
1084 "raise Exception('oopsie!')\n",
1085 "------------------\n",
1086 "\n",
1087 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n",
1088 "\u001b[0;31mException\u001b[0m Traceback (most recent call last)\n",
1089 "\u001b[0;32m/var/folders/t2/xbl15_3n4tsb1vr_ccmmtmbr0000gn/T/ipykernel_1308/340246212.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n",
1090 "\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'oopsie!'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
1091 "\u001b[0m\n",
1092 "\u001b[0;31mException\u001b[0m: oopsie!\n",
1093 "Exception: oopsie!\n",
1094 "\n"
1095 ]
1096 },
1097 {
1098 "data": {
1099 "text/plain": [
1100 "ExecutorRunResult(succeeded=['/Users/chrisjsewell/Documents/GitHub/jupyter-cache/docs/using/example_nbs/basic.ipynb'], excepted=['/Users/chrisjsewell/Documents/GitHub/jupyter-cache/docs/using/example_nbs/basic_failing.ipynb'], errored=[])"
1101 ]
1102 },
1103 "execution_count": 37,
1104 "metadata": {},
1105 "output_type": "execute_result"
1106 }
1107 ],
1108 "source": [
1109 "result = executor.run_and_cache()\n",
1110 "result"
1111 ]
1112 },
1113 {
1114 "cell_type": "markdown",
1115 "metadata": {},
1116 "source": [
1117 "Successfully executed notebooks will be added to the cache, and data about their execution (such as time taken) will be stored in the cache record:"
1118 ]
1119 },
1120 {
1121 "cell_type": "code",
1122 "execution_count": 38,
1123 "metadata": {},
1124 "outputs": [
1125 {
1126 "data": {
1127 "text/plain": [
1128 "[NbCacheRecord(pk=1)]"
1129 ]
1130 },
1131 "execution_count": 38,
1132 "metadata": {},
1133 "output_type": "execute_result"
1134 }
1135 ],
1136 "source": [
1137 "cache.list_cache_records()"
1138 ]
1139 },
1140 {
1141 "cell_type": "code",
1142 "execution_count": 39,
1143 "metadata": {},
1144 "outputs": [
1145 {
1146 "data": {
1147 "text/plain": [
1148 "{'description': '',\n",
1149 " 'hashkey': '94c17138f782c75df59e989fffa64e3a',\n",
1150 " 'created': datetime.datetime(2022, 1, 12, 15, 17, 45, 471862),\n",
1151 " 'accessed': datetime.datetime(2022, 1, 12, 15, 17, 45, 471871),\n",
1152 " 'data': {'execution_seconds': 1.8344826350000005},\n",
1153 " 'uri': '/Users/chrisjsewell/Documents/GitHub/jupyter-cache/docs/using/example_nbs/basic.ipynb',\n",
1154 " 'pk': 1}"
1155 ]
1156 },
1157 "execution_count": 39,
1158 "metadata": {},
1159 "output_type": "execute_result"
1160 }
1161 ],
1162 "source": [
1163 "record = cache.get_cache_record(1)\n",
1164 "record.to_dict()"
1165 ]
1166 },
1167 {
1168 "cell_type": "markdown",
1169 "metadata": {},
1170 "source": [
1171 "Notebooks which failed to run will **not** be added to the cache,\n",
1172 "but details about their execution (including the exception traceback)\n",
1173 "will be added to the stage record:"
1174 ]
1175 },
1176 {
1177 "cell_type": "code",
1178 "execution_count": 40,
1179 "metadata": {},
1180 "outputs": [
1181 {
1182 "name": "stdout",
1183 "output_type": "stream",
1184 "text": [
1185 "Traceback (most recent call last):\n",
1186 " File \"/Users/chrisjsewell/Documents/GitHub/jupyter-cache/jupyter_cache/executors/utils.py\", line 58, in single_nb_execution\n",
1187 " executenb(\n",
1188 " File \"/Users/chrisjsewell/Documents/GitHub/jupyter-cache/.tox/py38/lib/python3.8/site-packages/nbclient/client.py\", line 1093, in execute\n",
1189 " return NotebookClient(nb=nb, resources=resources, km=km, **kwargs).execute()\n",
1190 " File \"/Users/chrisjsewell/Documents/GitHub/jupyter-cache/.tox/py38/lib/python3.8/site-packages/nbclient/util.py\", line 84, in wrapped\n",
1191 " return just_run(coro(*args, **kwargs))\n",
1192 " File \"/Users/chrisjsewell/Documents/GitHub/jupyter-cache/.tox/py38/lib/python3.8/site-packages/nbclient/util.py\", line 62, in just_run\n",
1193 " return loop.run_until_complete(coro)\n",
1194 " File \"/Users/chrisjsewell/Documents/GitHub/jupyter-cache/.tox/py38/lib/python3.8/site-packages/nest_asyncio.py\", line 81, in run_until_complete\n",
1195 " return f.result()\n",
1196 " File \"/Users/chrisjsewell/Documents/GitHub/jupyter-cache/.tox/py38/lib/python3.8/asyncio/futures.py\", line 178, in result\n",
1197 " raise self._exception\n",
1198 " File \"/Users/chrisjsewell/Documents/GitHub/jupyter-cache/.tox/py38/lib/python3.8/asyncio/tasks.py\", line 280, in __step\n",
1199 " result = coro.send(None)\n",
1200 " File \"/Users/chrisjsewell/Documents/GitHub/jupyter-cache/.tox/py38/lib/python3.8/site-packages/nbclient/client.py\", line 559, in async_execute\n",
1201 " await self.async_execute_cell(\n",
1202 " File \"/Users/chrisjsewell/Documents/GitHub/jupyter-cache/.tox/py38/lib/python3.8/site-packages/nbclient/client.py\", line 854, in async_execute_cell\n",
1203 " self._check_raise_for_error(cell, exec_reply)\n",
1204 " File \"/Users/chrisjsewell/Documents/GitHub/jupyter-cache/.tox/py38/lib/python3.8/site-packages/nbclient/client.py\", line 756, in _check_raise_for_error\n",
1205 " raise CellExecutionError.from_cell_and_msg(cell, exec_reply_content)\n",
1206 "nbclient.exceptions.CellExecutionError: An error occurred while executing the following cell:\n",
1207 "------------------\n",
1208 "raise Exception('oopsie!')\n",
1209 "------------------\n",
1210 "\n",
1211 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n",
1212 "\u001b[0;31mException\u001b[0m Traceback (most recent call last)\n",
1213 "\u001b[0;32m/var/folders/t2/xbl15_3n4tsb1vr_ccmmtmbr0000gn/T/ipykernel_1308/340246212.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n",
1214 "\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'oopsie!'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
1215 "\u001b[0m\n",
1216 "\u001b[0;31mException\u001b[0m: oopsie!\n",
1217 "Exception: oopsie!\n",
1218 "\n",
1219 "\n"
1220 ]
1221 }
1222 ],
1223 "source": [
1224 "record = cache.get_project_record(2)\n",
1225 "print(record.traceback)"
1226 ]
1227 },
1228 {
1229 "cell_type": "markdown",
1230 "metadata": {},
1231 "source": [
1232 "We now have two staged records, and one cache record:"
1233 ]
1234 },
1235 {
1236 "cell_type": "code",
1237 "execution_count": 41,
1238 "metadata": {},
1239 "outputs": [
1240 {
1241 "name": "stdout",
1242 "output_type": "stream",
1243 "text": [
1244 " ID URI Reader Added Status\n",
1245 "---- ------------------------------- -------- ---------------- --------\n",
1246 " 1 example_nbs/basic.ipynb nbformat 2022-01-12 15:17 ✅ [1]\n",
1247 " 2 example_nbs/basic_failing.ipynb nbformat 2022-01-12 15:17 ❌\n"
1248 ]
1249 }
1250 ],
1251 "source": [
1252 "print(tabulate_project_records(\n",
1253 " cache.list_project_records(), path_length=2, cache=cache\n",
1254 "))"
1255 ]
1256 },
1257 {
1258 "cell_type": "code",
1259 "execution_count": 42,
1260 "metadata": {},
1261 "outputs": [
1262 {
1263 "name": "stdout",
1264 "output_type": "stream",
1265 "text": [
1266 " ID Origin URI Created Accessed Hashkey\n",
1267 "---- ------------ ---------------- ---------------- --------------------------------\n",
1268 " 1 basic.ipynb 2022-01-12 15:17 2022-01-12 15:17 94c17138f782c75df59e989fffa64e3a\n"
1269 ]
1270 }
1271 ],
1272 "source": [
1273 "print(tabulate_cache_records(\n",
1274 " cache.list_cache_records(), path_length=1, hashkeys=True\n",
1275 "))"
1276 ]
1277 },
1278 {
1279 "cell_type": "markdown",
1280 "metadata": {},
1281 "source": [
1282 "### Timeout\n",
1283 "A **timeout** argument can also be passed to `run_and_cache()` which takes value in seconds.\n",
1284 "Alternatively, timeout can also be specified inside the notebook metadata:\n",
1285 "\n",
1286 "```\n",
1287 "'execution': {\n",
1288 " 'timeout': 30\n",
1289 " }\n",
1290 "```\n",
1291 "```{note}\n",
1292 "Timeout specified in notebook metadata will take precedence over the one passed as an argument to `run_and_cache()`.\n",
1293 "```"
1294 ]
1295 }
1296 ],
1297 "metadata": {
1298 "celltoolbar": "Tags",
1299 "interpreter": {
1300 "hash": "8398b65b1e6feb38b0506d5ab1aedf8bf63748a9844a9c81ed9242850234e24f"
1301 },
1302 "kernelspec": {
1303 "display_name": "Coconut",
1304 "language": "python",
1305 "name": "python3"
1306 },
1307 "language_info": {
1308 "codemirror_mode": {
1309 "name": "ipython",
1310 "version": 3
1311 },
1312 "file_extension": ".py",
1313 "mimetype": "text/x-python",
1314 "name": "python",
1315 "nbconvert_exporter": "python",
1316 "pygments_lexer": "ipython3",
1317 "version": "3.8.12"
1318 }
1319 },
1320 "nbformat": 4,
1321 "nbformat_minor": 2
1322 }