Skip to content

Commit

Permalink
Add gallery feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeier committed Mar 3, 2020
1 parent f792bd3 commit dd29cac
Show file tree
Hide file tree
Showing 14 changed files with 1,000 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ nbsphinx.egg-info/
.python-version
.vscode
doc/_build
doc/gallery/a-local-file.png
40 changes: 40 additions & 0 deletions doc/a-normal-rst-file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,43 @@ see https://github.com/mcmtroffaes/sphinxcontrib-bibtex/issues/156.
There is an alternative Sphinx extension for creating bibliographies:
https://bitbucket.org/wnielson/sphinx-natbib/.
However, this project seems to be abandoned (last commit in 2011).


Thumbnail Galleries
-------------------

With ``nbsphinx`` you can create thumbnail galleries in notebook files
as described in :ref:`/subdir/gallery.ipynb`.

If you like, you can also create such galleries in reST files
using the ``nbgallery`` directive.

It takes the same parameters as the `toctree`__ directive.

__ https://www.sphinx-doc.org/en/master/usage/restructuredtext/
directives.html#directive-toctree

.. note::

The notes regarding LaTeX in :ref:`/subdir/gallery.ipynb`
and :ref:`/subdir/toctree.ipynb` also apply here!

The following example gallery was created using:

.. code-block:: rest
.. nbgallery::
:caption: This is a thumbnail gallery:
:name: rst-gallery
:glob:
:reversed:
gallery/*-rst
.. nbgallery::
:caption: This is a thumbnail gallery:
:name: rst-gallery
:glob:
:reversed:

gallery/*-rst
9 changes: 9 additions & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
'sphinxcontrib.rsvgconverter', # for SVG->PDF conversion in LaTeX output
]

import sphinx_gallery
html_static_path = [sphinx_gallery.glr_path_static()]
html_css_files = ['gallery.css']

# Exclude build directory and Jupyter backup files:
exclude_patterns = ['_build', '**.ipynb_checkpoints']

Expand All @@ -35,6 +39,11 @@
# Environment variables to be passed to the kernel:
os.environ['MY_DUMMY_VARIABLE'] = 'Hello from conf.py!'

nbsphinx_thumbnails = {
'gallery/thumbnail-from-conf-py': 'gallery/a-local-file.png',
'gallery/*-rst': '_static/copy-button.svg',
}

# This is processed by Jinja2 and inserted before each notebook
nbsphinx_prolog = r"""
{% set docname = 'doc/' + env.doc2path(env.docname, base=None) %}
Expand Down
120 changes: 120 additions & 0 deletions doc/gallery/cell-metadata.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"nbsphinx": "hidden"
},
"source": [
"This notebook is part of the `nbsphinx` documentation: https://nbsphinx.readthedocs.io/."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using Cell Metadata to Select a Thumbnail\n",
"\n",
"If the [nbsphinx-thumbnail](cell-tag.ipynb) cell tag is not enough,\n",
"you can use cell metadata to specify more options.\n",
"\n",
"The last cell in this notebook has this metadata:\n",
"\n",
"```json\n",
"{\n",
" \"nbsphinx-thumbnail\": {\n",
" \"tooltip\": \"This tooltip message was defined in cell metadata\"\n",
" }\n",
"}\n",
"```\n",
"\n",
"If there are multiple outputs in the selected cell,\n",
"the last one is used.\n",
"See [Choosing from Multiple Outputs](multiple-outputs.ipynb)\n",
"for how to select a specific output."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.rcParams['image.cmap'] = 'coolwarm'\n",
"plt.rcParams['image.origin'] = 'lower'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Some example data stolen from\n",
"https://matplotlib.org/examples/pylab_examples/pcolor_demo.html:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x, y = np.meshgrid(np.arange(-3, 3, 0.1), np.arange(-2, 2, 0.1))\n",
"z = (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"zmax = np.max(np.abs(z))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbsphinx-thumbnail": {
"tooltip": "This tooltip message was defined in cell metadata"
}
},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"ax.imshow(z, vmin=-zmax, vmax=zmax)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
81 changes: 81 additions & 0 deletions doc/gallery/cell-tag.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"nbsphinx": "hidden"
},
"source": [
"This notebook is part of the `nbsphinx` documentation: https://nbsphinx.readthedocs.io/."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using a Cell Tag to Select a Thumbnail\n",
"\n",
"You can select any code cell (with appropriate output)\n",
"by tagging it with the `nbsphinx-thumbnail` tag.\n",
"\n",
"If there are multiple outputs in the selected cell,\n",
"the last one is used.\n",
"See [Choosing from Multiple Outputs](multiple-outputs.ipynb)\n",
"for how to select a specific output.\n",
"If you want to show a tooltip, have a look at\n",
"[Using Cell Metadata to Select a Thumbnail](cell-metadata.ipynb)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following cell has the `nbsphinx-thumbnail` tag:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"nbsphinx-thumbnail"
]
},
"outputs": [],
"source": [
"fig, ax = plt.subplots(figsize=[6, 3])\n",
"ax.plot([4, 9, 7, 20, 6, 33, 13, 23, 16, 62, 8])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
46 changes: 46 additions & 0 deletions doc/gallery/due-rst.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"nbsphinx": "hidden"
},
"source": [
"This notebook is part of the `nbsphinx` documentation: https://nbsphinx.readthedocs.io/."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dummy Notebook 2 for Gallery\n",
"\n",
"This is a dummy file just to fill\n",
"[the gallery in the reST file](../a-normal-rst-file.rst#thumbnail-galleries).\n",
"\n",
"The thumbnail image is assigned in [conf.py](../conf.py)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit dd29cac

Please sign in to comment.