Skip to content

Commit

Permalink
matplotlib svg scraper with no pickling issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fsoubelet committed Jul 22, 2024
1 parent 6808991 commit c9f0617
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
19 changes: 3 additions & 16 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import sys
import warnings

from sphinx_gallery.scrapers import matplotlib_scraper
from sphinx_gallery.sorting import ExampleTitleSortKey

import pyhdtoolkit

# ignore numpy warnings, see:
Expand All @@ -28,7 +25,7 @@
warnings.filterwarnings(
"ignore",
category=UserWarning,
message="Matplotlib is currently using agg, which is a" " non-GUI backend, so cannot show the figure.",
message="Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.",
)

TOPLEVEL_DIR = pathlib.Path(__file__).parent.parent.absolute()
Expand All @@ -41,14 +38,6 @@
# See: https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_type_aliases
autodoc_type_aliases = {"ArrayLike": "ArrayLike"}

# To use SVG outputs when scraping matplotlib figures for the sphinx-gallery
class matplotlib_svg_scraper:
def __repr__(self):
return self.__class__.__name__

def __call__(self, *args, **kwargs):
return matplotlib_scraper(*args, format="svg", **kwargs)


# -- Project information -----------------------------------------------------

Expand Down Expand Up @@ -177,18 +166,16 @@ def __call__(self, *args, **kwargs):
"examples_dirs": ["../examples"], # directory where to find plotting scripts
"gallery_dirs": ["gallery"], # directory where to store generated plots
"filename_pattern": "^((?!sgskip).)*$", # which files to execute, taken from matplotlib
# "subsection_order": ExampleTitleSortKey,
# "within_subsection_order": ExampleTitleSortKey,
"backreferences_dir": "gen_modules/backreferences", # where function/class granular galleries are stored
# Modules for which function/class level galleries are created
"doc_module": "pyhdtoolkit",
# "image_scrapers": (matplotlib_svg_scraper(),), # scrape gallery as SVG
"image_scrapers": ("pyhdtoolkit.plotting.utils._matplotlib_svg_scraper",), # scrape gallery as SVG
"image_srcset": ["2x"], # use srcset twice as dense for high-resolution images display
"min_reported_time": 2, # minimum execution time to enable reporting
"remove_config_comments": True, # remove config comments from the code
"capture_repr": ("_repr_html_",),
"compress_images": ("images", "thumbnails", "-o1"),
"only_warn_on_example_error": True, # keep the build going if an example fails, very important for doc workflow
"only_warn_on_example_error": True, # keep the build going if an example fails, important for doc workflow
}

# Config for the sphinx_panels extension
Expand Down
9 changes: 9 additions & 0 deletions pyhdtoolkit/plotting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,12 @@ def _determine_default_sbs_phase_ylabel(plane: str) -> str:
beginning = r"\Delta "
term = r"\phi_{x}" if plane.upper() == "X" else r"\phi_{y}"
return r"$" + beginning + term + r"$"

# ----- Sphinx Gallery Scraper ----- #

# To use SVG outputs when scraping matplotlib figures for the sphinx-gallery, see:
# https://sphinx-gallery.github.io/stable/advanced.html#example-3-matplotlib-with-svg-format
def _matplotlib_svg_scraper(*args, **kwargs):
from sphinx_gallery.scrapers import matplotlib_scraper
kwargs.pop("format", None)
return matplotlib_scraper(*args, format="svg", **kwargs)

0 comments on commit c9f0617

Please sign in to comment.