| 1 |
# Jupyter Cache |
| 2 |
|
| 3 |
Execute and cache multiple Jupyter Notebook-like files via an [API](use/api) and [CLI](use/cli). |
| 4 |
|
| 5 |
🤓 Smart re-execution |
| 6 |
: Notebooks will only be re-executed when **code cells** have changed (or code related metadata), not Markdown/Raw cells. |
| 7 |
|
| 8 |
🧩 Pluggable execution modes |
| 9 |
: Select the executor for notebooks, including serial and parallel execution |
| 10 |
|
| 11 |
📈 Execution reports |
| 12 |
: Timing statistics and exception tracebacks are stored for analysis |
| 13 |
|
| 14 |
📖 [jupytext](https://jupytext.readthedocs.io) integration |
| 15 |
: Read and execute notebooks written in multiple formats |
| 16 |
|
| 17 |
## Why use jupyter-cache? |
| 18 |
|
| 19 |
If you have a number of notebooks whose execution outputs you want to ensure are kept up to date, without having to re-execute them every time (particularly for long running code, or text-based formats that do not store the outputs). |
| 20 |
|
| 21 |
The notebooks must have deterministic execution outputs: |
| 22 |
|
| 23 |
- You use the same environment to run them (e.g. the same installed packages) |
| 24 |
- They run no non-deterministic code (e.g. random numbers) |
| 25 |
- They do not depend on external resources (e.g. files or network connections) that change over time |
| 26 |
|
| 27 |
For example, it is utilised by [jupyter-book](https://jupyterbook.org/content/execute.html#caching-the-notebook-execution), to allow for fast document re-builds. |
| 28 |
|
| 29 |
## Installation |
| 30 |
|
| 31 |
Install `jupyter-cache`, via pip or Conda: |
| 32 |
|
| 33 |
```bash |
| 34 |
pip install jupyter-cache |
| 35 |
``` |
| 36 |
|
| 37 |
```bash |
| 38 |
conda install jupyter-cache |
| 39 |
``` |
| 40 |
|
| 41 |
## Quick-start |
| 42 |
|
| 43 |
```{jcache-clear} |
| 44 |
``` |
| 45 |
|
| 46 |
Add one or more source notebook files to the "project" (a folder containing a database and a cache of executed notebooks): |
| 47 |
|
| 48 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 49 |
:command: add |
| 50 |
:args: tests/notebooks/basic_unrun.ipynb tests/notebooks/basic_failing.ipynb |
| 51 |
:input: y |
| 52 |
``` |
| 53 |
|
| 54 |
These files are now ready for execution: |
| 55 |
|
| 56 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 57 |
:command: list |
| 58 |
``` |
| 59 |
|
| 60 |
Now run the execution: |
| 61 |
|
| 62 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_project:cmnd_project |
| 63 |
:command: execute |
| 64 |
``` |
| 65 |
|
| 66 |
Successfully executed files will now be associated with a record in the cache: |
| 67 |
|
| 68 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 69 |
:command: list |
| 70 |
``` |
| 71 |
|
| 72 |
The cache record includes execution statistics: |
| 73 |
|
| 74 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_cache:cmnd_cache |
| 75 |
:command: info |
| 76 |
:args: 1 |
| 77 |
``` |
| 78 |
|
| 79 |
Next time we execute, jupyter-cache will check which files require re-execution: |
| 80 |
|
| 81 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_project:cmnd_project |
| 82 |
:command: execute |
| 83 |
``` |
| 84 |
|
| 85 |
The source files themselves will not be modified during/after execution. |
| 86 |
You can create a new "final" notebook, with the cached outputs merged into the source notebook with: |
| 87 |
|
| 88 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 89 |
:command: merge |
| 90 |
:args: 1 final_notebook.ipynb |
| 91 |
``` |
| 92 |
|
| 93 |
You can also add notebooks with custom formats, such as those read by [jupytext](https://jupytext.readthedocs.io): |
| 94 |
|
| 95 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 96 |
:command: add |
| 97 |
:args: --reader jupytext tests/notebooks/basic.md |
| 98 |
``` |
| 99 |
|
| 100 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 101 |
:command: list |
| 102 |
``` |
| 103 |
|
| 104 |
## Design considerations |
| 105 |
|
| 106 |
Although there are certainly other use cases, the principle use case this was written for is generating books / websites, created from multiple notebooks (and other text documents). |
| 107 |
It is desired that notebooks can be *auto-executed* **only** if the notebook had been modified in a way that may alter its code cell outputs. |
| 108 |
|
| 109 |
Some desired requirements (not yet all implemented): |
| 110 |
|
| 111 |
- A clear and robust API |
| 112 |
- The cache is persistent on disk |
| 113 |
- Notebook comparisons separate out "edits to content" from "edits to code cells". |
| 114 |
Cell rearranges and code cell changes should require a re-execution. |
| 115 |
Text content changes should not. |
| 116 |
- Allow parallel access to notebooks (for execution) |
| 117 |
- Store execution statistics/reports. |
| 118 |
- Store external assets: Notebooks being executed often require external assets: importing scripts/data/etc. These are prepared by the users. |
| 119 |
- Store execution artefacts: created during execution |
| 120 |
- A transparent and robust cache invalidation: imagine the user updating an external dependency or a Python module, or checking out a different git branch. |
| 121 |
|
| 122 |
## Contents |
| 123 |
|
| 124 |
```{toctree} |
| 125 |
:caption: Tutorials |
| 126 |
using/cli |
| 127 |
using/api |
| 128 |
``` |
| 129 |
|
| 130 |
```{toctree} |
| 131 |
:caption: Development |
| 132 |
develop/contributing |
| 133 |
``` |