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
12 changes: 12 additions & 0 deletions docs/directives/jupyterlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ You can also pass a Notebook file to open automatically:
:prompt_color: #00aa42
```

If you use the `:new_tab:` option in the directive, the Notebook will be opened in a new browser tab.

```rst
.. jupyterlite:: my_notebook.ipynb
:new_tab: True
```

```{eval-rst}
.. jupyterlite:: my_notebook.ipynb
:new_tab: True
```

The directive `search_params` allows to transfer some search parameters from the documentation URL to the Jupyterlite URL.\
Jupyterlite will then be able to fetch these parameters from its own URL.\
For example `:search_params: ["param1", "param2"]` will transfer the parameters *param1* and *param2*.
Expand Down
69 changes: 69 additions & 0 deletions jupyterlite_sphinx/jupyterlite_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,39 @@ def html(self):
)


class _InTab(Element):
def __init__(
self,
rawsource="",
*children,
prefix=JUPYTERLITE_DIR,
notebook=None,
lite_options={},
**attributes,
):
app_path = self.lite_app
if notebook is not None:
lite_options["path"] = notebook
app_path = f"{self.lite_app}{self.notebooks_path}"

options = "&".join(
[f"{key}={quote(value)}" for key, value in lite_options.items()]
)
self.lab_src = f'{prefix}/{app_path}{f"?{options}" if options else ""}'

super().__init__(
rawsource,
**attributes,
)

def html(self):
return (
'<button class="try_examples_button" '
f"onclick=\"window.open('{self.lab_src}')\">"
"Open as a notebook</button>"
)


class _LiteIframe(_PromptedIframe):
def __init__(
self,
Expand Down Expand Up @@ -165,6 +198,16 @@ class JupyterLiteIframe(_LiteIframe):
notebooks_path = ""


class JupyterLiteTab(_InTab):
"""Appended to the doctree by the JupyterliteDirective directive

Renders a button that opens a Notebook with JupyterLite in a new tab.
"""

lite_app = "lab/"
notebooks_path = ""


class NotebookLiteIframe(_LiteIframe):
"""Appended to the doctree by the NotebookliteDirective directive

Expand Down Expand Up @@ -262,6 +305,7 @@ class _LiteDirective(SphinxDirective):
"prompt": directives.unchanged,
"prompt_color": directives.unchanged,
"search_params": directives.unchanged,
"new_tab": directives.unchanged,
}

def run(self):
Expand All @@ -273,6 +317,8 @@ def run(self):

search_params = search_params_parser(self.options.pop("search_params", False))

new_tab = self.options.pop("new_tab", False)

source_location = os.path.dirname(self.get_source_info()[0])

prefix = os.path.relpath(
Expand All @@ -299,6 +345,20 @@ def run(self):
else:
notebook_name = None

if new_tab:
return [
self.newtab_cls(
prefix=prefix,
notebook=notebook_name,
width=width,
height=height,
prompt=prompt,
prompt_color=prompt_color,
search_params=search_params,
lite_options=self.options,
)
]

return [
self.iframe_cls(
prefix=prefix,
Expand All @@ -320,6 +380,7 @@ class JupyterLiteDirective(_LiteDirective):
"""

iframe_cls = JupyterLiteIframe
newtab_cls = JupyterLiteTab


class NotebookLiteDirective(_LiteDirective):
Expand Down Expand Up @@ -694,6 +755,14 @@ def setup(app):
text=(skip, None),
man=(skip, None),
)
app.add_node(
JupyterLiteTab,
html=(visit_element_html, None),
latex=(skip, None),
textinfo=(skip, None),
text=(skip, None),
man=(skip, None),
)
app.add_directive("jupyterlite", JupyterLiteDirective)

# Initialize Replite directive
Expand Down