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 10 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.
|
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. |
|
Sorry to come in after must of the discussion is done, but I have some skepticism of this, particularly as the usecase is to temporarily edit the conf.py files during development. Would controlling this via an |
| assert st in (html_dir / 'nestedpage2/index.html').read_text(encoding='utf-8') | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('plot_exclude_patterns', [False, "*index*", |
There was a problem hiding this comment.
At some level this is really testing the functionality of Path.match which I am happy to assume works as documented. I think just one is enough in this case (or maybe one with 1 and one with multiple).
That's not necessary for Sphinx options; you can pass |
|
For the tests: the fact that I was testing so many variations is I think a legacy of the fact that I initially was using regex and wanted to make sure I understood how it worked. I agree that now that I'm using I also agree that a mark would be better (much easier to use) and an overall "Skip all" would be sufficient for my use case, and it might be preferable to keep it really simple. I'm not familiar with that many examples using a decorator with Sphinx, only Deprecated. If we want to go that route, are there other packages you're familiar with that I can look at as examples?
I see this functionality being used by having some logic in How should I proceed on this? Like I said, I think either of your suggestions (mark/decorator or some sort of global "skip all") work well, and are simpler to use / explain than my current solution, so are preferable. I defer to you all as to what makes the most sense. |
|
If skipall is sufficient to you, let's just do that. That's much simpler than adding markers and we can always add markers later should the need arise. |
no longer parametrizes, just tests that we don't create any images when plot_skip_all=True
b67b208 to
0a1931b
Compare
|
Okay, I went ahead and switched this to now be I kept the logic at the same place, so that the only thing that is skipped is running the code (all the machinery around captions, show code, etc. are unaffected). Happy to move it if that would be better. |
|
Can you please also add a whats_new entry including how to use this from the command line (as @QuLogic noted above)? |
Done, let me know if you have any comments on it. I also added This made me realize maybe |


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.