# Usage¶

## Installation¶

### nbsphinx¶

Install nbsphinx with pip:

python3 -m pip install nbsphinx --user


If you want to install it system-wide for all users (assuming you have the necessary rights), just drop the --user flag.

If you suddenly change your mind, you can un-install it with:

python3 -m pip uninstall nbsphinx


Depending on your Python installation, you may have to use python instead of python3. Recent versions of Python already come with pip pre-installed. If you don’t have it, you can install it manually.

To upgrade to the newest release, use the --upgrade flag:

python3 -m pip install nbsphinx --upgrade --user


If you don’t want to use pip, you can also try to install nbsphinx with your package manager. For example, there is a package for conda-forge and packages for Debian, Ubuntu, Arch and Gentoo Linux. Note that those might not provide the latest releases.

### pandoc¶

The stand-alone program pandoc is used to convert Markdown content to something Sphinx can understand. You have to install this program separately, ideally with your package manager. If you are using conda, you can select the conda-forge channel to install the pandoc package. If that doesn’t work out for you, have a look at pandoc’s installation instructions.

Note:

The use of pandoc in nbsphinx is temporary, but will likely stay that way for a long time, see issue #36.

### Syntax Highlighting¶

To get proper syntax highlighting in code cells, you’ll need an appropriate Pygments lexer. This of course depends on the programming language of your Jupyter notebooks (more specifically, the pygments_lexer metadata of your notebooks).

For example, if you use Python in your notebooks, you’ll have to have the IPython package installed:

python3 -m pip install IPython --user


You’ll most likely have this installed already.

Note:

If you are using Anaconda and syntax highlighting in code cells doesn’t seem to work, you can try to install IPython with pip, or as a work-around, add 'IPython.sphinxext.ipython_console_highlighting' to extensions in my conf.py.

For details, see Anaconda issue #1430 and nbsphinx issue #24.

### Jupyter Kernel¶

If you want to execute your notebooks during the Sphinx build process (see Executing Notebooks), you need an appropriate Jupyter kernel installed.

For example, if you use Python, you should install the ipykernel package:

python3 -m pip install ipykernel --user


Again, it’s very likely that you have that installed already.

## Sphinx Setup¶

In the directory with your notebook files, run this command (assuming you have Sphinx installed already):

python3 -m sphinx.quickstart


Answer the questions that appear on the screen. In case of doubt, just press the <Return> key repeatedly to take the default values.

After that, there will be a few brand-new files in the current directory. You’ll have to make a few changes to the file named conf.py. You should at least check if those two variables contain the right things:

extensions = [
'nbsphinx',
'sphinx.ext.mathjax',
]
exclude_patterns = ['_build', '**.ipynb_checkpoints']


Once your conf.py is in place, edit the file named index.rst and add the file names of your notebooks (with or without the .ipynb extension) to the toctree directive.

autosummary bug:

If you are using the sphinx.ext.autosummary Sphinx extension, there is a bug in Sphinx (below version 1.5) which prevents notebooks from being parsed. As a work-around you can explicitly list all the files for which autosummary should be ran using the autosummary_generate variable in conf.py. For example,

autosummary_generate = ['myfile1.rst', 'myfile2.rst']


## Running Sphinx¶

To create the HTML pages, use this command:

python3 -m sphinx <source-dir> <build-dir>


If you have many notebooks, you can do a parallel build by using the -j option:

python3 -m sphinx <source-dir> <build-dir> -j<number-of-processes>


For example, if your source files are in the current directory and you have 4 CPU cores, you can run this:

python3 -m sphinx . _build -j4


Afterwards, you can find the main HTML file in _build/index.html.

Subsequent builds will be faster, because only those source files which have changed will be re-built. To force re-building all source files, use the -E option.

To create LaTeX output, use:

python3 -m sphinx <source-dir> <build-dir> -b latex


If you don’t know how to create a PDF file from the LaTeX output, you should have a look at Latexmk (see also this tutorial).

Sphinx can automatically check if the links you are using are still valid. Just invoke it like this:

python3 -m sphinx <source-dir> <build-dir> -b linkcheck


## Watching for Changes with sphinx-autobuild¶

If you think it’s tedious to run the Sphinx build command again and again while you make changes to your notebooks, you’ll be happy to hear that there is a way to avoid that: sphinx-autobuild!

It can be installed with

python3 -m pip install sphinx-autobuild --user


You can start auto-building your files with

sphinx-autobuild <source-dir> <build-dir>


This will start a local webserver which will serve the generated HTML pages at http://localhost:8000/. Whenever you save changes in one of your notebooks, the appropriate HTML page(s) will be re-built and when finished, your browser view will be refreshed automagically. Neat!

You can also abuse this to auto-build the LaTeX output:

sphinx-autobuild <source-dir> <build-dir> -b latex


However, to auto-build the final PDF file as well, you’ll need an additional tool. Again, you can use latexmk for this (see above). Change to the build directory and run

latexmk -pdf -pvc


If your PDF viewer isn’t opened because of LaTeX build errors, you can use the command line flag -f to force creating a PDF file.

## Automatic Creation of HTML and PDF output on readthedocs.org¶

There are two different methods, both of which are described below.

In both cases, you’ll first have to create an account on https://readthedocs.org/ and connect your Github/Bitbucket account. Instead of connecting, you can also manually add any publicly available Git/Subversion/Mercurial/Bazaar repository.

After doing the steps described below, you only have to “push” to your repository, and the HTML pages and the PDF file of your stuff are automagically created on readthedocs.org. Awesome!

You can even have different versions of your stuff, just use Git tags and branches and select in the readthedocs.org settings (under “Admin”, “Versions”) which of those should be created.

If your new versions are not automatically built, go to the “Settings” of your Github repository, continue to “Integrations & services”, and make sure that “ReadTheDocs” is listed and activated in the “Services” section. If not, use “Add service”. There is probably a similar thing for Bitbucket and others.

Note:

If you want to execute notebooks (see Controlling Notebook Execution), you’ll need to install the appropriate Jupyter kernel. In the examples below, the IPython kernel ist installed from the packet ipykernel.

### Using requirements.txt¶

1. Create a file named requirements.txt (or whatever name you wish) in your repository containing the required pip packages:

sphinx>=1.4
ipykernel
nbsphinx


You can also install directly from Github et al., using a specific branch/tag/commit, e.g.

git+https://github.com/spatialaudio/nbsphinx.git@master

2. In the “Advanced Settings” on readthedocs.org, specify the path to your requirements.txt file (or however you called it) in the box labeled “Requirements file”. Kinda obvious, isn’t it?

3. Still in the “Advanced Settings”, make sure the right Python interpreter is chosen. This must be the same version (2.x or 3.x) as you were using in your notebooks!

### Using conda¶

1. Create a file named readthedocs.yml in the main directory of your repository, containing the name of yet another file:

conda:

2. Create the file mentioned above. You can choose whatever name you want (it may also live in a subdirectory, e.g. doc/environment.yml), it just has to match whatever is specified in readthedocs.yml. The second file describes a conda environment and should contain something like this:

channels:
- conda-forge
dependencies:
- python==3.5
- sphinx>=1.4
- pandoc
- nbconvert
- ipykernel
- pip:
- nbsphinx


You can of course add further conda and pip packages. You can also install packages directly from Github et al., using a specific branch/tag/commit, e.g.

- pip:
- git+https://github.com/spatialaudio/nbsphinx.git@master


Note:

The specification of the conda-forge channel is necessary for the pandoc package, which is not part of the default channel.

Note:

Most of the “Advanced Settings” on readthedocs.org will be ignored if you have a readthedocs.yml file.

Warning:

If you have a very long repository name (or branch name), you might run into this quite obscure problem: ‘placeholder too short’.

## HTML Themes¶

The nbsphinx extension does not provide its own theme, you can use any of the available themes or create a custom one, if you feel like it.

The following (incomplete) list of themes contains several links for each theme:

1. The documentation (or the official sample page) of this theme (if available, see also the documentation of the built-in Sphinx themes)
2. How the nbsphinx documentation looks when using this theme
3. How to enable this theme using either requirements.txt or readthedocs.yml and theme-specific settings (in some cases)

### 3rd-Party Themes¶

If you know of another Sphinx theme that should be included here, please open an issue on Github.