A community-maintained Python framework for creating mathematical animations.
Added ``media_embed`` config option to control whether media in Jupyter notebooks is embedded (#2442)
Michael McNeil Forbes99b11da9c52a
diff --git a/example_scenes/manim_jupyter_example.ipynb b/example_scenes/manim_jupyter_example.ipynbindex 45cde4834..4daae076e 100644--- a/example_scenes/manim_jupyter_example.ipynb+++ b/example_scenes/manim_jupyter_example.ipynb@@ -32,6 +32,8 @@"\n","# set the maximum width for video outputs to a predefined value\n","config.media_width = \"20vw\"\n",+ "# embed video\n",+ "config.media_embed = True\n","\n","class HelloManim(Scene):\n"," def construct(self):\n",diff --git a/manim/_config/default.cfg b/manim/_config/default.cfgindex f80c07707..eb48df5cd 100644--- a/manim/_config/default.cfg+++ b/manim/_config/default.cfg@@ -202,4 +202,5 @@ repr_number = greenloglevel = ERROR[jupyter]+media_embed =media_width = 60%%diff --git a/manim/_config/utils.py b/manim/_config/utils.pyindex a284d8b4b..cb5533507 100644--- a/manim/_config/utils.py+++ b/manim/_config/utils.py@@ -253,6 +253,7 @@ class ManimConfig(MutableMapping):"from_animation_number","images_dir","input_file",+ "media_embed","media_width","webgl_renderer_path","log_dir",@@ -627,6 +628,12 @@ class ManimConfig(MutableMapping):if val:self.ffmpeg_loglevel = val+ try:+ val = parser["jupyter"].getboolean("media_embed")+ except ValueError:+ val = None+ setattr(self, "media_embed", val)+val = parser["jupyter"].get("media_width")if val:setattr(self, "media_width", val)@@ -971,6 +978,12 @@ class ManimConfig(MutableMapping):doc="Verbosity level of ffmpeg (no flag).",)+ media_embed = property(+ lambda self: self._d["media_embed"],+ lambda self, val: self._d.__setitem__("media_embed", val),+ doc="Embed videos in Jupyter notebook",+ )+media_width = property(lambda self: self._d["media_width"],lambda self, val: self._d.__setitem__("media_width", val),diff --git a/manim/utils/ipython_magic.py b/manim/utils/ipython_magic.pyindex c89d32f08..82b5a5398 100644--- a/manim/utils/ipython_magic.py+++ b/manim/utils/ipython_magic.py@@ -78,6 +78,14 @@ else:which is 25% of your current viewport width. To allow the output to become as largeas possible, set ``config.media_width = "100%"``.+ The ``media_embed`` option will embed the image/video output in the notebook. This is+ generally undesirable as it makes the notebooks very large, but is required on some+ platforms (notably Google's CoLab, where it is automatically enabled unless suppressed+ by ``config.embed = False``) and needed in cases when the notebook (or converted HTML+ file) will be moved relative to the video locations. Use-cases include building+ documentation with Sphinx and JupyterBook. See also the :mod:`manim directive for Sphinx+ <manim.utils.docbuild.manim_directive>`.+Examples--------@@ -88,6 +96,7 @@ else:%%manim -v WARNING --disable_caching -qm BannerExampleconfig.media_width = "75%"+ config.media_embed = Trueclass BannerExample(Scene):def construct(self):@@ -112,6 +121,7 @@ else:if not len(args) or "-h" in args or "--help" in args or "--version" in args:main(args, standalone_mode=False, prog_name="manim")return+modified_args = self.add_additional_args(args)args = main(modified_args, standalone_mode=False, prog_name="manim")with tempconfig(local_ns.get("config", {})):@@ -163,20 +173,23 @@ else:shutil.copy(local_path, tmpfile)file_type = mimetypes.guess_type(config["output_file"])[0]- if file_type.startswith("image"):- display(Image(filename=config["output_file"]))- return+ embed = config["media_embed"]+ if embed is None:+ # videos need to be embedded when running in google colab.+ # do this automatically in case config.media_embed has not been+ # set explicitly.+ embed = "google.colab" in str(get_ipython())- # videos need to be embedded when running in google colab- video_embed = "google.colab" in str(get_ipython())-- display(- Video(+ if file_type.startswith("image"):+ result = Image(filename=config["output_file"], embed=embed)+ else:+ result = Video(tmpfile,html_attributes=f'controls autoplay loop style="max-width: {config["media_width"]};"',- embed=video_embed,- ),- )+ embed=embed,+ )++ display(result)def add_additional_args(self, args: list[str]) -> list[str]:additional_args = ["--jupyter"]