sphinx_gallery.scrapers#
Scrapers for embedding images.
Collect images that have been produced by code blocks.
The only scraper we natively support is Matplotlib, others should
live in modules that will support them (e.g., PyVista, Plotly). Scraped
images are injected as rst image-sg
directives into the .rst
file generated for each example script.
Functions#
- sphinx_gallery.scrapers.clean_modules(gallery_conf, fname, when)[source]#
Remove, unload, or reset modules.
After a script is executed it can load a variety of settings that one does not want to influence in other examples in the gallery.
- Parameters:
gallery_conf (dict) β The gallery configuration.
fname (str or None) β The example being run. Will be None when this is called entering a directory of examples to be built.
when (str) β
Whether this module is run before or after examples.
This parameter is only forwarded when the callables accept 3 parameters.
- sphinx_gallery.scrapers.figure_rst(figure_list, sources_dir, fig_titles='', srcsetpaths=None)[source]#
Generate reST for a list of image filenames.
Depending on whether we have one or more figures, we use a single rst call to βimageβ or a horizontal list.
- Parameters:
figure_list (list) β List of strings of the figuresβ absolute paths.
sources_dir (str) β absolute path of Sphinx documentation sources
fig_titles (str) β Titles of figures, empty string if no titles found. Currently only supported for matplotlib figures, default = ββ.
srcsetpaths (list or None) β List of dictionaries containing absolute paths. If empty, then srcset field is populated with the figure path. (see
image_srcset
configuration option). Otherwise, each dict is of the form {0: /images/image.png, 2.0: /images/image_2_00x.png} where the key is the multiplication factor and the contents the path to the image created above.
- Returns:
images_rst (str) β rst code to embed the images in the document
The rst code will have a custom
image-sg
directive that allowsmultiple resolution images to be served e.g.
`` (srcset: /plot_types/imgs/img_001.png,) β /plot_types/imgs/img_2_00x.png 2.00x``
- sphinx_gallery.scrapers.matplotlib_scraper(block, block_vars, gallery_conf, **kwargs)[source]#
Scrape Matplotlib images.
- Parameters:
block (sphinx_gallery.py_source_parser.Block) β The code block to be executed. Format (label, content, lineno).
block_vars (dict) β Dict of block variables.
gallery_conf (dict) β Contains the configuration of Sphinx-Gallery
**kwargs (dict) β
Additional keyword arguments to pass to
savefig()
, e.g.format='svg'
. Theformat
keyword argument in particular is used to set the file extension of the output file (currently only βpngβ, βjpgβ, βsvgβ, βgifβ, and βwebpβ are supported).This is not used internally, but intended for use when overriding the scraper.
- Returns:
rst β The reStructuredText that will be rendered to HTML containing the images. This is often produced by
figure_rst()
.- Return type:
- sphinx_gallery.scrapers.save_figures(block, block_vars, gallery_conf)[source]#
Save all open figures of the example code-block.
- Parameters:
block (sphinx_gallery.py_source_parser.Block) β The code block to be executed. Format (label, content, lineno).
block_vars (dict) β Dict of block variables.
gallery_conf (dict) β Contains the configuration of Sphinx-Gallery
- Returns:
images_rst β rst code to embed the images in the document.
- Return type: