@@ -216,45 +216,43 @@ It uses an approach similar to what
216
216
:func: `sphinx_gallery.scrapers.matplotlib_scraper ` does under the hood, which
217
217
use the helper function :func: `sphinx_gallery.scrapers.figure_rst ` to
218
218
create the standardized reST. If your package will be used to write an image
219
- file to disk (e.g., PNG or JPEG), we recommend you use a similar approach,
220
- but via a class so that the ``__repr__ `` can remain stable across Sphinx runs::
219
+ file to disk (e.g., PNG or JPEG), we recommend you use a similar approach::
221
220
222
- class MyModuleScraper():
223
- def __repr__(self):
224
- return 'MyModuleScraper'
221
+ def my_module_scraper(block, block_vars, gallery_conf):
222
+ import mymodule
223
+ # We use a list to collect references to image names
224
+ image_names = list()
225
+ # The `image_path_iterator` is created by Sphinx-Gallery, it will yield
226
+ # a path to a file name that adheres to Sphinx-Gallery naming convention.
227
+ image_path_iterator = block_vars['image_path_iterator']
225
228
226
- def __call__(self, block, block_vars, gallery_conf):
227
- import mymodule
228
- # We use a list to collect references to image names
229
- image_names = list()
230
- # The `image_path_iterator` is created by Sphinx-Gallery, it will yield
231
- # a path to a file name that adheres to Sphinx-Gallery naming convention.
232
- image_path_iterator = block_vars['image_path_iterator']
229
+ # Define a list of our already-created figure objects.
230
+ list_of_my_figures = mymodule.get_figures()
233
231
234
- # Define a list of our already-created figure objects.
235
- list_of_my_figures = mymodule.get_figures()
232
+ # Iterate through figure objects, save to disk, and keep track of paths.
233
+ for fig, image_path in zip(list_of_my_figures, image_path_iterator):
234
+ fig.save_png(image_path)
235
+ image_names.append(image_path)
236
236
237
- # Iterate through figure objects, save to disk, and keep track of paths.
238
- for fig, image_path in zip(list_of_my_figures, image_path_iterator):
239
- fig.save_png(image_path)
240
- image_names.append(image_path)
237
+ # Close all references to figures so they aren't used later.
238
+ mymodule.close('all')
241
239
242
- # Close all references to figures so they aren't used later.
243
- mymodule.close('all')
240
+ # Use the `figure_rst` helper function to generate the reST for this
241
+ # code block's figures. Alternatively you can define your own reST.
242
+ return figure_rst(image_names, gallery_conf['src_dir'])
244
243
245
- # Use the `figure_rst` helper function to generate the reST for this
246
- # code block's figures. Alternatively you can define your own reST.
247
- return figure_rst(image_names, gallery_conf['src_dir'])
248
-
249
- This code would be defined either in your ``conf.py `` file, or as a module that
250
- you import into your ``conf.py `` file. The configuration needed to use this
251
- scraper would look like::
244
+ This code could be defined either in your ``conf.py `` file, or as a module that
245
+ you import into your ``conf.py `` file (see :ref: `importing_callables `).
246
+ The configuration needed to use this scraper would look like::
252
247
253
248
sphinx_gallery_conf = {
254
249
...
255
- 'image_scrapers': ('matplotlib', MyModuleScraper() ),
250
+ 'image_scrapers': ('matplotlib', "my_module._scraper.my_module_scraper" ),
256
251
}
257
252
253
+ Where Sphinx-Gallery will parse the string ``"my_module._scraper.my_module_scraper" ``
254
+ to import the callable function.
255
+
258
256
Example 2: detecting image files on disk
259
257
----------------------------------------
260
258
@@ -264,67 +262,55 @@ the reST needed to embed them in the documentation. Note that the example script
264
262
will still need to be executed to scrape the files, but the images
265
263
don't need to be produced during the execution.
266
264
267
- We'll use a callable class in this case, and assume it is defined within your
268
- package in a module called ``scraper ``. Here is the scraper code::
265
+ We assume the function is defined within your
266
+ package in a module called ``_scraper ``. Here is the scraper code::
269
267
270
268
from glob import glob
271
269
import shutil
272
270
import os
273
271
from sphinx_gallery.scrapers import figure_rst
274
272
275
- class PNGScraper(object):
276
- def __init__(self):
277
- self.seen = set()
278
-
279
- def __repr__(self):
280
- return 'PNGScraper'
281
-
282
- def __call__(self, block, block_vars, gallery_conf):
283
- # Find all PNG files in the directory of this example.
284
- path_current_example = os.path.dirname(block_vars['src_file'])
285
- pngs = sorted(glob(os.path.join(path_current_example, '*.png')))
286
-
287
- # Iterate through PNGs, copy them to the sphinx-gallery output directory
288
- image_names = list()
289
- image_path_iterator = block_vars['image_path_iterator']
290
- for png in pngs:
291
- if png not in self.seen:
292
- self.seen |= set(png)
293
- this_image_path = image_path_iterator.next()
294
- image_names.append(this_image_path)
295
- shutil.move(png, this_image_path)
296
- # Use the `figure_rst` helper function to generate reST for image files
297
- return figure_rst(image_names, gallery_conf['src_dir'])
298
-
273
+ def png_scraper(block, block_vars, gallery_conf):
274
+ # Find all PNG files in the directory of this example.
275
+ path_current_example = os.path.dirname(block_vars['src_file'])
276
+ pngs = sorted(glob(os.path.join(path_current_example, '*.png')))
277
+
278
+ # Iterate through PNGs, copy them to the Sphinx-Gallery output directory
279
+ image_names = list()
280
+ image_path_iterator = block_vars['image_path_iterator']
281
+ seen = set()
282
+ for png in pngs:
283
+ if png not in seen:
284
+ seen |= set(png)
285
+ this_image_path = image_path_iterator.next()
286
+ image_names.append(this_image_path)
287
+ shutil.move(png, this_image_path)
288
+ # Use the `figure_rst` helper function to generate reST for image files
289
+ return figure_rst(image_names, gallery_conf['src_dir'])
299
290
300
291
Then, in our ``conf.py `` file, we include the following code::
301
292
302
- from mymodule import PNGScraper
303
-
304
293
sphinx_gallery_conf = {
305
294
...
306
- 'image_scrapers': ('matplotlib', PNGScraper() ),
295
+ 'image_scrapers': ('matplotlib', 'my_module._scraper.png_scraper' ),
307
296
}
308
297
309
298
Example 3: matplotlib with SVG format
310
299
-------------------------------------
311
300
The :func: `sphinx_gallery.scrapers.matplotlib_scraper ` supports ``**kwargs ``
312
301
to pass to :meth: `matplotlib.figure.Figure.savefig `, one of which is the
313
302
``format `` argument. See :ref: `custom_scraper ` for supported formats.
314
- To use SVG, you can do::
303
+ To use SVG you can define the following function and ensure it is
304
+ :ref: `importable <importing_callables >`::
315
305
316
- from sphinx_gallery.scrapers import matplotlib_scraper
306
+ def matplotlib_svg_scraper(*args, **kwargs):
307
+ return matplotlib_scraper(*args, format='svg', **kwargs)
317
308
318
- class matplotlib_svg_scraper(object):
319
- def __repr__(self):
320
- return self.__class__.__name__
321
-
322
- def __call__(self, *args, **kwargs):
323
- return matplotlib_scraper(*args, format='svg', **kwargs)
309
+ Then in your ``conf.py ``::
324
310
325
311
sphinx_gallery_conf = {
326
312
...
327
- 'image_scrapers': (matplotlib_svg_scraper() ,),
313
+ 'image_scrapers': ("sphinxext. matplotlib_svg_scraper" ,),
328
314
...
329
315
}
330
316
@@ -335,35 +321,31 @@ writing a customized scraper class or function.
335
321
336
322
Example 4: Mayavi scraper
337
323
-------------------------
338
- Historically, sphinx-gallery supported scraping Mayavi figures as well as
324
+ Historically, Sphinx-Gallery supported scraping Mayavi figures as well as
339
325
matplotlib figures. However, due to the complexity of maintaining the scraper,
340
326
support was deprecated in version 0.12.0. To continue using a Mayavi scraping,
341
327
consider using something like the following::
342
328
343
329
from sphinx_gallery.scrapers import figure_rst
344
330
345
- class MayaviScraper():
346
- def __repr__(self):
347
- return 'MyModuleScraper'
348
-
349
- def __call__(self, block, block_vars, gallery_conf):
350
- from mayavi import mlab
351
- image_path_iterator = block_vars['image_path_iterator']
352
- image_paths = list()
353
- e = mlab.get_engine()
354
- for scene, image_path in zip(e.scenes, image_path_iterator):
355
- try:
356
- mlab.savefig(image_path, figure=scene)
357
- except Exception:
358
- mlab.close(all=True)
359
- raise
360
- # make sure the image is not too large
361
- scale_image(image_path, image_path, 850, 999)
362
- if 'images' in gallery_conf['compress_images']:
363
- optipng(image_path, gallery_conf['compress_images_args'])
364
- image_paths.append(image_path)
365
- mlab.close(all=True)
366
- return figure_rst(image_paths, gallery_conf['src_dir'])
331
+ def mayavi_scraper(self, block, block_vars, gallery_conf):
332
+ from mayavi import mlab
333
+ image_path_iterator = block_vars['image_path_iterator']
334
+ image_paths = list()
335
+ e = mlab.get_engine()
336
+ for scene, image_path in zip(e.scenes, image_path_iterator):
337
+ try:
338
+ mlab.savefig(image_path, figure=scene)
339
+ except Exception:
340
+ mlab.close(all=True)
341
+ raise
342
+ # make sure the image is not too large
343
+ scale_image(image_path, image_path, 850, 999)
344
+ if 'images' in gallery_conf['compress_images']:
345
+ optipng(image_path, gallery_conf['compress_images_args'])
346
+ image_paths.append(image_path)
347
+ mlab.close(all=True)
348
+ return figure_rst(image_paths, gallery_conf['src_dir'])
367
349
368
350
Integrate custom scrapers with Sphinx-Gallery
369
351
---------------------------------------------
@@ -421,14 +403,15 @@ For example, to reset matplotlib to always use the ``ggplot`` style, you could d
421
403
style.use('ggplot')
422
404
423
405
Any custom functions can be defined (or imported) in ``conf.py `` and given to
424
- the ``reset_modules `` configuration key. To add the function defined above::
406
+ the ``reset_modules `` configuration key. To add the function defined above (assuming
407
+ you've make it :ref: `importable <importing_callables >`)::
425
408
426
409
sphinx_gallery_conf = {
427
410
...
428
- 'reset_modules': (reset_mpl, ' seaborn' ),
411
+ 'reset_modules': ("sphinxext. reset_mpl", " seaborn" ),
429
412
}
430
413
431
- In the config above ``' seaborn' `` refers to the native seaborn resetting
414
+ In the config above ``" seaborn" `` refers to the native seaborn resetting
432
415
function (see :ref: `reset_modules `).
433
416
434
417
.. note :: Using resetters such as ``reset_mpl`` that deviate from the
@@ -441,7 +424,6 @@ after an example, a function signature with three parameters can be used, where
441
424
the third parameter is required to be named ``when ``::
442
425
443
426
def reset_mpl(gallery_conf, fname, when):
444
-
445
427
import matplotlib as mpl
446
428
mpl.rcParams['lines.linewidth'] = 2
447
429
if when == 'after' and fname=='dashed_lines':
0 commit comments