Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Matplotlib scraper #1241

Merged
merged 4 commits into from
Dec 20, 2023
Merged

Conversation

QuLogic
Copy link
Contributor

@QuLogic QuLogic commented Dec 15, 2023

Three things:

  • Validate image_srcset once during configuration, instead of repeatedly on every scraper run. Note, I also passed the scaling numbers through a set, in order to prevent saving extra times.
  • Simplify the animation part of the scraper; instead of looping through all animations to match a figure, use a dictionary for direct lookup.
  • Add a note about kwargs to matplotlib_scraper. I was confused why nothing seemed to call it with kwargs, but was able to find it in other parts of the documentation. So I mentioned that in the docstring as well.

@QuLogic
Copy link
Contributor Author

QuLogic commented Dec 15, 2023

Interesting, the Pillow writer doesn't support Paths; I'll put a str back in there, but fix it in Matplotlib as well.

Copy link
Contributor

@larsoner larsoner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one minor concern, otherwise looks useful!

Copy link
Contributor

@lucyleeow lucyleeow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Nitpick and some questions.

)
srcset_mult_facs = set()
for st in srcset:
if not isinstance(st, str) or st[-1:] not in ("", "x"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, why the logic order re-shuffle here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous version did not check types, so this adds that, and I wanted to only have one raise.

extension of the output file (currently only 'png', 'jpg', 'svg',
'gif', and 'webp' are supported).

This is not used internally, but intended to be used when overriding
Copy link
Contributor

@lucyleeow lucyleeow Dec 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is not used internally, but intended to be used when overriding
This is not used internally, but intended for use when overriding

Small nitpick

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not grammatically correct, unless you mean "for use when..."?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry I've amended. I just want to make clearer that it is API available 'for' the user to use.

@@ -80,7 +80,7 @@ def test_save_matplotlib_figures(gallery_conf, ext):
def test_save_matplotlib_figures_hidpi(gallery_conf):
"""Test matplotlib hidpi figure save."""
ext = "png"
gallery_conf["image_srcset"] = ["2x"]
gallery_conf["image_srcset"] = [2]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm confused, 'image_srcset' does not have to be a string with 'x' at the end?

Copy link
Contributor Author

@QuLogic QuLogic Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, but now it's normalized in the configuration initialization, to avoid doing that on every scrape. But these tests appear to skip that step, so I needed to set it in its normalized form. I could add that initialization here instead (and actually maybe that should be tested as well.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh right, that is confusing.

I could add that initialization here instead

+1

actually maybe that should be tested as well.)

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, though this spilled over into some other tests. Not sure if that warrants a separate PR.

Instead of on every call to the scraper.
Instead of looping through all animations to match each figure, use a
dictionary to map back to them.
These are for external users.
Copy link
Contributor

@larsoner larsoner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, @lucyleeow feel free to merge if you're happy!

@lucyleeow lucyleeow merged commit 7f4edbc into sphinx-gallery:master Dec 20, 2023
@lucyleeow
Copy link
Contributor

Thanks @QuLogic !

@QuLogic QuLogic deleted the simplify-scraper branch December 20, 2023 20:12
"Must be a list of strings with the multiplicative "
'factor followed by an "x". e.g. ["2.0x", "1.5x"]'
)
if st == "":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QuLogic was just looking at our check config func - why do we want to allow item in gallery_conf["image_srcset"] to be an empty string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That preserved the previous code; I think it's always been that way since its initial PR, but as of #808 (comment), I think that was dropped from the documentation or usage, but the config handling part never did.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thank you 🙏 , I have a vague memory of empty string being a config option previously now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants