Skip to content

Commit 71d0a4a

Browse files
authored
Merge pull request #39 from jbweston/docs
add documentation page
2 parents ccbcee9 + cd977be commit 71d0a4a

File tree

6 files changed

+304
-70
lines changed

6 files changed

+304
-70
lines changed

README.md

+6-70
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Jupyter Sphinx Extensions
22

3-
Jupyter-sphinx enables the rendering of Jupyter interactive widgets in sphinx
4-
documentation.
3+
``jupyter-sphinx`` enables running code embedded in Sphinx documentation and
4+
embedding output of that code into the resulting document. It has support
5+
for rich output such as images and even Jupyter interactive widgets.
56

67
## Installation
78

@@ -17,76 +18,11 @@ with conda:
1718
conda install jupyter_sphinx -c conda-forge
1819
```
1920

20-
## Render Jupyter Interactive Widgets `jupyter_sphinx.embed_widgets`
21+
## Usage
2122

22-
This extension provides a means of rendering Jupyter interactive widgets within
23-
sphinx documentation.
23+
You can check out the documentation on https://jupyter-sphinx.readthedocs.io for up to date
24+
usage information and examples.
2425

25-
It is derived from the [`altair`](https://github.com/altair-viz/altair) sphinx
26-
extension, which is distributed under the terms of the BSD 3-Clause license.
27-
28-
### Enabling the extension
29-
30-
To enable this extension, add `jupyter_sphinx.embed_widgets` to your enabled
31-
extensions in `conf.py`.
32-
33-
### Directives
34-
35-
Two directives are provided: `ipywidgets-setup` and `ipywidgets-display`.
36-
37-
`ipywidgets-setup` code is used to set-up various options
38-
prior to running the display code. For example:
39-
40-
```rst
41-
.. ipywidgets-setup::
42-
43-
from ipywidgets import VBox, jsdlink, IntSlider, Button
44-
45-
.. ipywidgets-display::
46-
47-
s1, s2 = IntSlider(max=200, value=100), IntSlider(value=40)
48-
b = Button(icon='legal')
49-
jsdlink((s1, 'value'), (s2, 'max'))
50-
VBox([s1, s2, b])
51-
```
52-
53-
In the case of the `ipywidgets-display` code, if the *last statement* of the
54-
code-block returns a widget object, it will be rendered.
55-
56-
### Options
57-
58-
The directives have the following options:
59-
60-
```rst
61-
.. ipywidgets-setup::
62-
:show: # if set, then show the setup code as a code block
63-
64-
from ipywidgets import Button
65-
66-
.. ipywidgets-display::
67-
:hide-code: # if set, then hide the code and only show the widget
68-
:code-below: # if set, then code is below rather than above the widget
69-
:alt: text # alternate text when widget cannot be rendered
70-
71-
Button()
72-
```
73-
74-
### Configuration
75-
76-
File `conf.py` has two extra (optional) configuration options:
77-
78-
* `jupyter_sphinx_require_url`: url for `require.js` (if your theme already provides this, set it to False or '')
79-
* `jupyter_sphinx_embed_url`: url for the embedding, if set to None (default) a proper default will be taken from the `ipywidgets.embed` module.
80-
81-
### Misc.
82-
83-
- For the widgets to be succesfuly rendered, this extension requires an
84-
internet connection, since it depends on a cdn-served JavaScript file to be
85-
loaded.
86-
- Widgets rendered on the same page use the same widget manager. As a
87-
consequence, they can be linked with each other via JavaScript link widgets.
88-
However, no kernel is connected and therefore, interaction with the backend
89-
will not happen.
9026

9127
## License
9228

doc/Makefile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line.
5+
SPHINXOPTS =
6+
SPHINXBUILD = sphinx-build
7+
SOURCEDIR = source
8+
BUILDDIR = build
9+
10+
# Put it first so that "make" without argument is like "make help".
11+
help:
12+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
13+
14+
.PHONY: help Makefile
15+
16+
# Catch-all target: route all unknown targets to Sphinx using the new
17+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
18+
%: Makefile
19+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

doc/source/conf.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Configuration file for the Sphinx documentation builder.
4+
#
5+
# This file does only contain a selection of the most common options. For a
6+
# full list see the documentation:
7+
# http://www.sphinx-doc.org/en/master/config
8+
9+
import os
10+
import sys
11+
12+
sys.path.insert(0, os.path.abspath('../..'))
13+
14+
import jupyter_sphinx
15+
16+
project = 'Jupyter Sphinx'
17+
copyright = '2019, Jupyter Development Team'
18+
author = 'Jupyter Development Team'
19+
20+
# The full version, including alpha/beta/rc tags
21+
release = jupyter_sphinx.__version__
22+
# The short X.Y version
23+
version = release[:len(release) - len(release.lstrip('0123456789.'))].rstrip('.')
24+
25+
extensions = [
26+
'sphinx.ext.mathjax',
27+
'jupyter_sphinx.execute',
28+
]
29+
30+
html_theme = 'alabaster'

doc/source/index.rst

+232
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
Jupyter Sphinx Extension
2+
========================
3+
4+
.. toctree::
5+
:maxdepth: 2
6+
:caption: Contents:
7+
8+
Jupyter Sphinx is a Sphinx extension that executes embedded code in a Jupyter
9+
kernel, and embeds outputs of that code in the output document. It has support
10+
for rich output such as images, Latex math and even javascript widgets.
11+
12+
Enabling the extension
13+
----------------------
14+
To enable the extension, add ``jupyter_sphinx.execute`` to your enabled extensions in
15+
``conf.py``.
16+
17+
Basic Usage
18+
-----------
19+
20+
You can use the ``jupyter-execute`` directive to embed code into the document::
21+
22+
.. jupyter-execute::
23+
24+
name = 'world'
25+
print('hello ' + name + '!')
26+
27+
The above is rendered as follows:
28+
29+
.. jupyter-execute::
30+
31+
name = 'world'
32+
print('hello ' + name + '!')
33+
34+
Note that the code produces *output* (printing the string 'hello world!'), and the output
35+
is rendered directly after the code snippet.
36+
37+
Because all code cells in a document are run in the same kernel, cells later in the document
38+
can use variables and functions defined in cells earlier in the document:
39+
40+
.. jupyter-execute::
41+
42+
a = 1
43+
print('first cell: a = {}'.format(a))
44+
45+
.. jupyter-execute::
46+
47+
a += 1
48+
print('second cell: a = {}'.format(a))
49+
50+
Because jupyter-sphinx uses the machinery of ``nbconvert``, it is capable of rendering
51+
much richer output than mere text; plots, for example:
52+
53+
.. jupyter-execute::
54+
55+
import numpy as np
56+
from matplotlib import pyplot
57+
%matplotlib inline
58+
59+
x = np.linspace(0, 2 * np.pi)
60+
61+
pyplot.plot(x, np.sin(x) / x)
62+
pyplot.plot(x, np.cos(x))
63+
pyplot.grid()
64+
65+
or even full-blown javascript widgets:
66+
67+
.. jupyter-execute::
68+
69+
import ipywidgets as w
70+
from IPython.display import display
71+
72+
a = w.IntSlider()
73+
b = w.IntText()
74+
display(a, b)
75+
w.jslink((a, 'value'), (b, 'value'))
76+
77+
78+
Directive options
79+
-----------------
80+
You may choose to hide the code of a cell, but keep its output visible using ``:hide-code:``::
81+
82+
.. jupyter-execute::
83+
:hide-code:
84+
85+
print('this code is invisible')
86+
87+
produces:
88+
89+
.. jupyter-execute::
90+
:hide-code:
91+
92+
print('this code is invisible')
93+
94+
or vice versa with ``:hide-output:``::
95+
96+
.. jupyter-execute::
97+
:hide-output:
98+
99+
print('this output is invisible')
100+
101+
produces:
102+
103+
.. jupyter-execute::
104+
:hide-output:
105+
106+
print('this output is invisible')
107+
108+
You may also display the code *below* the output with ``:code-below:``::
109+
110+
.. jupyter-execute::
111+
:code-below:
112+
113+
print('this output is above the code')
114+
115+
produces:
116+
117+
.. jupyter-execute::
118+
:code-below:
119+
120+
print('this code is below the output')
121+
122+
123+
Controlling exceptions
124+
----------------------
125+
126+
The default behaviour when jupyter-sphinx encounters an error in the embedded code is just to
127+
stop execution of the document and display a stack trace. However, there are many cases where it may be
128+
illustrative for execution to continue and for a stack trace to be shown as *output of the cell*. This
129+
behaviour can be enabled by using the ``raises`` option::
130+
131+
.. jupyter-execute::
132+
:raises:
133+
134+
1 / 0
135+
136+
produces:
137+
138+
.. jupyter-execute::
139+
:raises:
140+
141+
1 / 0
142+
143+
Note that when given no arguments, ``raises`` will catch all errors. It is also possible to give ``raises``
144+
a list of error types; if an error is raised that is not in the list then execution stops as usual::
145+
146+
.. jupyter-execute::
147+
:raises: KeyError, ValueError
148+
149+
a = {'hello': 'world!'}
150+
a['jello']
151+
152+
produces:
153+
154+
.. jupyter-execute::
155+
:raises: KeyError, ValueError
156+
157+
a = {'hello': 'world!'}
158+
a['jello']
159+
160+
161+
Controlling the execution environment
162+
-------------------------------------
163+
The execution environment can be controlled by using the ``jupyter-kernel`` directive. This directive takes
164+
the name of the Jupyter kernel in which all future cells (until the next ``jupyter-kernel`` directive) should
165+
be run::
166+
167+
.. jupyter-kernel:: python3
168+
:id: a_unique_name
169+
170+
``jupyter-kernel`` can also take a directive option ``:id:`` that names the Jupyter session;
171+
it is used in conjunction with the ``jupyter-download`` roles described in the next section.
172+
173+
Note that putting a ``jupyter-kernel`` directive starts a *new* kernel, so any variables and functions declared
174+
in cells *before* a ``jupyter-kernel`` directive will not be available in future cells.
175+
176+
Note that we are also not limited to working with Python: Jupyter Sphinx supports kernels for
177+
any programming language, and we even get proper syntax highlighting thanks to the power of
178+
Pygments.
179+
180+
Downloading the code as a script
181+
--------------------------------
182+
Jupyter Sphinx includes 2 roles that can be used to download the code embedded in a document:
183+
``:jupyter-download:script:`` (for a raw script file) and ``:jupyter-download:notebook:`` (for
184+
a Jupyter notebook). For example, to download all the code from this document as a script we
185+
would use::
186+
187+
:jupyter-download:script:`index`
188+
189+
Which produces a link like this: :jupyter-download:script:`index`. The name that the role is
190+
applied to (``index`` in this case) is the name of the document for which you wish to download
191+
the code. If a document contains ``jupyter-kernel`` directives with ``:id:`` specified, then
192+
the name provided to ``:id:`` can be used to get the code for the cells belonging to the
193+
that Jupyter session.
194+
195+
196+
Configuration options
197+
---------------------
198+
199+
Typically you will be using Sphinx to build documentation for a software package.
200+
201+
If you are building documentation for a Python package you should add the following
202+
lines to your sphinx ``conf.py``::
203+
204+
import os
205+
206+
package_path = os.path.abspath('../..')
207+
os.environ['PYTHONPATH'] = ':'.join((package_path, os.environ.get('PYTHONPATH', '')))
208+
209+
This will ensure that your package is importable by any IPython kernels, as they will
210+
inherit the environment variables from the main Sphinx process.
211+
212+
Here is a list of all the configuration options available to the Jupyter Sphinx extension:
213+
214+
jupyter_execute_default_kernel
215+
216+
The default kernel to launch when executing code in ``jupyter-execute`` directives.
217+
The default is ``python3``.
218+
219+
220+
jupyter_execute_data_priority
221+
222+
The display priority of different output mimetypes. Mimetypes earlier in the data priority
223+
list are preferred over later ones. This is relevant if a code cell produces an output
224+
that has several possible representations (e.g. description text or an image).
225+
The default is
226+
``['application/vnd.jupyter.widget-view+json', 'text/html', 'image/svg+xml', 'image/png', 'image/jpeg', 'text/latex', 'text/plain']``.
227+
228+
229+
jupyter_execute_kwargs
230+
231+
Keyword arguments to pass to ``nbconvert.preprocessors.execute.executenb``, which controls how
232+
code cells are executed. The default is ``dict(timeout=-1, allow_errors=True)``.

environment.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: jupyter-sphinx
2+
3+
channels:
4+
- conda-forge
5+
6+
dependencies:
7+
- python=3.6
8+
- sphinx>=0.6
9+
- ipywidgets>=7.0.0
10+
- IPython
11+
- nbconvert>=5.4
12+
- nbformat
13+
# For documentation building and testing
14+
- matplotlib
15+
- pytest

readthedocs.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
conda:
2+
environment: environment.yml

0 commit comments

Comments
 (0)