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
`jupyterlite-sphinx` provides the experimental `try_examples` directive which allows
4
-
docstring examples sections written in [doctestformat](https://docs.python.org/3/library/doctest.html) to be swapped with an embedded classic Notebook at the push of a button.
4
+
docstring examples sections written in [doctest format](https://docs.python.org/3/library/doctest.html) to be swapped with an embedded classic Notebook at the push of a button.
5
+
6
+
Below is an example of the directive in use. The button has been styled with custom
7
+
css as explained in the configuration section below. Without custom css, the button will
8
+
be plain and unadorned.
5
9
6
10
7
11
```rst
8
12
Examples
9
13
--------
10
14
.. try_examples::
11
-
:button_css:
12
-
background-color: #f7dc1e;
13
-
border: none;
14
-
padding: 5px 10px;
15
-
border-radius: 15px;
16
-
font-family: vibur;
17
-
font-size: x-large;
18
-
box-shadow: 0 2px 5px rgba(108,108,108,0.2);
19
-
:button_hover_css:
20
-
background-color: #fff221;
21
-
transform: scale(1.02);
22
-
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
23
-
cursor: pointer;
24
-
:button_horizontal_position: right
25
-
:button_vertical_position: top
26
-
:button_text: Try it in a classic notebook!
27
-
:min_height: 200px
28
-
29
15
30
16
Doctest examples sections are parsed and converted to notebooks. Blocks of text
31
-
like this become markdown cells. Codeblocks begin with `>>>`. Contiguous blocks
17
+
like this become markdown cells. Codeblocks begin with ``>>>``. Contiguous blocks
32
18
of code are combined into a single code cell.
33
19
34
20
>>> x = 2
35
21
>>> y = 2
36
22
>>> x + y
37
23
4
38
24
39
-
`...` is used to continue multiline statements.
25
+
``...`` is used to continue multiline statements.
40
26
41
27
>>> def f(x, y):
42
28
... return x + y
@@ -54,28 +40,13 @@ Examples
54
40
markdown format.
55
41
```
56
42
43
+
and here is how this looks and works when rendered.
44
+
57
45
58
46
```{eval-rst}
59
47
Examples
60
48
--------
61
49
.. try_examples::
62
-
:button_css:
63
-
background-color: #f7dc1e;
64
-
border: none;
65
-
padding: 5px 10px;
66
-
border-radius: 15px;
67
-
font-family: vibur;
68
-
font-size: x-large;
69
-
box-shadow: 0 2px 5px rgba(108,108,108,0.2);
70
-
:button_hover_css:
71
-
background-color: #fff221;
72
-
transform: scale(1.02);
73
-
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
74
-
cursor: pointer;
75
-
:button_horizontal_position: right
76
-
:button_vertical_position: top
77
-
:button_text: Try it in a classic notebook!
78
-
:min_height: 200px
79
50
80
51
Doctest examples sections are parsed and converted to notebooks. Blocks of text
81
52
like this become markdown cells. Codeblocks begin with `>>>`. Contiguous blocks
@@ -104,75 +75,144 @@ Examples
104
75
markdown format.
105
76
```
106
77
78
+
By default, the height of the embedded notebook's iframe container is calculated to match the height of the rendered doctest examples so that it takes up the same amount of space on the
79
+
page.
80
+
107
81
## Configuration
108
82
109
-
The button's text, position, and style can be configured to match your page design. The
110
-
text can be configured with the option `:button_text:`. The options `:button_css:` and
111
-
`:button_hover_css:` take lists of css properties as in the example above, and
112
-
apply them to the button. `:button_horizontal_position:` can be one of `left`, `right`, or
113
-
`center` and `:button_vertical_position:` can be one of `top` or `bottom`. The position
114
-
is with respect to the rendered examples block / embedded notebook
115
-
(depending on which is active).
83
+
The position and style of the button can be customized to match your documentation's
84
+
design by adding custom css as explained in Sphinx's documentation [here](https://docs.readthedocs.io/en/stable/guides/adding-custom-css.html#how-to-add-custom-css-or-javascript-to-sphinx-documentation). The buttons have class `try_examples_button`. The buttons are placed within
85
+
containers with class `try_examples_button_container`, which can be selected to adjust the
86
+
positioning of the button. The css for the example above is
87
+
88
+
```css
89
+
90
+
.try_examples_button {
91
+
color: white;
92
+
background-color: #0054a6;
93
+
border: none;
94
+
padding: 5px10px;
95
+
border-radius: 10px;
96
+
margin-bottom: 5px;
97
+
box-shadow: 02px5pxrgba(108,108,108,0.2);
98
+
}
99
+
100
+
.try_examples_button:hover {
101
+
background-color: #0066cc;
102
+
transform: scale(1.02);
103
+
box-shadow: 02px5pxrgba(0, 0, 0, 0.2);
104
+
cursor: pointer;
105
+
}
106
+
107
+
.try_examples_button_container {
108
+
display: flex;
109
+
justify-content: flex-end;
110
+
}
111
+
```
116
112
117
-
The height of the embedded notebook's iframe container is calculated to match the height
118
-
of the rendered doctest examples so that it takes up the same amount of space on the
119
-
page. The `:min_height:` option can be used to ensure that the embedded notebook will not
120
-
be unuseably small for very short examples blocks, though its use can cause the contents
121
-
of the page to shift when the button is pressed.
122
113
123
-
the `:theme:` option available for other `jupyterlite-sphinx` directives is also
124
-
available.
114
+
The `try_examples` directive has options
115
+
*`:height:` To set a specific value for the height of the [iframe](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe) containing the embeddednotebook.
116
+
*`:button_text` To customize the text of the button that replaces the rendered examples with an embedded notebook.
117
+
*`:theme:` This works the same as for the other JupyterLite-Sphinx directives.
118
+
*`:example_class:` An html class to attach to the outer container for the rendered
119
+
examples content and embedded notebook. This can be used in a custom css file to allow
120
+
for more precise customization, eg. different button styles across different examples.
125
121
126
-
If you are using [sphinx.ext.autodoc](https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html) with [numpydoc](https://numpydoc.readthedocs.io/en/latest/format.html) or [sphinx.ext.napoleon](https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html), you
127
-
can set the option
122
+
Here's an example with some options set
123
+
124
+
```rst
125
+
.. try_examples::
126
+
:button_text: Try it in your browser!
127
+
:height: 400px
128
+
129
+
The button text has changed and the height now exceeds the size of the content.
130
+
131
+
>>> x = 2
132
+
>>> y = 2
133
+
>>> x + y
134
+
4
135
+
136
+
```
137
+
138
+
and here is the result
139
+
140
+
```{eval-rst}
141
+
.. try_examples::
142
+
:button_text: Try it in your browser!
143
+
:height: 400px
144
+
145
+
The button text has changed and the height now exceeds the size of the content.
146
+
147
+
>>> x = 2
148
+
>>> y = 2
149
+
>>> x + y
150
+
4
128
151
129
-
```python
130
-
global_enable_try_examples =True
131
152
```
132
153
133
-
in your sphinx `conf.py` in order to automatically insert the `try_examples` directive
134
-
in examples sections during the `"autodoc-process-docstring"` event. Configuration values
135
-
can be set globally for the inserted `try_examples` directives by setting the config values
136
-
`try_examples_global_button_css`, etc. as below. All valid config values are supported
137
-
by prepending `try_examples_global_`.
154
+
155
+
### Global Configuration
156
+
157
+
For projects with a large number of existing doctest examples, it would be tedious to add
158
+
the `try_examples` directive manually to each docstring example. If you are using [sphinx.ext.autodoc](https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html) with either [numpydoc](https://numpydoc.readthedocs.io/en/latest/format.html) or [sphinx.ext.napoleon](https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html), you
159
+
can set the option.
138
160
139
161
```python
140
162
global_enable_try_examples =True
141
-
try_examples_global_button_css ="""
142
-
color: white;
143
-
background-color: #0054a6;
144
-
border: none;
145
-
padding: 5px 10px;
146
-
border-radius: 5px;
147
-
cursor: pointer;
148
-
"""
149
-
try_examples_global_button_hover_css ="""
150
-
background-color: #0066cc;
151
-
transform: scale(1.02);
152
-
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
153
-
"""
154
-
155
-
try_examples_global_button_text ="Try it in your browser!"
156
-
try_examples_global_min_height ="200px"
157
163
```
158
164
165
+
in your sphinx `conf.py` in order to automatically insert the `try_examples` directive
166
+
in examples sections during the `"autodoc-process-docstring"` event. This works by
167
+
identifying section headings. An examples section includes all of the content beneath
168
+
an examples heading, and up to either the next heading or the end of the docstring if
169
+
there are no further headings. One of `numpydoc` or `sphinx.ext.napoleon` is required
170
+
because these map the section headers to a standardized format.
171
+
159
172
If an examples section already contains a `try_examples` directive, no additional
160
173
directives will be inserted, allowing for specific cases to be separately configured
161
-
if needed. Adding the comment `..! disable_try_examples` as the first non-empty line under
174
+
if needed. Adding the comment
175
+
176
+
177
+
```rst
178
+
..! disable_try_examples`
179
+
```
180
+
181
+
as the first non-empty line under
162
182
the section header for an examples section will prevent a directive from being inserted,
163
183
allowing for specification of examples sections which should not be made interactive.
164
184
165
-
## Other considerations
166
-
If you are using the `TryExamples` directive in your documentation, you'll need to ensure
167
-
that the version of the package installed in the Jupyterlite kernel you are using
168
-
matches that of the version you are documenting.
169
185
170
-
## Configuration without rebuilding
186
+
The button text and theme can be set globally with the config variables
187
+
`try_examples_global_button_text`, and `try_examples_global_theme`.
188
+
189
+
```python
190
+
global_enable_try_examples =True
191
+
try_examples_global_button_text ="Try it in your browser!"
192
+
try_examples_global_height ="200px"
193
+
```
194
+
195
+
There is no option to set a global specific height because the proper height
196
+
should depend on the size of the examples content. Again, the default height of
197
+
the embedded notebook's iframe container matches the height of the associated
198
+
rendered doctest example so that it takes up the same amount of space on the
199
+
page. For very small examples this may lead to an unusably small notebook. It's possible
200
+
to set a global minimum height in the `try_examples.json` configuration file described
201
+
below.
202
+
203
+
### try_examples.json configuration file.
204
+
205
+
Users may place a configuration file `try_examples.json` in the source root of
206
+
their documentation. This configuration file will be copied to the build root of
207
+
the deployed documentation. Changes to the configuration file in the build root
208
+
will be respected without rebuilding the documentation, allowing for runtime
209
+
configuration.
171
210
172
-
The `TryExamples` directive supports disabling interactive examples without rebuilding
173
-
the documentation. This can be helpful for projects requiring substantial documentation
174
-
build time. Users may add a json config file entitled `.try_examples.json` to the root
175
-
directory of the build directory for the deployed documentation. The format is a list of
211
+
The current options are
212
+
213
+
#### "ignore_patterns"
214
+
215
+
The format is a list of
176
216
[JavaScript Regex patterns](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions) attached to the key `"ignore_patterns"` like below.
177
217
178
218
```json
@@ -184,23 +224,38 @@ directory of the build directory for the deployed documentation. The format is a
184
224
`TryExamples` buttons will be hidden in url pathnames matching at least one of these
185
225
patterns, effectively disabling the interactive documentation. In the provided example:
186
226
187
-
* The pattern `".*latest/.*" disables interactive examples for urls for the documentation
227
+
* The pattern `"^/latest/.*"` disables interactive examples for urls for the documentation
188
228
for the latest version of the package, which may be useful if this documentation is
189
229
for a development version for which a corresponding package build is not available
190
230
in a Jupyterlite kernel.
191
231
192
-
* The pattern `".*stable/reference/generated/example.html"` targets a particular url
232
+
* The pattern `"^/stable/reference/generated/example.html"` targets a particular url
193
233
in the documentation for the latest stable release.
194
234
195
235
Note that these patterns should match the [pathname](https://developer.mozilla.org/en-US/docs/Web/API/Location/pathname) of the url, not the full url. This is the path portion of
196
236
the url. For instance, the pathname of https://jupyterlite-sphinx.readthedocs.io/en/latest/directives/try_examples.html is `/en/latest/directives/try_examples.html`.
197
237
238
+
Again, the configuration file can be added or edited within the deployed documentation,
239
+
allowing for disabling or enabling examples without rebuilding the documentation.
198
240
199
-
A default configuration file can be specified in `conf.py` with the option
200
-
`try_examples_default_runtime_config`.
241
+
#### "global_min_height"
242
+
To avoid having unusably small notebooks for very small examples due to the default of
243
+
having the embedded notebooks' iframe containers take the same amount of space as the
244
+
rendered content they replace, users can set a global minimum height in
0 commit comments