Merge pull request #31510 from QuLogic/default-html-animation · matplotlib/matplotlib@5910a8b · GitHub
Skip to content

Commit 5910a8b

Browse files
authored
Merge pull request #31510 from QuLogic/default-html-animation
Make Animation._repr_html_ more useful by default
2 parents 49a080c + 95fab38 commit 5910a8b

6 files changed

Lines changed: 57 additions & 19 deletions

File tree

Lines changed: 7 additions & 0 deletions

‎galleries/users_explain/animations/animations.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
# - `.Animation.to_jshtml` to create HTML code with interactive JavaScript animation
6565
# controls
6666
# - `.Animation.save` to save the animation to a file
67+
# - Place the animation as the last statement in a Jupyter Notebook cell; this will
68+
# automatically render the animation as video and display it as the cell output,
69+
# unless :rc:`animation.html` is set to ``'none'``.
6770
#
6871
# The following table shows a few plotting methods, the artists they return and some
6972
# commonly used ``set_*`` methods that update the underlying data. While updating data

‎lib/matplotlib/animation.py‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ def _end_redraw(self, event):
12761276
self._resize_id = self._fig.canvas.mpl_connect('resize_event',
12771277
self._on_resize)
12781278

1279-
def to_html5_video(self, embed_limit=None):
1279+
def to_html5_video(self, embed_limit=None, *, _writer_cls=None):
12801280
"""
12811281
Convert the animation to an HTML5 ``<video>`` tag.
12821282
@@ -1319,10 +1319,11 @@ def to_html5_video(self, embed_limit=None):
13191319
path = Path(tmpdir, "temp.m4v")
13201320
# We create a writer manually so that we can get the
13211321
# appropriate size for the tag
1322-
Writer = writers[mpl.rcParams['animation.writer']]
1323-
writer = Writer(codec='h264',
1324-
bitrate=mpl.rcParams['animation.bitrate'],
1325-
fps=1000. / self._interval)
1322+
if _writer_cls is None:
1323+
_writer_cls = writers[mpl.rcParams['animation.writer']]
1324+
writer = _writer_cls(codec='h264',
1325+
bitrate=mpl.rcParams['animation.bitrate'],
1326+
fps=1000 / self._interval)
13261327
self.save(str(path), writer=writer)
13271328
# Now open and base64 encode.
13281329
vid64 = base64.encodebytes(path.read_bytes())
@@ -1401,10 +1402,19 @@ def to_jshtml(self, fps=None, embed_frames=True, default_mode=None):
14011402
def _repr_html_(self):
14021403
"""IPython display hook for rendering."""
14031404
fmt = mpl.rcParams['animation.html']
1405+
if fmt == 'none':
1406+
return
1407+
kwargs = {}
1408+
if fmt == 'auto':
1409+
if writers.is_available('ffmpeg'):
1410+
fmt = 'html5'
1411+
kwargs['_writer_cls'] = writers['ffmpeg']
1412+
else:
1413+
fmt = 'jshtml'
14041414
if fmt == 'html5':
1405-
return self.to_html5_video()
1415+
return self.to_html5_video(**kwargs)
14061416
elif fmt == 'jshtml':
1407-
return self.to_jshtml()
1417+
return self.to_jshtml(**kwargs)
14081418

14091419
def pause(self):
14101420
"""Pause the animation."""

‎lib/matplotlib/mpl-data/matplotlibrc‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,10 +823,11 @@
823823
## ***************************************************************************
824824
## * ANIMATION *
825825
## ***************************************************************************
826-
#animation.html: none # How to display the animation as HTML in
827-
# the IPython notebook:
828-
# - 'html5' uses HTML5 video tag
829-
# - 'jshtml' creates a JavaScript animation
826+
#animation.html: auto # How to display the animation as HTML in the IPython notebook:
827+
# - 'none' displays nothing
828+
# - 'html5' uses HTML5 video tag
829+
# - 'jshtml' creates a JavaScript animation
830+
# - 'auto' picks the best available option
830831
#animation.writer: ffmpeg # MovieWriter 'backend' to use
831832
#animation.codec: h264 # Codec to use for writing movie
832833
#animation.bitrate: -1 # Controls size/quality trade-off for movie.

‎lib/matplotlib/rcsetup.py‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,7 @@ def _convert_validator_spec(key, conv):
14481448
"keymap.copy": validate_stringlist,
14491449

14501450
# Animation settings
1451-
"animation.html": ["html5", "jshtml", "none"],
1451+
"animation.html": ["auto", "html5", "jshtml", "none"],
14521452
# Limit, in MB, of size of base64 encoded animation in HTML
14531453
# (i.e. IPython notebook)
14541454
"animation.embed_limit": validate_float,
@@ -3833,13 +3833,14 @@ class _Subsection:
38333833
_Section("Animation"),
38343834
_Param(
38353835
"animation.html",
3836-
default="none",
3837-
type=Literal["html5", "jshtml", "none"],
3838-
validator=["html5", "jshtml", "none"],
3839-
description="How to display animations as HTML in IPython notebooks: "
3840-
"'html5' uses the HTML5 video tag, "
3841-
"'jshtml' creates a JavaScript animation, and "
3842-
"'none' disables HTML output."
3836+
default="auto",
3837+
type=Literal["auto", "html5", "jshtml", "none"],
3838+
validator=["auto", "html5", "jshtml", "none"],
3839+
description="How to display animations as HTML in IPython notebooks:\n"
3840+
"- 'auto' picks the best available option\n"
3841+
"- 'html5' uses HTML5 video tag\n"
3842+
"- 'jshtml' creates a JavaScript animation\n"
3843+
"- 'none' displays nothing"
38433844
),
38443845
_Param(
38453846
"animation.writer",

‎lib/matplotlib/tests/test_animation.py‎

Lines changed: 16 additions & 0 deletions

0 commit comments

Comments
 (0)