Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/directives/try_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ as the first non-empty line under
the section header for an examples section will prevent a directive from being inserted,
allowing for specification of examples sections which should not be made interactive.

A code cell containing common setup code (similar to `pytest`'s `doctest_namespace`) can be added using `try_examples_preamble`.

The button text, theme, and warning text can be set globally with the config variables
`try_examples_global_button_text`, `try_examples_global_theme`, and `try_examples_global_warning_text` in `conf.py`;
Expand All @@ -213,6 +214,11 @@ global_enable_try_examples = True
try_examples_global_button_text = "Try it in your browser!"
try_examples_global_height = "200px"
try_examples_global_warning_text = "Interactive examples are experimental and may not always work as expected."
try_examples_preamble = """
import datetime as dt

now = dt.datetime.now()
"""
```

There is no option to set a global specific height because the proper height
Expand Down
13 changes: 12 additions & 1 deletion jupyterlite_sphinx/jupyterlite_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
from sphinx.util.fileutil import copy_asset
from sphinx.parsers import RSTParser

from ._try_examples import examples_to_notebook, insert_try_examples_directive
from ._try_examples import (
examples_to_notebook,
insert_try_examples_directive,
new_code_cell,
)

import jupytext
import nbformat
Expand Down Expand Up @@ -816,6 +820,12 @@ def run(self):

if notebook_unique_name is None:
nb = examples_to_notebook(self.content, warning_text=warning_text)

preamble = self.env.config.try_examples_preamble
if preamble:
# insert after the "experimental" warning
nb.cells.insert(1, new_code_cell(preamble))

self.content = None
notebooks_dir = Path(self.env.app.srcdir) / CONTENT_DIR
notebook_unique_name = f"{uuid4()}.ipynb".replace("-", "_")
Expand Down Expand Up @@ -1123,6 +1133,7 @@ def setup(app):
default=None,
rebuild="html",
)
app.add_config_value("try_examples_preamble", default=None, rebuild="html")

# Allow customising the button text for each directive (this is useful
# only when "new_tab" is set to True)
Expand Down
Loading