Skip to content

Commit 5390900

Browse files
committed
Add support for an initial cell with warning text
1 parent a9f5ba2 commit 5390900

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

jupyterlite_sphinx/_try_examples.py

Lines changed: 14 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,14 @@ def examples_to_notebook(input_lines):
4448
"""
4549
nb = nbf.v4.new_notebook()
4650

51+
52+
if warning_text is not None:
53+
# Two newlines \n\n signal that the inner content should be parsed as
54+
# markdown.
55+
warning = f"<div class='alert alert-warning'>\n\n{warning_text}\n\n</div>"
56+
nb.cells.append(new_markdown_cell(warning))
57+
58+
4759
code_lines = []
4860
md_lines = []
4961
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)