Skip to content

Commit 0ef8be2

Browse files
authored
Merge pull request #143 from steppi/warning
Add option for initial warning cell in try examples directive
2 parents 518686f + 9907d4a commit 0ef8be2

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

docs/directives/try_examples.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ The `try_examples` directive has options
123123
* `:example_class:` An html class to attach to the outer container for the rendered
124124
examples content and embedded notebook. This can be used in a custom css file to allow
125125
for more precise customization, eg. different button styles across different examples.
126+
* `:warning_text:` Prepend a markdown cell to the notebook containing this text, styled to make it clear this is intended as a warning.
126127

127128
Here's an example with some options set
128129

@@ -131,6 +132,7 @@ Here's an example with some options set
131132
:button_text: Try it in your browser!
132133
:height: 400px
133134
:example_class: blue-bottom
135+
:warning_text: Interactive examples are experimental and may not always work as expected.
134136
135137
The button text has changed and the height now exceeds the size of the content.
136138
@@ -153,6 +155,7 @@ and here is the result
153155
:button_text: Try it in your browser!
154156
:height: 400px
155157
:example_class: blue-bottom
158+
:warning_text: Interactive examples are experimental and may not always work as expected.
156159
157160
The button text has changed and the height now exceeds the size of the content.
158161
@@ -200,13 +203,14 @@ the section header for an examples section will prevent a directive from being i
200203
allowing for specification of examples sections which should not be made interactive.
201204

202205

203-
The button text and theme can be set globally with the config variables
204-
`try_examples_global_button_text`, and `try_examples_global_theme`.
206+
The button text, theme, and warning text can be set globally with the config variables
207+
`try_examples_global_button_text`, `try_examples_global_theme`, and `try_examples_global_warning_text`.
205208

206209
```python
207210
global_enable_try_examples = True
208211
try_examples_global_button_text = "Try it in your browser!"
209212
try_examples_global_height = "200px"
213+
try_examples_global_warning_text = "Interactive examples are experimental and may not always work as expected."
210214
```
211215

212216
There is no option to set a global specific height because the proper height

jupyterlite_sphinx/_try_examples.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
import re
44

55

6-
def examples_to_notebook(input_lines):
6+
def examples_to_notebook(input_lines, *, warning_text=None):
77
"""Parse examples section of a docstring and convert to Jupyter notebook.
88
99
Parameters
1010
----------
1111
input_lines : iterable of str.
12-
Lines within
12+
13+
warning_text : str[Optional]
14+
If given, add a markdown cell at the top of the generated notebook
15+
containing the given text. The cell will be styled to indicate that
16+
this is a warning.
1317
1418
Returns
1519
-------
@@ -44,6 +48,12 @@ def examples_to_notebook(input_lines):
4448
"""
4549
nb = nbf.v4.new_notebook()
4650

51+
if warning_text is not None:
52+
# Two newlines \n\n signal that the inner content should be parsed as
53+
# markdown.
54+
warning = f"<div class='alert alert-warning'>\n\n{warning_text}\n\n</div>"
55+
nb.cells.append(new_markdown_cell(warning))
56+
4757
code_lines = []
4858
md_lines = []
4959
output_lines = []

jupyterlite_sphinx/jupyterlite_sphinx.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ class TryExamplesDirective(SphinxDirective):
368368
"theme": directives.unchanged,
369369
"button_text": directives.unchanged,
370370
"example_class": directives.unchanged,
371+
"warning_text": directives.unchanged,
371372
}
372373

373374
def run(self):
@@ -382,6 +383,7 @@ def run(self):
382383
button_text = self.options.pop("button_text", "Try it with Jupyterlite!")
383384
height = self.options.pop("height", None)
384385
example_class = self.options.pop("example_class", "")
386+
warning_text = self.options.pop("warning_text", None)
385387

386388
# We need to get the relative path back to the documentation root from
387389
# whichever file the docstring content is in.
@@ -404,7 +406,7 @@ def run(self):
404406
self.state.nested_parse(self.content, self.content_offset, content_node)
405407

406408
if notebook_unique_name is None:
407-
nb = examples_to_notebook(self.content)
409+
nb = examples_to_notebook(self.content, warning_text=warning_text)
408410
self.content = None
409411
notebooks_dir = Path(self.env.app.srcdir) / CONTENT_DIR
410412
notebook_unique_name = f"{uuid4()}.ipynb".replace("-", "_")
@@ -506,6 +508,7 @@ def _process_autodoc_docstrings(app, what, name, obj, options, lines):
506508
try_examples_options = {
507509
"theme": app.config.try_examples_global_theme,
508510
"button_text": app.config.try_examples_global_button_text,
511+
"warning_text": app.config.try_examples_global_warning_text,
509512
}
510513
try_examples_options = {
511514
key: value for key, value in try_examples_options.items() if value is not None
@@ -614,6 +617,7 @@ def setup(app):
614617

615618
app.add_config_value("global_enable_try_examples", default=False, rebuild=True)
616619
app.add_config_value("try_examples_global_theme", default=None, rebuild=True)
620+
app.add_config_value("try_examples_global_warning_text", default=None, rebuild=True)
617621
app.add_config_value(
618622
"try_examples_global_button_text",
619623
default=None,

0 commit comments

Comments
 (0)