Skip to content

Commit

Permalink
Add gif extension to Sphinx-Gallery scraper (pyvista#4403)
Browse files Browse the repository at this point in the history
* Add gif extension

* Reset cache

* Add test for GIFs

* Revert cache regeneration

* Fix test

* use cells for the example

---------

Co-authored-by: Alex Kaszynski <[email protected]>
  • Loading branch information
AlejandroFernandezLuces and akaszynski authored May 11, 2023
1 parent 93f06cc commit 438dd0d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/02-plot/gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
"jittery" GIFs, especially for the scalar bar.
"""
# sphinx_gallery_thumbnail_number = 2

import numpy as np

import pyvista as pv

###############################################################################
# Create a structured grid
# ~~~~~~~~~~~~~~~~~~~~~~~~
# Create a structured grid and make a "wave" my shifting the Z position based
# on the cartesian distance from the origin.

x = np.arange(-10, 10, 0.5)
y = np.arange(-10, 10, 0.5)
x, y = np.meshgrid(x, y)
Expand All @@ -23,6 +30,13 @@

# Create and structured surface
grid = pv.StructuredGrid(x, y, z)
grid.plot()


###############################################################################
# Generate a GIF
# ~~~~~~~~~~~~~~
# Generate a GIF using ``off_screen=True`` parameter.

# Create a plotter object and set the scalars to the Z height
plotter = pv.Plotter(notebook=False, off_screen=True)
Expand Down
1 change: 1 addition & 0 deletions pyvista/utilities/sphinx_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __call__(self, block, block_vars, gallery_conf):
fname = next(image_path_iterator)
if hasattr(plotter, '_gif_filename'):
# move gif to fname
fname = fname[:-3] + "gif"
shutil.move(plotter._gif_filename, fname)
else:
plotter.screenshot(fname)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ def test_scraper(tmpdir, monkeypatch, n_win):
monkeypatch.setattr(pyvista, 'BUILDING_GALLERY', True)
pyvista.close_all()
plotters = [pyvista.Plotter(off_screen=True) for _ in range(n_win)]
plotter_gif = pyvista.Plotter()

scraper = Scraper()
src_dir = str(tmpdir)
out_dir = op.join(str(tmpdir), '_build', 'html')

img_fnames = [
op.join(src_dir, 'auto_examples', 'images', f'sg_img_{n}.png') for n in range(n_win)
]

# create and save GIF to tmpdir
gif_path = op.abspath(tmpdir + 'sg_img_0.gif')
plotter_gif.open_gif(gif_path)
plotter_gif.write_frame()
plotter_gif.close()

gallery_conf = {"src_dir": src_dir, "builder_name": "html"}
target_file = op.join(src_dir, 'auto_examples', 'sg.py')
block = None
Expand All @@ -32,9 +42,13 @@ def test_scraper(tmpdir, monkeypatch, n_win):
example_globals=dict(a=1),
target_file=target_file,
)

os.makedirs(op.dirname(img_fnames[0]))
for img_fname in img_fnames:
assert not os.path.isfile(img_fname)

# add gif to list after checking other filenames are empty
img_fnames.append(gif_path)
os.makedirs(out_dir)
scraper(block, block_vars, gallery_conf)
for img_fname in img_fnames:
Expand Down

0 comments on commit 438dd0d

Please sign in to comment.