manim icon indicating copy to clipboard operation
manim copied to clipboard

Feature request: Autoreload for the same file

Open Splines opened this issue 2 months ago • 1 comments

In the last few weeks, 3b1b and me introduced these features to ease reloading:

  • reload() command via #2240
  • --autoreload CLI flag via #2268

The reload(x) command is essentially like quitting Manim altogether then starting it again with the -se option set to line x, but without the hassle of doing this manually and waiting for the complete window to be restarted. Instead, we reuse the window. Note that only your user-defined modules will be reloaded, no external libraries like numpy, no Python in-built modules like sys or time and also no manimlib files will be reloaded (the latter can be adjusted via a config option though).

The --autoreload command line flag will activate the autoreload IPython magic command. With this, your imported modules will be automatically reloaded for you such that you don't even have to execute reload() anymore.


The only missing piece (in my opinion), is a feature that enables auto-reloading also for classes/objects/etc. in the same file (and not only for imported files). E.g. consider this example

from manimlib import *


class ClassInSameFile:
    def answer(self):
        return "Hello from class in same file"


class AScene(Scene):
    def construct(self):
        ## Starting
        print("Hello from A")

        ## Class in same file
        test = ClassInSameFile()
        print(test.answer())

The autoreload feature from IPython will not pick up on changes to the string "Hello from class in same file" . If you do a reload(14) and then the checkpoint_paste(), it will work fine, but for the lazy programmer, that is one reload(14) too much 😅 A current workaround is to outsource ClassInSameFile to another file. Then import that into the original file and autoreload will do its magic.

Note that even if autoreload was perfect in the above sense, reload() might still have its raison d'être in the case where autoreloading might not work (due to whatever reason, I'm sure there might exist some...).

Splines avatar Dec 12 '24 23:12 Splines