-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New Feature: IPython %%manim magic #943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks a lot for this!
+1 for this |
The tests are currently failing because The "easy" solution would be to add IPython as a dependency of Manim (but I'm not sure that this is necessary). I'm also sure it is possible to tell pytest to ignore the file. And the third option would be to change the code in I probably have a slight preference for telling pytest to skip the file, but I'd have to find out how this can be done. Any other opinions? |
+1 for this idea. Another thing:
Some further thoughts on that:
|
Extensions are welcome, although I'd suggest to move them to follow-up PRs. The current implementation caps the width of the video frame to 100% of the corresponding container; larger videos are scaled down to fit in. But besides that, the rendered resolution should be displayed. |
Nice! |
Regarding caching: I've played around for quite some time now, and I fear that I don't really have a good solution for it. (The problem, which I realize @kolibril13 only reported to me privately: sometimes videos don't update after rendering a cell again.) I've found that the issue is mainly resolved by switching from classic Jupyter notebooks to JupyterLab (at least after a minor modification of the code; I'll push that later) -- which is what I will suggest in the documentation. |
This is ready for being reviewed. I'd like to propose that we move implementation of further features and refinements of this interface to follow-up PRs, if you are happy with the current basic functionality. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me as expected in jupyter lab.
Jupyter notebooks have an issue with updating the videos (chrome and firefox), however, this can be addressed in a future pr (I'll have a look on that in a few days).
I've added one additional commit, 0dbd067 -- this is a suggestion how the caching problem could be solved, at the cost of (intelligently managed) video files in a @kolibril13, could you please test whether this resolves the problem with caching for both JupyterLab and classic Jupyter notebooks? (For me it works, but I'd like to make sure.) (If it doesn't work as intended, we can simply revert the commit.) |
Works for me as well! :) Thanks for your efforts! Interesting timing test:
from manim import * %%time
%%manim Example -m -p -v WARNING --disable_caching
class Example(Scene):
def construct(self):
def phi_func(t):
phi = phi_max * np.sin(np.sqrt(g / l) * t) - 90*DEGREES
return phi
self.camera.background_color = BLACK
phi_max = 45 * DEGREES
g = 9.81
l = 1
l_dis = 3
t = ValueTracker(0)
coordinate = [l_dis * np.cos(phi_func(t.get_value())),
l_dis * np.sin(phi_func(t.get_value())),
0]
anchor = Dot()
mass = LabeledDot(label= "1kg", point=coordinate).set_z(10)
mass.add_updater(lambda x : x.move_to([l_dis * np.cos(phi_func(t.get_value())),
l_dis * np.sin(phi_func(t.get_value())),
0]))
line = Line(anchor.get_center(), mass.get_center() , color= BLUE)
line.add_updater(lambda x : x.become(Line(anchor.get_center(), mass.get_center(),color= BLUE)))
self.add(anchor,mass, line, mass)
self.play(t.animate.set_value(2),rate_func=linear , run_time=3) import sys
sys.executable import manim.utils.ipython_magic
manim.utils.ipython_magic.__file__ |
There don't seem to be further discussions regarding this PR, so I'd like to merge this tomorrow morning (in, like, 6-7h) or so. In particular, I'd like to move this forward in order to have some time before the next release for preparing a follow-up PR for
Further comments and/or input is -- of course, as always -- very welcome! |
Motivation
See #895.
Overview / Explanation for Changes
This PR introduces a
manim
magic (works both as a line and as a cell magic). The logic for parsing the passed arguments is (up to a minor modification) using the same, already existing functions that our CLI uses. (Consistency between the interfaces is thus ensured.)This is how it currently looks like:
Steps to try it out locally:
[poetry run] pip install notebook
.[poetry run] jupyter notebook
), and open a (new) worksheet (with a Python 3 kernel).import manim
in some way (from manim import *
is not required, no further function call is required; the necessary code to setup the%%manim
magic is run when the library is imported).gh pr checkout 943
)conda install -c anaconda ipykernel
python -m ipykernel install --user --name=manim-env
were you replace manim-env with the name of your environment.Oneline Summary of Changes
Testing Status
I am not sure whether there is a simple way of adding (unit) tests for this interface.
Further Comments
I am very happy that this implementation is as simple as it is; most of it is due to our much improved config system. There are some details with this implementation that I don't really like (but I could not find a way to fix these issues):
tqdm
(our progress bar) prints tostderr
, which causes the red box underneath the code cell. (While rendering, the progress bar will be visible there -- but in the end, when it disappears, the box unfortunately does not disappear with it.)WARNING
in the screenshot above (BTW: we should downgrade thecreating dummy animation
message toINFO
or evenDEBUG
)). What I would have liked was, if the logging output didn't appear there, but in a separate, less intrusive place -- try runningfrom IPython.core.page import page; page("this is where I would have liked the log to show")
in a notebook for my suggestion, but I could not manage to move the log there (and I've tried for quite a bit; but I'm also not a IPython expert).ManimMagic
class), but I do think that if you are satisfied with this sort of interface, we should reference it prominently in our documentation. (Suggestions for where?)Closes #895.
Acknowledgements
Reviewer Checklist