Skip to content

Commit f14181a

Browse files
authored
Merge pull request #19 from martinRenou/add_jupyterlite_directive
Add jupyterlite directive and rework docs
2 parents 55d1211 + 9cc9d79 commit f14181a

File tree

7 files changed

+197
-123
lines changed

7 files changed

+197
-123
lines changed

docs/configuration.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Configuration
2+
=============
3+
4+
You can provide custom configuration to your JupyterLite deployment.
5+
6+
For example, if you want to have bqplot working in this deployment, you need to install the bqplot federated extension
7+
and you can serve the bqplot wheel to ``piplite``, this is done by telling your ``conf.py`` where to look for the jupyterlite config:
8+
9+
.. code-block:: python
10+
11+
jupyterlite_config = "jupyterlite_config.json"
12+
13+
The ``jupyterlite_config.json`` containing the following:
14+
15+
.. code-block:: json
16+
17+
{
18+
"LiteBuildConfig": {
19+
"federated_extensions": [
20+
"https://github.com/conda-forge/releases/releases/download/noarch/bqplot-0.12.33-pyhd8ed1ab_0.tar.bz2/bqplot-0.12.33-pyhd8ed1ab_0.tar.bz2",
21+
],
22+
"ignore_sys_prefix": true,
23+
"piplite_urls": [
24+
"https://files.pythonhosted.org/packages/py2.py3/b/bqplot/bqplot-0.12.33-py2.py3-none-any.whl",
25+
]
26+
}
27+
}
28+
29+
Then you should be able to show Notebooks working with bqplot!
30+
31+
.. code-block:: rst
32+
33+
.. retrolite:: bqplot.ipynb
34+
35+
.. retrolite:: bqplot.ipynb

docs/index.rst

Lines changed: 13 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,27 @@
11
jupyterlite-sphinx
22
==================
33

4-
Installation
5-
------------
6-
7-
You can install ``jupyterlite-sphinx`` with ``pip``:
8-
9-
.. code-block::
10-
11-
pip install jupyterlite-sphinx
12-
13-
then you need to add the ``jupyterlite-sphinx`` extension to your ``conf.py`` file of your sphinx docs:
14-
15-
.. code-block:: python
16-
17-
extensions = [
18-
'jupyterlite_sphinx',
19-
# And other sphinx extensions
20-
# ...
21-
]
22-
23-
Replite directive
24-
-----------------
25-
26-
``jupyterlite-sphinx`` provides a ``replite`` directive that allows you to embed a replite console in your docs.
27-
This directive takes the same options as the ``replite`` package, see https://github.com/jtpio/replite for reference.
28-
29-
.. code-block:: rst
30-
31-
.. replite::
32-
:kernel: python
33-
:toolbar: 1
34-
:theme: JupyterLab Light
35-
:width: 100%
36-
:height: 600px
37-
38-
print('Hello, World!')
39-
40-
.. replite::
41-
:kernel: python
42-
:toolbar: 1
43-
:theme: JupyterLab Light
44-
:width: 100%
45-
:height: 600px
46-
47-
print('Hello, World!')
48-
49-
Retrolite directive
50-
-------------------
51-
52-
``jupyterlite-sphinx`` provides a ``retrolite`` directive that allows you to embed an entire executable Notebook in your docs.
53-
It takes the name of the Notebook as argument:
54-
55-
.. code-block:: rst
56-
57-
.. retrolite:: my_notebook.ipynb
58-
:width: 100%
59-
:height: 600px
60-
61-
.. retrolite:: my_notebook.ipynb
62-
:width: 100%
63-
:height: 600px
64-
65-
Or you can simply show the filetree:
66-
67-
.. code-block:: rst
68-
69-
.. retrolite::
70-
:width: 100%
71-
:height: 600px
72-
73-
.. retrolite::
74-
:width: 100%
75-
:height: 600px
76-
77-
78-
JupyterLab and RetroLab deployed for you
79-
----------------------------------------
4+
A Sphinx extension that provides utilities for embedding JupyterLite in your docs.
805

816
``jupyterlite-sphinx`` makes a full deployment of JupyterLite for you, you can access the JupyterLab UI and RetroLab UI following the
827
``./lite/lab/index.html`` and ``./lite/retro/index.html`` relative URLs:
838

849
`JupyterLab <./lite/lab/index.html>`_
85-
`Retrolab <./lite/retro/index.html>`_
86-
87-
Configuration
88-
-------------
89-
90-
You can provide custom configuration to your JupyterLite deployment.
91-
92-
For example, if you want to have bqplot working in this deployment, you need to install the bqplot federated extension
93-
and you can serve the bqplot wheel to ``piplite``, this is done by telling your ``conf.py`` where to look for the jupyterlite config:
9410

95-
.. code-block:: python
96-
97-
jupyterlite_config = "jupyterlite_config.json"
98-
99-
The ``jupyterlite_config.json`` containing the following:
100-
101-
.. code-block:: json
11+
`Retrolab <./lite/retro/index.html>`_
10212

103-
{
104-
"LiteBuildConfig": {
105-
"federated_extensions": [
106-
"https://github.com/conda-forge/releases/releases/download/noarch/bqplot-0.12.33-pyhd8ed1ab_0.tar.bz2/bqplot-0.12.33-pyhd8ed1ab_0.tar.bz2",
107-
],
108-
"ignore_sys_prefix": true,
109-
"piplite_urls": [
110-
"https://files.pythonhosted.org/packages/py2.py3/b/bqplot/bqplot-0.12.33-py2.py3-none-any.whl",
111-
]
112-
}
113-
}
11413

115-
Then you should be able to show Notebooks working with bqplot!
14+
.. toctree::
15+
:caption: Installation
16+
:maxdepth: 2
11617

117-
.. code-block:: rst
18+
installation
19+
configuration
11820

119-
.. retrolite:: bqplot.ipynb
21+
.. toctree::
22+
:caption: Usage
23+
:maxdepth: 2
12024

121-
.. retrolite:: bqplot.ipynb
25+
jupyterlite
26+
retrolite
27+
replite

docs/installation.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Installation
2+
============
3+
4+
You can install ``jupyterlite-sphinx`` with ``pip``:
5+
6+
.. code-block::
7+
8+
pip install jupyterlite-sphinx
9+
10+
then you need to add the ``jupyterlite-sphinx`` extension to your ``conf.py`` file of your sphinx docs:
11+
12+
.. code-block:: python
13+
14+
extensions = [
15+
'jupyterlite_sphinx',
16+
# And other sphinx extensions
17+
# ...
18+
]

docs/jupyterlite.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
JupyterLite directive
2+
=====================
3+
4+
``jupyterlite-sphinx`` provides a ``jupyterlite`` directive that allows you to embed JupyterLab in your docs.
5+
6+
.. code-block:: rst
7+
8+
.. jupyterlite::
9+
:width: 100%
10+
:height: 600px
11+
12+
.. jupyterlite::
13+
:width: 100%
14+
:height: 600px
15+
16+
You can also pass a Notebook file to open automatically:
17+
18+
.. code-block:: rst
19+
20+
.. jupyterlite:: my_notebook.ipynb
21+
:width: 100%
22+
:height: 600px
23+
24+
.. jupyterlite:: my_notebook.ipynb
25+
:width: 100%
26+
:height: 600px

docs/replite.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Replite directive
2+
=================
3+
4+
``jupyterlite-sphinx`` provides a ``replite`` directive that allows you to embed a replite console in your docs.
5+
This directive takes the same options as the ``replite`` package, see https://github.com/jtpio/replite for reference.
6+
7+
.. code-block:: rst
8+
9+
.. replite::
10+
:kernel: python
11+
:toolbar: 1
12+
:theme: JupyterLab Light
13+
:width: 100%
14+
:height: 600px
15+
16+
print('Hello, World!')
17+
18+
.. replite::
19+
:kernel: python
20+
:toolbar: 1
21+
:theme: JupyterLab Light
22+
:width: 100%
23+
:height: 600px
24+
25+
print('Hello, World!')

docs/retrolite.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
RetroLite directive
2+
===================
3+
4+
``jupyterlite-sphinx`` provides a ``retrolite`` directive that allows you to embed Retrolab in your docs.
5+
6+
.. code-block:: rst
7+
8+
.. retrolite::
9+
:width: 100%
10+
:height: 600px
11+
12+
.. retrolite::
13+
:width: 100%
14+
:height: 600px
15+
16+
You can also pass a Notebook file to open:
17+
18+
.. code-block:: rst
19+
20+
.. retrolite:: my_notebook.ipynb
21+
:width: 100%
22+
:height: 600px
23+
24+
.. retrolite:: my_notebook.ipynb
25+
:width: 100%
26+
:height: 600px

src/jupyterlite_sphinx.py

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ def run(self):
8686
return [RepliteIframe(width=width, height=height, replite_options=self.options)]
8787

8888

89-
class RetroliteIframe(Element):
90-
"""Appended to the doctree by the RetroliteDirective directive
91-
92-
Renders an iframe that shows a Notebook with RetroLite.
93-
"""
94-
89+
class _LiteIframe(Element):
9590
def __init__(
9691
self,
9792
rawsource="",
@@ -107,9 +102,9 @@ def html(self):
107102
notebook = self["notebook"]
108103

109104
src = (
110-
f"{JUPYTERLITE_DIR}/retro/notebooks/?path={notebook}"
105+
f"{JUPYTERLITE_DIR}/{self.lite_app}/{self.notebooks_path}?path={notebook}"
111106
if notebook is not None
112-
else f"{JUPYTERLITE_DIR}/retro"
107+
else f"{JUPYTERLITE_DIR}/{self.lite_app}"
113108
)
114109

115110
return (
@@ -118,12 +113,28 @@ def html(self):
118113
)
119114

120115

121-
class RetroliteDirective(SphinxDirective):
122-
"""The ``.. retrolite::`` directive.
116+
class JupyterLiteIframe(_LiteIframe):
117+
"""Appended to the doctree by the JupyterliteDirective directive
123118
124-
Renders a Notebook with RetroLite in the docs.
119+
Renders an iframe that shows a Notebook with JupyterLite.
120+
"""
121+
122+
lite_app = "lab"
123+
notebooks_path = ""
124+
125+
126+
class RetroLiteIframe(_LiteIframe):
127+
"""Appended to the doctree by the RetroliteDirective directive
128+
129+
Renders an iframe that shows a Notebook with RetroLite.
125130
"""
126131

132+
lite_app = "retro"
133+
notebooks_path = "notebooks/"
134+
135+
136+
class _LiteDirective(SphinxDirective):
137+
127138
has_content = False
128139
optional_arguments = 1
129140
option_spec = {
@@ -154,7 +165,25 @@ def run(self):
154165
else:
155166
notebook_name = None
156167

157-
return [RetroliteIframe(notebook=notebook_name, width=width, height=height)]
168+
return [self.iframe_cls(notebook=notebook_name, width=width, height=height)]
169+
170+
171+
class JupyterLiteDirective(_LiteDirective):
172+
"""The ``.. jupyterlite::`` directive.
173+
174+
Renders a Notebook with JupyterLite in the docs.
175+
"""
176+
177+
iframe_cls = JupyterLiteIframe
178+
179+
180+
class RetroLiteDirective(_LiteDirective):
181+
"""The ``.. retrolite::`` directive.
182+
183+
Renders a Notebook with RetroLite in the docs.
184+
"""
185+
186+
iframe_cls = RetroLiteIframe
158187

159188

160189
class RetroLiteParser(rst.Parser):
@@ -213,25 +242,34 @@ def jupyterlite_build(app: Sphinx, error):
213242

214243

215244
def setup(app):
216-
# Build the JupyterLite output
217245
app.connect("config-inited", inited)
218246
# We need to build JupyterLite at the end, when all the content was created
219247
app.connect("build-finished", jupyterlite_build)
220248

221249
# Config options
222250
app.add_config_value("jupyterlite_config", None, rebuild="html")
223251

224-
# Initialize RetroLite directive and parser
252+
# Initialize RetroLite and JupyterLite directives
253+
app.add_node(
254+
RetroLiteIframe,
255+
html=(visit_element_html, None),
256+
latex=(skip, None),
257+
textinfo=(skip, None),
258+
text=(skip, None),
259+
man=(skip, None),
260+
)
261+
app.add_directive("retrolite", RetroLiteDirective)
225262
app.add_node(
226-
RetroliteIframe,
263+
JupyterLiteIframe,
227264
html=(visit_element_html, None),
228265
latex=(skip, None),
229266
textinfo=(skip, None),
230267
text=(skip, None),
231268
man=(skip, None),
232269
)
233-
app.add_directive("retrolite", RetroliteDirective)
270+
app.add_directive("jupyterlite", JupyterLiteDirective)
234271

272+
# Initialize RetroLite parser
235273
app.add_source_parser(RetroLiteParser)
236274
app.add_source_suffix(".ipynb", "jupyterlite_notebook")
237275

0 commit comments

Comments
 (0)