Using Docker to run Manim in Jupyter notebooks
If you want to develop Manim animations easily the a Jupyter notebook makes as great working environment. However it can be fiddly to get set up.
Luckily there is a pre-built Docker image that can get you up and running with two simple commands:
cd <your working directory>
docker run --rm -it -p 8888:8888 -v "%cd%:/manim" manimcommunity/manim jupyter lab --ip=0.0.0.0
In the console you will then see a URL looking something like http://127.0.0.1:8888/?token=xxxxxxx
. Cut and paste this into your browser and you are away laughing.
For Linux you will need to tweak the %cd%
bit and use pwd
instead so:
cd <your working directory>
docker run --rm -it -p 8888:8888 -v "$(pwd):/manim" manimcommunity/manim jupyter lab --ip=0.0.0.0
These commands mount the current directory into the docker container, so any notebooks or videos you create will be saved into that directory and won’t be lost when you stop the container.
Finally to create an animation put the following code into two separate cells in a new notebook:
from manim import *
and
%%manim SquareToCircle
class SquareToCircle(Scene):
def construct(self):
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set color and transparency
square = Square() # create a square
square.rotate(PI / 4) # rotate a certain amount
self.play(Create(square)) # animate the creation of the square
self.play(Transform(square, circle)) # interpolate the square into the circle
self.play(FadeOut(square)) # fade out animation
When you run the notebook you will see the rendered animation (and it will be saved into a media
sub-directory). The %%manim
line is the magic instruction to trigger Manim to render the specified scene.