Skip to content

Commit

Permalink
Merge pull request #1066 from jakevdp/sphinx-no-selenium
Browse files Browse the repository at this point in the history
MAINT: make doc build work without selenium
  • Loading branch information
jakevdp authored Aug 3, 2018
2 parents e744f1b + 56e4b5f commit c578fda
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 9 additions & 3 deletions altair/sphinxext/altairgallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import random
import collections
from operator import itemgetter
import warnings

import jinja2

Expand All @@ -14,7 +15,8 @@

from sphinx.util.nodes import nested_parse_with_titles

from .utils import get_docstring_and_rest, prev_this_next, create_thumbnail
from .utils import (get_docstring_and_rest, prev_this_next,
create_thumbnail, create_generic_image)
from altair.utils.execeval import eval_block
from altair.vegalite.v2.examples import iter_examples

Expand Down Expand Up @@ -126,8 +128,12 @@ def save_example_pngs(examples, image_dir, make_thumbnails=True):
# the file changed or the image file does not exist. Generate it.
print('-> saving {0}'.format(image_file))
chart = eval_block(example['code'])
chart.save(image_file)
hashes[filename] = example_hash
try:
chart.save(image_file)
hashes[filename] = example_hash
except ImportError:
warnings.warn("Could not import selenium: using generic image")
create_generic_image(image_file)

with open(hash_file, 'w') as f:
json.dump(hashes, f)
Expand Down
15 changes: 15 additions & 0 deletions altair/sphinxext/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ def create_thumbnail(image_filename, thumb_filename, window_size=(144, 80)):
thumb.save(thumb_filename)


def create_generic_image(filename, shape=(200, 300), gradient=True):
"""Create a generic image"""
from PIL import Image
import numpy as np

assert len(shape) == 2

arr = np.zeros((shape[0], shape[1], 3))
if gradient:
# gradient from gray to white
arr += np.linspace(128, 255, shape[1])[:, None]
im = Image.fromarray(arr.astype('uint8'))
im.save(filename)


SYNTAX_ERROR_DOCSTRING = """
SyntaxError
===========
Expand Down

0 comments on commit c578fda

Please sign in to comment.