-
Notifications
You must be signed in to change notification settings - Fork 197
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
Allow setting animation format from gallery config #1243
Conversation
Note, it is possible to achieve this externally by replacing def matplotlib_html5_scraper(block, block_vars, gallery_conf, **kwargs):
matplotlib.rcParams['animation.html'] = 'html5'
return sphinx_gallery.scrapers.matplotlib_scraper(block, block_vars, gallery_conf, **kwargs) However, I would like to extend the |
The suggestion in our docs is to use a resetter like:
For a real-life example we tweak params in MNE-Python: I think it's a more general way to tackle this problem. If you agree, do you see a way to make this feature more discoverable in our docs? |
See mne-tools/mne-python#12298 for an example that implements this |
I think using the |
Actually I consider it to be more of the opposite --
Ahh I see what you mean. As long as what you implement here is compatible with users having already set |
I was under the impression that the
I've managed to implement this locally using |
Yeah you can replace or run after (or before!) depending on how you set the params
+1 from me. The |
783fa85
to
46e8b93
Compare
I've posted what I have so you can see the issues I see with the
For those reasons I think it might be best to use Also, I'm not sure where to add |
Agreed a new directive would be good For testing you can probably add it here https://github.com/sphinx-gallery/sphinx-gallery/blob/master/continuous_integration/install.sh#L43 and/or in the |
d3cddf2
to
ddd2555
Compare
I started writing a new directive, before thinking maybe someone has done this before, and so I found sphinxcontrib-video. It is currently optional unless you've asked for external files, but I could make it required or an extra (it's only a single Python file, so not that heavy of a dependency) if you prefer that. Unfortunately, I also needed to do some patching there, so this depends on my branch for sphinx-contrib/video#36, but this will hopefully be temporary. |
41a88f1
to
a4962b6
Compare
I rebased now that |
How about:
Then the skip will only be hit on Windows pip which I guess is okay. Bonus territory would be adding a windows conda job so that it's not skipped, but I can add that later if needed |
6f0dcd4
to
21f490c
Compare
Not sure if you got pinged when this was marked ready, but it should be good to go now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a flyby as I'm not so familiar with animation.
Will take another look once there are docs! Thanks!
if dpi == "figure": | ||
dpi = fig.dpi | ||
video_uri = video.relative_to(gallery_conf["src_dir"]).as_posix() | ||
html = _ANIMATION_VIDEO_RST.format( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question, in what cases would we use _ANIMATION_VIDEO_RST
instead of _ANIMATION_RST
, i.e., when is html
None
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
html
is None
if fmt not in (None, "html5", "jshtml")
; those are the originally supported formats kept for backwards compatibility.
This will then place them external to the HTML file, saving the extra base64 encoding.
I rebased, updated for your suggestions, and added some docs. |
For my understanding:
is this right? |
This extension supports more of the HTML video tag's options, allowing for things like looping, autoplay, etc.
Yes, I believe that to be correct. Feel free to push any followups; I'll be on vacation. |
for more information, see https://pre-commit.ci
Failures unrelated and I due to new sphinx release, see sphinx-doc/sphinx#12299 |
Will merge in a few days, in case @larsoner wants to check. |
Looks great, thanks @QuLogic for working on this and @lucyleeow for reviewing ! |
Matplotlib currently defaults to
rcParams['animation.html'] = 'none'
, which ends up producingjshtml
. This is an HTML fragment with base64-encoded PNG frames, and this becomes fairly large. A better option ishtml5
, which produces an mp4 video (though still base64 encoded). The docs do mention you can set thercParam
, but that is reset for every example by sphinx-gallery callingrcdefaults
.As discussed on #280, that reset is intentional, but does mean that you cannot set
animation.html
globally in amatplotlibrc
. But unlike styling, I think settinganimation.html
in every example is distracting. It is an option forAnimation.save
, which is called by sphinx-gallery somewhere outside the example, so it's really not a relevant configuration for someone reading the example.This PR modifies the
matplotlib_animations
gallery configuration option to accept a(enabled: bool, fmt: str | None)
tuple (with the single boolean allowed for backwards-compatibility.) Thisfmt
setting allows picking betweenjshtml
orhtml5
(or the previous method if set toNone
.) I have not yet updated the documentation, but will do so if this seems desirable.This change does mean you get a video with browser controls instead of the frame-by-frame one that Matplotlib provides, but on the other hand, you save a bunch of space. When configured on the
tinybuild
here,plot_animation.html
is 93% smaller, from 823992 to 58214 bytes.Note: this is based on #1241 and #1242 right now, so I marked it draft.