| 1 |
(use/cli)= |
| 2 |
|
| 3 |
# Command-Line |
| 4 |
|
| 5 |
Note, you can follow this tutorial by cloning <https://github.com/executablebooks/jupyter-cache>, and running these commands inside it.: |
| 6 |
tox |
| 7 |
```{jcache-clear} |
| 8 |
``` |
| 9 |
|
| 10 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_main:jcache |
| 11 |
:args: --help |
| 12 |
``` |
| 13 |
|
| 14 |
The first time the cache is required, it will be lazily created: |
| 15 |
|
| 16 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 17 |
:command: list |
| 18 |
:input: y |
| 19 |
``` |
| 20 |
|
| 21 |
You can specify the path to the cache, with the `--cache-path` option, |
| 22 |
or set the `JUPYTERCACHE` environment variable. |
| 23 |
|
| 24 |
You can also clear it at any time: |
| 25 |
|
| 26 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_project:cmnd_project |
| 27 |
:command: clear |
| 28 |
:input: y |
| 29 |
``` |
| 30 |
|
| 31 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 32 |
:command: list |
| 33 |
:input: y |
| 34 |
``` |
| 35 |
|
| 36 |
````{tip} |
| 37 |
Execute this in the terminal for auto-completion: |
| 38 |
|
| 39 |
```console |
| 40 |
eval "$(_JCACHE_COMPLETE=source jcache)" |
| 41 |
``` |
| 42 |
```` |
| 43 |
|
| 44 |
## Adding notebooks to the project |
| 45 |
|
| 46 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 47 |
:args: --help |
| 48 |
``` |
| 49 |
|
| 50 |
A project consist of a set of notebooks to be executed. |
| 51 |
|
| 52 |
When adding notebooks to the project, they are recorded by their URI (e.g. file path), |
| 53 |
i.e. no physical copying takes place until execution time. |
| 54 |
|
| 55 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 56 |
:command: add |
| 57 |
:args: tests/notebooks/basic.ipynb tests/notebooks/basic_failing.ipynb tests/notebooks/basic_unrun.ipynb tests/notebooks/complex_outputs.ipynb tests/notebooks/external_output.ipynb |
| 58 |
``` |
| 59 |
|
| 60 |
You can list the notebooks in the project, at present none have an existing execution record in the cache: |
| 61 |
|
| 62 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 63 |
:command: list |
| 64 |
``` |
| 65 |
|
| 66 |
You can remove a notebook from the project by its URI or ID: |
| 67 |
|
| 68 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 69 |
:command: remove |
| 70 |
:args: 4 |
| 71 |
``` |
| 72 |
|
| 73 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 74 |
:command: list |
| 75 |
``` |
| 76 |
|
| 77 |
or clear all notebooks from the project: |
| 78 |
|
| 79 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 80 |
:command: clear |
| 81 |
:input: y |
| 82 |
``` |
| 83 |
|
| 84 |
## Add a custom reader to read notebook files |
| 85 |
|
| 86 |
By default, notebook files are read using the [nbformat reader](https://nbformat.readthedocs.io/en/latest/api.html#nbformat.read). |
| 87 |
However, you can also specify a custom reader, defined by an entry point in the `jcache.readers` group. |
| 88 |
Included with jupyter_cache is the [jupytext](https://jupytext.readthedocs.io) reader, for formats like MyST Markdown: |
| 89 |
|
| 90 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 91 |
:command: add |
| 92 |
:args: --reader nbformat tests/notebooks/basic.ipynb tests/notebooks/basic_failing.ipynb |
| 93 |
``` |
| 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 |
:::{important} |
| 105 |
To use the `jupytext` reader, you must have the `jupytext` package installed. |
| 106 |
::: |
| 107 |
|
| 108 |
## Executing the notebooks |
| 109 |
|
| 110 |
Simply call the `execute` command, to execute all notebooks in the project that do not have an existing record in the cache. |
| 111 |
|
| 112 |
Executors are defined by entry points in the `jcache.executors` group. |
| 113 |
jupyter-cache includes these executors: |
| 114 |
|
| 115 |
- `local-serial`: execute notebooks with the working directory set to their path, in serial mode (using a single process). |
| 116 |
- `local-parallel`: execute notebooks with the working directory set to their path, in parallel mode (using multiple processes). |
| 117 |
- `temp-serial`: execute notebooks with a temporary working directory, in serial mode (using a single process). |
| 118 |
- `temp-parallel`: execute notebooks with a temporary working directory, in parallel mode (using multiple processes). |
| 119 |
|
| 120 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_project:cmnd_project |
| 121 |
:command: execute |
| 122 |
:args: --executor local-serial |
| 123 |
``` |
| 124 |
|
| 125 |
Successfully executed notebooks will now have a record in the cache, uniquely identified by the a hash of their code and metadata content: |
| 126 |
|
| 127 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_cache:cmnd_cache |
| 128 |
:command: list |
| 129 |
:args: --hashkeys |
| 130 |
``` |
| 131 |
|
| 132 |
These records are then compared to the hashes of notebooks in the project, to find which have up-to-date executions. |
| 133 |
Note here both notebooks share the same cached notebook (denoted by `[1]` in the status): |
| 134 |
|
| 135 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 136 |
:command: list |
| 137 |
``` |
| 138 |
|
| 139 |
Next time you execute the project, only notebooks which don't match a cached record will be executed: |
| 140 |
|
| 141 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_project:cmnd_project |
| 142 |
:command: execute |
| 143 |
:args: --executor local-serial -v CRITICAL |
| 144 |
``` |
| 145 |
|
| 146 |
You can also `force` all notebooks to be re-executed: |
| 147 |
|
| 148 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_project:cmnd_project |
| 149 |
:command: execute |
| 150 |
:args: --force |
| 151 |
``` |
| 152 |
|
| 153 |
If you modify a code cell, the notebook will no longer match a cached notebook or, if you wish to re-execute unchanged notebook(s) (for example if the runtime environment has changed), you can remove their records from the cache (keeping the project record): |
| 154 |
|
| 155 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_cache:cmnd_cache |
| 156 |
:command: clear |
| 157 |
:input: n |
| 158 |
:allow-exception: |
| 159 |
``` |
| 160 |
|
| 161 |
:::{note} |
| 162 |
The number of notebooks in the cache is limited |
| 163 |
(current default 1000). |
| 164 |
Once this limit is reached, the oldest (last accessed) notebooks begin to be deleted. |
| 165 |
change this default with `jcache config cache-limit` |
| 166 |
::: |
| 167 |
|
| 168 |
## Analysing executed/excepted notebooks |
| 169 |
|
| 170 |
You can see the elapsed execution time of a notebook via its ID in the cache: |
| 171 |
|
| 172 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_cache:cmnd_cache |
| 173 |
:command: info |
| 174 |
:args: 1 |
| 175 |
``` |
| 176 |
|
| 177 |
Failed execution tracebacks are also available on the project record: |
| 178 |
|
| 179 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 180 |
:command: info |
| 181 |
:args: --tb tests/notebooks/basic_failing.ipynb |
| 182 |
``` |
| 183 |
|
| 184 |
```{tip} |
| 185 |
Code cells can be tagged with `raises-exception` to let the executor known that a cell *may* raise an exception |
| 186 |
(see [this issue on its behaviour](https://github.com/jupyter/nbconvert/issues/730)). |
| 187 |
``` |
| 188 |
|
| 189 |
## Retrieving executed notebooks |
| 190 |
|
| 191 |
Notebooks added to the project are not modified in any way during or after execution: |
| 192 |
|
| 193 |
You can create a new "final" notebook, with the cached outputs merged into the source notebook with: |
| 194 |
|
| 195 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 196 |
:command: merge |
| 197 |
:args: tests/notebooks/basic.md final_notebook.ipynb |
| 198 |
``` |
| 199 |
|
| 200 |
## Invalidating cached notebooks |
| 201 |
|
| 202 |
If you want to invalidate a notebook's cached execution, |
| 203 |
for example if you have changed the notebook's execution environment, |
| 204 |
you can do so by calling the `invalidate` command: |
| 205 |
|
| 206 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 207 |
:command: invalidate |
| 208 |
:args: tests/notebooks/basic.ipynb |
| 209 |
``` |
| 210 |
|
| 211 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 212 |
:command: list |
| 213 |
``` |
| 214 |
|
| 215 |
## Specifying notebooks with assets |
| 216 |
|
| 217 |
When executing in a temporary directory, you may want to specify additional "asset" files that also need to be be copied to this directory for the notebook to run. |
| 218 |
|
| 219 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 220 |
:command: remove |
| 221 |
:args: tests/notebooks/basic.ipynb |
| 222 |
``` |
| 223 |
|
| 224 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 225 |
:command: add-with-assets |
| 226 |
:args: -nb tests/notebooks/basic.ipynb tests/notebooks/artifact_folder/artifact.txt |
| 227 |
``` |
| 228 |
|
| 229 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_notebook:cmnd_notebook |
| 230 |
:command: info |
| 231 |
:args: tests/notebooks/basic.ipynb |
| 232 |
``` |
| 233 |
|
| 234 |
## Adding notebooks directly to the cache |
| 235 |
|
| 236 |
Pre-executed notebooks can be added to the cache directly, without executing them. |
| 237 |
|
| 238 |
A check will be made that the notebooks look to have been executed correctly, |
| 239 |
i.e. the cell execution counts go sequentially up from 1. |
| 240 |
|
| 241 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_cache:cmnd_cache |
| 242 |
:command: add |
| 243 |
:args: tests/notebooks/complex_outputs.ipynb |
| 244 |
:input: y |
| 245 |
``` |
| 246 |
|
| 247 |
Or to skip the validation: |
| 248 |
|
| 249 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_cache:cmnd_cache |
| 250 |
:command: add |
| 251 |
:args: --no-validate tests/notebooks/external_output.ipynb |
| 252 |
``` |
| 253 |
|
| 254 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_cache:cmnd_cache |
| 255 |
:command: list |
| 256 |
``` |
| 257 |
|
| 258 |
:::{tip} |
| 259 |
To only show the latest versions of cached notebooks. |
| 260 |
|
| 261 |
```console |
| 262 |
$ jcache cache list --latest-only |
| 263 |
``` |
| 264 |
|
| 265 |
::: |
| 266 |
|
| 267 |
## Diffing notebooks |
| 268 |
|
| 269 |
You can diff any of the cached notebooks with any (external) notebook: |
| 270 |
|
| 271 |
```{warning} |
| 272 |
This requires `pip install nbdime` |
| 273 |
``` |
| 274 |
|
| 275 |
```{jcache-cli} jupyter_cache.cli.commands.cmd_cache:cmnd_cache |
| 276 |
:command: diff |
| 277 |
:args: 1 tests/notebooks/basic_unrun.ipynb |
| 278 |
``` |