Allow setting animation format from gallery config#1243
Allow setting animation format from gallery config#1243larsoner merged 8 commits intosphinx-gallery:masterfrom
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. |
lucyleeow
left a comment
There was a problem hiding this comment.
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.
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.
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 ! |
… to version 0.17.0 v0.17.0 ------- Support for Python 3.8 and Sphinx 4 dropped in this release. Requirement is now Python >= 3.9 and Sphinx >= 5. **Implemented enhancements:** - Introduction tooltip corresponds to the first paragraph `#1344 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1344>`__ (`fgmacedo <https://github.com/fgmacedo>`__) - FIX Jupyterlite in CircleCI artifact `#1336 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1336>`__ (`lesteve <https://github.com/lesteve>`__) - MNT: Rename README.rst to GALLERY_HEADER.rst `#1321 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1321>`__ (`timhoffm <https://github.com/timhoffm>`__) - [ENH] Add custom thumbnails for failing examples `#1313 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1313>`__ (`tsbinns <https://github.com/tsbinns>`__) - ENH integrate download/launcher links into ``pydata-sphinx-theme`` secondary sidebar `#1312 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1312>`__ (`Charlie-XIAO <https://github.com/Charlie-XIAO>`__) - add option for zip downloads `#1299 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1299>`__ (`jamiecook <https://github.com/jamiecook>`__) - Allow setting animation format from gallery config `#1243 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1243>`__ (`QuLogic <https://github.com/QuLogic>`__) (NEWS truncated at 15 lines)
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.htmlglobally in amatplotlibrc. But unlike styling, I think settinganimation.htmlin 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_animationsgallery configuration option to accept a(enabled: bool, fmt: str | None)tuple (with the single boolean allowed for backwards-compatibility.) Thisfmtsetting allows picking betweenjshtmlorhtml5(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
tinybuildhere,plot_animation.htmlis 93% smaller, from 823992 to 58214 bytes.Note: this is based on #1241 and #1242 right now, so I marked it draft.