Skip to content

Commit

Permalink
Merge pull request #1026 from pelson/sphinx_gallery
Browse files Browse the repository at this point in the history
Use sphinx-gallery with original cartopy.examples __tags__ structure
  • Loading branch information
pelson committed Feb 20, 2018
2 parents 9cddf07 + 473244e commit 4f37a34
Show file tree
Hide file tree
Showing 55 changed files with 290 additions and 185 deletions.
17 changes: 0 additions & 17 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ doc-run: &doc-build
make html
working_directory: docs

doc-bundle-run: &doc-bundle
name: Bundle sphinx-gallery documentation artifacts
command: |
tar cf docs/build/sphinx-gallery-files.tar.gz \
docs/source/api \
docs/source/cartopy_outline.rst \
docs/source/gallery
when: always


##########################################
# Here is where the real jobs are defined.
Expand All @@ -92,10 +83,6 @@ jobs:

- run: *doc-build

- run: *doc-bundle
- store_artifacts:
path: docs/build/sphinx-gallery-files.tar.gz

- store_artifacts:
path: docs/build/html

Expand All @@ -119,10 +106,6 @@ jobs:

- run: *doc-build

- run: *doc-bundle
- store_artifacts:
path: docs/build/sphinx-gallery-files.tar.gz

- store_artifacts:
path: docs/build/html

Expand Down
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ lib/cartopy/geodesic/_geodesic.pyd
docs/build
lib/cartopy/tests/mpl/output/
docs/source/cartopy_outline.rst
docs/source/examples
docs/source/gallery.rst
docs/source/gallery
docs/source/api
docs/source/matplotlib/coastlines.p??

# some data files:
lib/cartopy/data/SRTM3
Expand Down Expand Up @@ -51,4 +49,4 @@ __pycache__/

# Egg that gets created with 'pip install -v -e .'
lib/Cartopy.egg-info/
.eggs/
.eggs/
3 changes: 1 addition & 2 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ help:

clean:
-rm -rf $(BUILDDIR)/*
-rm -rf source/examples/* source/cartopy_outline.rst source/gallery.rst
-rm -rf source/cartopy_outline.rst source/gallery source/matplotlib/coastlines.p??

html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
python add_noshow.py
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."

Expand Down
37 changes: 0 additions & 37 deletions docs/add_noshow.py

This file was deleted.

2 changes: 1 addition & 1 deletion docs/doc-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ netCDF4
iris
iris-sample-data
cf_units
sphinx-gallery
sphinx-gallery =0.1.12
beautifulsoup4
13 changes: 3 additions & 10 deletions docs/source/_static/layout.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
.gallery-images img { border: none;}
.gallery-images p { display: inline;}

.inline-paragraphs p {display: inline;}

.highlight {background: none;}
Expand All @@ -21,16 +18,12 @@ h1 {

/* Sphinx Gallery */

div#cartopy-gallery.section {
overflow: hidden;
.sphx-glr-download a {
background-image: none !important;
background-color: white !important;
}

div.section div.figure {
padding-left: 0 !important;
text-align: center;
}

/* hack to make the __init__.py files not display in SG*/
div.dontshow {
display: none !important;
}
8 changes: 5 additions & 3 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
# available in order
# to benefit from cached rebuilds of plots.
'sphinxext.plot_directive',
# Monkey-patch sphinx_gallery to handle cartopy's __tags__
# example convention.
'sphinxext.pre_sphinx_gallery',
'sphinx_gallery.gen_gallery',
'sphinx.ext.napoleon'
]
Expand All @@ -81,7 +84,7 @@

# General information about the project.
project = u'cartopy'
copyright = u'2011 - 2015 British Crown Copyright' # the template will need
copyright = u'2011 - 2018 British Crown Copyright' # the template will need
# updating if this is changed

# The version info for the project you're documenting, acts as replacement for
Expand All @@ -101,8 +104,7 @@
'gallery_dirs': ['gallery'],
'doc_module': ('cartopy',),
'reference_url': {'cartopy': None},
'backreferences_dir': 'api/_as_gen',
# Not currently used but kept for future, just in case.
'backreferences_dir': '../build/backrefs',
}

# The language for content autogenerated by Sphinx. Refer to documentation
Expand Down
2 changes: 2 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ and is at version |version|. You can find the source code for cartopy on
`our github page <https://github.com/SciTools/cartopy>`_.


.. _getting-started-with-cartopy:

Getting started
===============

Expand Down
8 changes: 4 additions & 4 deletions docs/source/matplotlib/advanced_plotting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ visualising vector fields:
:meth:`streamplots <cartopy.mpl.geoaxes.GeoAxes.streamplot>` (:ref:`example <examples-streamplot>`)
each with their own benefits for displaying certain vector field forms.

.. figure:: ../gallery/vector_data/images/sphx_glr_arrows_001.png
:target: ../gallery/vector_data/arrows.html
.. figure:: ../gallery/images/sphx_glr_arrows_001.png
:target: ../gallery/arrows.html
:align: center
:scale: 50

Expand All @@ -151,7 +151,7 @@ vector field into a regular grid on the target projection (done via
:func:`cartopy.vector_transform.vector_scalar_to_grid`). This is enabled with the ``regrid_shape``
keyword and can have a massive impact on the effectiveness of the visualisation:

.. figure:: ../gallery/vector_data/images/sphx_glr_regridding_arrows_001.png
:target: ../gallery/vector_data/regridding_arrows.html
.. figure:: ../gallery/images/sphx_glr_regridding_arrows_001.png
:target: ../gallery/regridding_arrows.html
:align: center
:scale: 50
4 changes: 2 additions & 2 deletions docs/source/matplotlib/feature_interface.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ For a full list of names in this dictionary:
Example of using the Feature class with the Matplotlib interface
----------------------------------------------------------------

.. figure:: ../gallery/lines_and_polygons/images/sphx_glr_feature_creation_001.png
:target: ../gallery/lines_and_polygons/feature_creation.html
.. figure:: ../gallery/images/sphx_glr_feature_creation_001.png
:target: ../gallery/feature_creation.html
:align: center
:scale: 50
177 changes: 177 additions & 0 deletions docs/source/sphinxext/pre_sphinx_gallery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
"""
Override sphinx_gallery's treatment of groups (folders) with cartopy's
``__tags__`` semantics. This is tightly bound to the sphinx_gallery
implementation, hence the explicit version checking.
"""
from collections import OrderedDict
import os.path
import shutil
import tempfile
import textwrap

import sphinx_gallery.gen_gallery
import sphinx_gallery.gen_rst
from sphinx_gallery.gen_rst import (
write_backreferences, extract_intro, _thumbnail_div,
generate_file_rst, sphinx_compatibility)


if sphinx_gallery.__version__ not in ['0.1.12']:
raise RuntimeError('not tested with this version of sphinx_gallery ({}). '
'Please modify this check, and validate sphinx_gallery'
' behaves as expected.'
''.format(sphinx_gallery.__version__))


GALLERY_HEADER = textwrap.dedent("""
Cartopy Gallery
---------------
The following visual examples demonstrate some of the functionality of
Cartopy, particularly its matplotlib interface.
For a structured introduction to cartopy, including some of these
examples, see :ref:`getting-started-with-cartopy`.
""")


def example_groups(src_dir):
"""Return a dictionary of {tag: [example filenames]} for the given dir."""

sorted_listdir = [fname for fname in sorted(os.listdir(src_dir))
if fname.endswith('.py') and not fname.startswith('_')]
tagged_examples = {}

for fname in sorted_listdir:
fpath = os.path.join(src_dir, fname)
__tags__ = []
with open(fpath, 'r') as fh:
for line in fh:
# Crudely remove the __tags__ line.
if line.startswith('__tags__ = '):
exec(line.strip(), locals(), globals())
for tag in __tags__:
tagged_examples.setdefault(tag, []).append(fname)
break
else:
tag = 'Miscellanea'
tagged_examples.setdefault(tag, []).append(fname)
return tagged_examples


def order_examples(tagged_examples):
"""Order the tags and their examples."""
preferred_tag_order = ['Introductory',
'Lines and polygons',
'Scalar data',
'Vector data',
'Web services']

def sort_key(item):
tag = item[0]
try:
index = preferred_tag_order.index(tag)
except ValueError:
index = len(preferred_tag_order) + 1

return (index, tag.lower())
sorted_items = sorted(tagged_examples.items(), key=sort_key)
return OrderedDict(sorted_items)


def write_example(src_fpath, target_dir):
target_fpath = os.path.join(target_dir, os.path.basename(src_fpath))
with open(src_fpath, 'r') as fh:
with open(target_fpath, 'w') as fh_out:
for line in fh:
# Crudely remove the __tags__ line.
if line.startswith('__tags__ = '):
continue
fh_out.write(line)


def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs):
"""Generate the gallery reStructuredText for an example directory"""

fhindex = GALLERY_HEADER

if not os.path.exists(target_dir):
os.makedirs(target_dir)
tagged_examples = example_groups(src_dir)
tagged_examples = order_examples(tagged_examples)

computation_times = []
build_target_dir = os.path.relpath(target_dir, gallery_conf['src_dir'])

seen = set()
tmp_dir = tempfile.mkdtemp()

for tag, examples in tagged_examples.items():
sorted_listdir = examples

entries_text = []
iterator = sphinx_compatibility.status_iterator(
sorted_listdir,
'Generating gallery for %s ' % tag,
length=len(sorted_listdir))
for fname in iterator:
write_example(os.path.join(src_dir, fname), tmp_dir)
amount_of_code, time_elapsed = generate_file_rst(
fname, target_dir, tmp_dir, gallery_conf)

if fname not in seen:
seen.add(fname)
computation_times.append((time_elapsed, fname))

new_fname = os.path.join(src_dir, fname)
intro = extract_intro(new_fname)
this_entry = _thumbnail_div(build_target_dir, fname, intro) + textwrap.dedent("""
.. toctree::
:hidden:
/%s
""") % os.path.join(build_target_dir, fname[:-3]).replace(os.sep, '/') # noqa: E501

entries_text.append((amount_of_code, this_entry))

if gallery_conf['backreferences_dir']:
write_backreferences(seen_backrefs, gallery_conf,
target_dir, fname, intro)

# sort to have the smallest entries in the beginning
entries_text.sort()

fhindex += textwrap.dedent("""
{tag}
{tag_underline}
.. container:: gallery_images
""".format(tag=tag, tag_underline='-' * len(tag)))

for _, entry_text in entries_text:
fhindex += '\n '.join(entry_text.split('\n'))

# clear at the end of the section
fhindex += """.. raw:: html\n
<div style='clear:both'></div>\n\n"""

# Tidy up the temp directory
shutil.rmtree(tmp_dir)

return fhindex, computation_times


# Monkey-patch sphinx_gallery to handle cartopy's example format.
sphinx_gallery.gen_rst.generate_dir_rst = generate_dir_rst
sphinx_gallery.gen_gallery.generate_dir_rst = generate_dir_rst


def setup(app):
pass
Loading

0 comments on commit 4f37a34

Please sign in to comment.