From 00b34f202044dd841c96c4bd60322d849226c8d7 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Thu, 12 Dec 2024 08:52:03 -0600 Subject: [PATCH] Autoreload v2 (#2268) * Add autoreload * Typo correction * Add --autoreload to configuration docts Co-Authored-By: Splines <37160523+Splines@users.noreply.github.com> --------- Co-authored-by: Splines <37160523+Splines@users.noreply.github.com> --- docs/source/getting_started/configuration.rst | 1 + manimlib/config.py | 12 ++++++++++++ manimlib/default_config.yml | 1 + manimlib/scene/scene_embed.py | 7 +++++++ 4 files changed, 21 insertions(+) diff --git a/docs/source/getting_started/configuration.rst b/docs/source/getting_started/configuration.rst index 89bcdaed..47e1a2e8 100644 --- a/docs/source/getting_started/configuration.rst +++ b/docs/source/getting_started/configuration.rst @@ -63,6 +63,7 @@ flag abbr function ``--video_dir VIDEO_DIR`` Directory to write video ``--config_file CONFIG_FILE`` Path to the custom configuration file ``--log-level LOG_LEVEL`` Level of messages to Display, can be DEBUG / INFO / WARNING / ERROR / CRITICAL +``--autoreload`` Automatically reload Python modules to pick up code changes across different files ========================================================== ====== ===================================================================================================================================================================================================== custom_config diff --git a/manimlib/config.py b/manimlib/config.py index 29af8f21..e91581d6 100644 --- a/manimlib/config.py +++ b/manimlib/config.py @@ -45,6 +45,7 @@ def initialize_manim_config() -> Dict: update_file_writer_config(config, args) update_scene_config(config, args) update_run_config(config, args) + update_embed_config(config, args) return Dict(config) @@ -209,6 +210,12 @@ def parse_cli(): "--log-level", help="Level of messages to Display, can be DEBUG / INFO / WARNING / ERROR / CRITICAL" ) + parser.add_argument( + "--autoreload", + action="store_true", + help="Automatically reload Python modules to pick up code changes " + + "across different files", + ) args = parser.parse_args() args.write_file = any([args.write_file, args.open, args.finder]) return args @@ -311,6 +318,11 @@ def update_run_config(config: dict, args: Namespace): ) +def update_embed_config(config: dict, args: Namespace): + if args.autoreload: + config["embed"]["autoreload"] = True + + # Helpers for the functions above diff --git a/manimlib/default_config.yml b/manimlib/default_config.yml index 9e340304..fe6cfd05 100644 --- a/manimlib/default_config.yml +++ b/manimlib/default_config.yml @@ -83,6 +83,7 @@ text: alignment: "LEFT" embed: exception_mode: "Verbose" + autoreload: False resolution_options: # When the user passes in -l, -m, --hd or --uhd, these are the corresponding # resolutions diff --git a/manimlib/scene/scene_embed.py b/manimlib/scene/scene_embed.py index 139cd205..4dad577a 100644 --- a/manimlib/scene/scene_embed.py +++ b/manimlib/scene/scene_embed.py @@ -29,6 +29,8 @@ class InteractiveSceneEmbed: self.enable_gui() self.ensure_frame_update_post_cell() self.ensure_flash_on_error() + if manim_config.embed.autoreload: + self.auto_reload() def launch(self): self.shell() @@ -139,6 +141,11 @@ class InteractiveSceneEmbed: print("Reloading...") self.shell.run_line_magic("exit_raise", "") + def auto_reload(self): + """Enables IPython autoreload for automatic reloading of modules.""" + self.shell.magic("load_ext autoreload") + self.shell.magic("autoreload all") + def checkpoint_paste( self, skip: bool = False,