You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Standardise auto/cache execution
Both now call the same underlying function (from jupyter-cache) and act the same.
This improves auto, by making it output error reports and not raising an exception on an error.
Additional config has also been added: `execution_allow_errors` and `execution_in_temp`.
Like for timeout, `allow_errors` can also be set in the notebook `metadata.execution.allow_errors`
This presents one breaking change, in that `auto` will now by default execute in a temporary folder as the cwd. (we could set temp to False by default, but I think this is safer?)
2. For both methods, executions data is captured into:
```python
env.nb_execution_data[env.docname] = {
"mtime": datetime.datetime.utcnow().isoformat(),
"runtime": runtime,
"method": execution_method,
"succeeded": succeeded,
}
```
and a directive `nb-exec-table` has been added, to create a table of these results.
Any file that matches one of the items in `execution_excludepatterns` will not be
69
-
executed.
63
+
Any file that matches one of the items in `execution_excludepatterns` will not be executed.
70
64
71
65
(execute/cache)=
72
66
## Cacheing the notebook execution
73
67
74
-
As mentioned above, you can **cache the results of executing a notebook page** by setting
68
+
As mentioned above, you can **cache the results of executing a notebook page** by setting:
75
69
76
-
```
70
+
```python
77
71
jupyter_execute_notebooks ="cache"
78
72
```
79
73
80
-
in your conf.py file. In this case, when a page is executed, its outputs
81
-
will be stored in a local database. This allows you to be sure that the
82
-
outputs in your documentation are up-to-date, while saving time avoiding
83
-
unnecessary re-execution. It also allows you to store your `.ipynb` files in
84
-
your `git` repository *without their outputs*, but still leverage a cache to
85
-
save time when building your site.
74
+
in your conf.py file.
75
+
76
+
In this case, when a page is executed, its outputs will be stored in a local database.
77
+
This allows you to be sure that the outputs in your documentation are up-to-date, while saving time avoiding unnecessary re-execution.
78
+
It also allows you to store your `.ipynb` files (or their `.md` equivalent) in your `git` repository *without their outputs*, but still leverage a cache to save time when building your site.
86
79
87
80
When you re-build your site, the following will happen:
88
81
89
-
* Notebooks that have not seen changes to their **code cells** since the last build
90
-
will not be re-executed. Instead, their outputs will be pulled from the cache
91
-
and inserted into your site.
92
-
* Notebooks that **have any change to their code cells** will be re-executed, and the
93
-
cache will be updated with the new outputs.
82
+
* Notebooks that have not seen changes to their **code cells** or **metadata** since the last build will not be re-executed.
83
+
Instead, their outputs will be pulled from the cache and inserted into your site.
84
+
* Notebooks that **have any change to their code cells** will be re-executed, and the cache will be updated with the new outputs.
94
85
95
-
By default, the cache will be placed in the parent of your build folder. Generally,
96
-
this is in `_build/.jupyter_cache`.
86
+
By default, the cache will be placed in the parent of your build folder.
87
+
Generally, this is in `_build/.jupyter_cache`.
97
88
98
89
You may also specify a path to the location of a jupyter cache you'd like to use:
99
90
100
-
```
101
-
jupyter_cache = path/to/mycache
91
+
```python
92
+
jupyter_cache ="path/to/mycache"
102
93
```
103
94
104
-
The path should point to an **empty folder**, or a folder where a
105
-
**jupyter cache already exists**.
95
+
The path should point to an **empty folder**, or a folder where a **jupyter cache already exists**.
By default, the command working directory (cwd) that a notebook runs in will be its parent directory.
102
+
However, you can set `execution_in_temp=True` in your `conf.py`, to change this behaviour such that, for each execution, a temporary directory will be created and used as the cwd.
103
+
109
104
(execute/timeout)=
110
105
## Execution Timeout
111
106
112
107
The execution of notebooks is managed by {doc}`nbclient <nbclient:client>`.
113
108
114
-
The `execution_timeout` sphinx option defines the maximum time (in seconds) each notebook cell is allowed to run, if the execution takes longer an exception will be raised.
109
+
The `execution_timeout` sphinx option defines the maximum time (in seconds) each notebook cell is allowed to run.
110
+
if the execution takes longer an exception will be raised.
115
111
The default is 30 s, so in cases of long-running cells you may want to specify an higher value.
116
-
The timeout option can also be set to None or -1 to remove any restriction on execution time.
112
+
The timeout option can also be set to `None` or -1 to remove any restriction on execution time.
117
113
118
114
This global value can also be overridden per notebook by adding this to you notebooks metadata:
119
115
@@ -126,19 +122,32 @@ This global value can also be overridden per notebook by adding this to you note
126
122
}
127
123
```
128
124
129
-
## Execution FAQs
125
+
(execute/allow_errors)=
126
+
## Dealing with code that raises errors
130
127
131
-
### How can I include code that raises errors?
128
+
In some cases, you may want to intentionally show code that doesn't work (e.g., to show the error message).
129
+
You can achieve this at "three levels":
132
130
133
-
In some cases, you may want to intentionally show code that doesn't work (e.g., to show
134
-
the error message). To do this, add a `raises-exception` tag to your code cell. This
135
-
can be done via a Jupyter interface, or via the `{code-cell}` directive like so:
131
+
Globally, by setting `execution_allow_errors=True` in your `conf.py`.
136
132
137
-
````
133
+
Per notebook (overrides global), by adding this to you notebooks metadata:
134
+
135
+
```json
136
+
{
137
+
"metadata": {
138
+
"execution": {
139
+
"allow_errors": true
140
+
}
141
+
}
142
+
```
143
+
144
+
Per cell, by adding a `raises-exception` tag to your code cell.
145
+
This can be done via a Jupyter interface, or via the `{code-cell}` directive like so:
146
+
147
+
````md
138
148
```{code-cell}
139
-
---
140
-
tags: [raises-exception]
141
-
---
149
+
:tags: [raises-exception]
150
+
142
151
print(thisvariabledoesntexist)
143
152
```
144
153
````
@@ -151,3 +160,20 @@ tags: [raises-exception]
151
160
---
152
161
print(thisvariabledoesntexist)
153
162
```
163
+
164
+
(execute/statistics)=
165
+
## Execution Statistics
166
+
167
+
As notebooks are executed, certain statistics are stored in a dictionary (`{docname:data}`), and saved on the [sphinx environment object](https://www.sphinx-doc.org/en/master/extdev/envapi.html#sphinx.environment.BuildEnvironment) as `env.nb_execution_data`.
168
+
169
+
You can access this in a post-transform in your own sphinx extensions, or use the built-in `nb-exec-table` directive:
Copy file name to clipboardExpand all lines: docs/use/start.md
+8-1Lines changed: 8 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,10 +52,17 @@ MyST-NB then adds some additional configuration, specific to notebooks:
52
52
* - `jupyter_execute_notebooks`
53
53
- "auto"
54
54
- The logic for executing notebooks, [see here](execute/config) for details.
55
+
* - `execution_in_temp`
56
+
- `False`
57
+
- If `True`, then a temporary directory will be created and used as the command working directory (cwd), if `False` then the notebook's parent directory will be the cwd.
58
+
* - `execution_allow_errors`
59
+
- `False`
60
+
- If `False`, when a code cell raises an error the execution is stopped, if `True` then all cells are always run.
61
+
This can also be overridden by metadata in a notebook, [see here](execute/allow_errors) for details.
55
62
* - `execution_timeout`
56
63
- 30
57
64
- The maximum time (in seconds) each notebook cell is allowed to run.
58
-
This can be overridden by metadata in a notebook, [see here](execute/timeout) for detail.
65
+
This can also be overridden by metadata in a notebook, [see here](execute/timeout) for details.
59
66
* - `execution_show_tb`
60
67
- `False`
61
68
- Show failed notebook tracebacks in stdout (in addition to writing to file).
0 commit comments