4. Jupyterο
This is optional.
It is possible to use Jupyter in Sphinx files.
- For the ability to edit the python code live and view the ouput there are two options:
jupyter-sphinx
sphinx-thebe
- For the ability to view the ouput from jupyter notebook files use:
nbsphinx
- For the ability to convert the docs rst files to jupyter notebook files use:
sphinxcontrib-jupyter
4.1. Jupyter-sphinxο
Jupyter-sphinx is a Sphinx extension that executes embedded code in a Jupyter kernel, and embeds outputs of that code in the document. It has support for rich output such as Latex math. It allows live code execution, and thereby, interactive code blocks.
It has the drawback that it is not fully compatable with the sphinx-copybutton extension. When both are used together the text βCopy to clipboardβ has to be manually deleted form the live python code before it will run properly.
4.1.1. Install the jupyter-sphinx extensionο
Install the jupyter-sphinx extension from the command line.
pip install jupyter-sphinx
4.1.2. docs/requirements.txtο
Add this text in the requirements file
docs/requirements.txt
:
jupyter-sphinx==0.4.0
4.1.3. VSCode conf.py fileο
Add this text into the file:
docs/conf.py
.
import os
import sys
sys.path.insert(0, os.path.abspath('../../'))
package_path = os.path.abspath('../..')
os.environ['PYTHONPATH'] = ':'.join((package_path, os.environ.get('PYTHONPATH', '')))
Edit the extensions to add
jupyter_sphinx
in the file:docs/conf.py
.
extensions = [
'jupyter_sphinx',
]
To enable interactive cells, add this text into the file:
docs/conf.py
.
jupyter_sphinx_thebelab_config = {
'requestKernel': True,
'binderOptions': {
'repo': "binder-examples/requirements",
},
}
4.1.4. Usageο
Use the jupyter-execute directive to embed code into the document.
.. jupyter-execute::
x = 4
y = 5
ans = x * y
print('x * y = ' + str(ans))
Interactive cells are activated with a button click.
By default the button is added at the end of the document, but it may also be inserted anywhere using:
.. thebe-button:: Optional title
4.2. sphinx-thebeο
4.2.1. Install the sphinx-thebe extensionο
Install the sphinx-thebe extension from the command line.
pip install sphinx-thebe
4.2.2. docs/requirements.txtο
Add this text in the requirements file
docs/requirements.txt
:
sphinx==4.5.0
sphinx-thebe==0.1.2
4.2.3. VSCode conf.py fileο
Edit the extensions to add
jupyter_sphinx
, in the file:docs/conf.py
.
extensions = [
'sphinx_thebe',
]
4.2.4. Usageο
Interactive cells are activated with a button click.
Insert the button anywhere using:
.. thebe-button:: Activate Jupyter
4.2.5. class: thebeο
Use the
:class: thebe
directive in a python code block to give thebe access to run the code.
def name_age_greeting(name, age):
age += 1
return "Hi " + name + ", you are " + str(age) + " years old"
print(name_age_greeting("Joe", 12))
4.3. nbsphinxο
.ipynb
, with sphinx.4.3.1. Install the nbsphinx extensionο
Install the nbsphinx extension from the command line.
pip install nbsphinx
To edit .ipynb files in VSCode install the ipykernel package.
4.3.2. VSCode Jupyter extensionο
In VSCode, click the Extensions icon in the left side bar.
Type
Jupyter
into the search box.Install the Jupyter extension.
To edit .ipynb files in VSCode install the python ipykernel package.
See: https://ipython.org/
pip install ipykernel
4.3.3. docs/requirements.txtο
Add this text in the requirements file
docs/requirements.txt
:
nbsphinx==0.8.9
4.3.4. VSCode conf.py fileο
Edit the extensions to add
nbsphinx
, in the file:docs/conf.py
.
extensions = [
'nbsphinx',
]
4.3.5. Usageο
4.4. sphinxcontrib-jupyterο
.ipynb
, with sphinx.4.4.1. Install the sphinxcontrib-jupyter extensionο
Install the sphinxcontrib-jupyter extension from the command line.
pip install sphinxcontrib-jupyter
4.4.2. VSCode conf.py fileο
Edit the extensions to add
nbsphinx
, in the file:docs/conf.py
.
extensions = [
'sphinxcontrib.jupyter',
]
4.4.3. Usageο
cd docs
.\make jupyter