Adds plot_exclude_patterns config to selectively disable plot_directive.#31270
Adds plot_exclude_patterns config to selectively disable plot_directive.#31270billbrod wants to merge 8 commits intomatplotlib:mainfrom
plot_exclude_patterns config to selectively disable plot_directive.#31270Conversation
|
Thank you for opening your first PR into Matplotlib! If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. You can also join us on gitter for real-time discussion. For details on testing, writing docs, and our review process, please see the developer guide. We strive to be a welcoming and open project. Please follow our Code of Conduct. |
| error will be raised. | ||
|
|
||
| plot_exclude_patterns | ||
| List of regex patterns to check against to selectively skip plot |
There was a problem hiding this comment.
My standard question when matching file patterns 😄: Do you really want regular expressions or are wildcard patterns (fnmatch) sufficient/better?
There was a problem hiding this comment.
I was not aware of fnmatch -- that looks sufficient (and easier) to me! I'll update the PR to use that instead.
|
|
||
| plot_exclude_patterns | ||
| List of regex patterns to check against to selectively skip plot | ||
| directives. If any pattern matches the name of the file containing the |
There was a problem hiding this comment.
Is filename enough or do we need a relative path?
There was a problem hiding this comment.
Oh relative path might be better. That increases the chance of false positive matches, but makes it easier to do a simple "disable all plot directives found in the API documentation", which is nice. I'll do that
There was a problem hiding this comment.
You may also want to check https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.full_match and https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.match, which may be more convenient than fnmatch.
|
Pushed changes following @timhoffm suggestions. Some questions this raised for me:
|
|
The failing tests look unrelated to my changes, should I try and fix them? |
|
Yes the failing tests are unrelated. On "a lot of wildcards": As mentioned above, you may also want to check https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.full_match and https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.match, which may be more convenient than fnmatch. |
|
Sorry this took me a bit, I was at a conference. It looks like
|
|
Hi all, wanted to bump this. |
|
Responded to your comments |
|
Please rebase so that the doc builds work. |
7e4e832 to
0f01f46
Compare
|
I believe I did the rebase, let me know if I did it incorrectly |
0f01f46 to
8378b29
Compare
| List of non-recursive glob-style patterns to check against to selectively | ||
| skip plot directives. If any pattern matches the relative path of the file | ||
| containing the plot directive, then that plot directive will be skipped. |
There was a problem hiding this comment.
| List of non-recursive glob-style patterns to check against to selectively | |
| skip plot directives. If any pattern matches the relative path of the file | |
| containing the plot directive, then that plot directive will be skipped. | |
| List of non-recursive glob-style patterns for selectively skipping plot | |
| directives. If any pattern matches the relative path of a documentation | |
| file, then plot directives in that file will be skipped. |
I'm not clear, but assume this works only for documentation source files (i.e. .rst files), not for plot directives in docstrings and not for sphinx-gallery input files. Is that correct?
There was a problem hiding this comment.
This works for all .rst files, but that includes plot directives in docstrings, at least in my setup. I'm using sphinx autosummary for my API docs, which generates rst files that sphinx then uses.
I am not sure about sphinx-gallery, since I don't use that package. I'll try testing this on a minimal example and see how that goes.
| else: | ||
| extra_args = [] | ||
| # Build the pages with warnings turned into errors | ||
| build_sphinx_html(tmp_path, doctree_dir, html_dir, |
There was a problem hiding this comment.
I'm slightly worried on the runtime cost 12x (parametrized) building a documentation seems quite expensive. Have you tested locally how long this test takes?
There was a problem hiding this comment.
This is on my laptop, not a workstation or cluster
There was a problem hiding this comment.
I appreciate the rigor of your testing, but that’s is too much. Imagine a developer runs the whole test suite, they’ll have to wait 23s longer just so that this comparably niche feature is tested.
Please condense to one test with a single documentation build. A basic test that this works is sufficient. By adding some variants to the list you can still test the most relevant cases: exclude by full filename, exclude full directory, exclude files with a specific pattern, e.g. a specific prefix.
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
|
Following up on
This works on all three possibilities, as can be seen in my minimal working example. In The wrinkle here, and this is something we went back and forth on, is that it matches the For my MWE, that's and you can see the three names to match there:
Properly, you can look at the .rst files produced by your apidocs generator and sphinx-gallery: What this doesn't allow you to match, which might be surprising to users is:
|
|
So to clarify: This always uses the .rst file from which the docs are rendered. If the plot directive is in a Python file, e.g. docstrings or Sphinx-gallery example, the .rst filename and location depends on the respective tooling that generates the intermediate .rst representation. Is this correct? If so, that should be added to the description of the option. |
|
On a general note: is filename a good (I.e. easily usable) filter? The alternative could be to give authors the ability to give plots markers and filter on them, similar to pytest markers. In the simplest case you could have a “slow” marker and just exclude them. |



PR summary
I opened a discussion on discourse asking whether it was possible to temporarily disable the plot_directive, and was told by @timhoffm that "There is currently no clean way of doing this. One could add a config value. It’s not important enough that I’d work on this myself, but I’d accept a PR for it." This is my attempt at that change.
Inspired by myst-nb's
nb_execution_excludepatternsconfig option, this allows users to specify in their sphinx configuration a list of patterns to selectively avoid running some plot directives. The goal is to speed up build time when developing, especially when making changes to project documentation. It does this in a simple way, by checkingre.matchagainst theoutput_basefor the created plots for all patterns in the list. If any match, we do not run the code, setresult = [(code, [])], and continue. Settingresultin this way makes it so the appearance in the built docs is the same as if an error was encountered: code is displayed if so configured, no associated plot.function_nameor something more specific, but it looks like that is often not visible to the sphinx extension, especially when building examples found in docstrings (which is my most common use case).re.fullmatchagainstoutput_base, but it seems to me that the full output_base might be difficult for a user to predict. Using justre.matchmay be over-zealous, but given that this is intended to speed up builds during development (not during production), false positives seem more acceptable to me.nb_execution_excludepatterns) are most common for filenames, and the use ofre.matchmeans that simple matches (e.g., just"plot") will function as a substring check, while still allowing for more complicated logic.plot_exclude_patterns(with the underscore) rather thanplot_excludepatterns(which would match myst-nb's value) to be more consistent with the naming pattern of the other config values.AI Disclosure
No LLMs were used in this contribution.
PR checklist
plot_directive.py, from grepping for the other config values.