Code Monkey home page Code Monkey logo

jupyter-manim's Introduction

jupyter-manim

Build Status codecov MIT License Binder Colab

Integrates 3b1b's ManimCairo (cairo-backend branch) with Jupyter displaying the resulting video when using %%manim cell magic to wrap a scene definition.

WARNING: This library only works for ManimCairo (the cairo-backend branch in 3b1b's version). It does not work for ManimCE (which already has Jupyter support by default) or ManimGL (which does not support Jupyter at all, as of time of writing).

Quick preview

The code in the example above comes from the excellent manim tutorial.

Run a live demo in your browser by clicking here.

Installation

pip3 install jupyter-manim

Usage

To enable the manim magic please run import jupyter_manim first. Then, you can use the magic as if it was the manim command: your arguments will be passed to manim, exactly as if these were command line options.

For example, to render scene defined with class Shapes(Scene) use

%%manim Shapes
from manimlib.scene.scene import Scene
from manimlib.mobject.geometry import Circle
from manimlib.animation.creation import ShowCreation

class Shapes(Scene):

    def construct(self):
        circle = Circle()
        self.play(ShowCreation(circle))

Since version 1.0, the code is no longer required to be self-contained - jupyter_manim will attempt to export your variables (and imported objects) from the notebook into the manim script.

Most variables can be easily exported, however there are limitations; in short everything which can be pickled can be exported. Additionally, variables whose names start with an underscore will be ommited.

To display manim help and options use:

%%manim -h
pass

The %%manim magic (by default) hides the progress bars as well as other logging messages generated by manim. You can disable this behaviour using --verbose flag

In the latest version of manimlib you can import everything at once using:

from manimlib.imports import *

Video player control options

  • --no-controls - hides the controls
  • --no-autoplay - disables the autoplay feature
  • -r or --resolution - control the height and width of the video player; this option is shared with manim and requires the resolution in following format: height,width, e.g. %%manim Shapes -r 200,1000
  • --base64 send the video with a data: URL instead of a local path - useful for remote notebooks like Google Colab, or to embed the video in notebook (note: the notebook size may increase rapidly)

Compatibility and testing

This package is continuously tested with Python 3.7 on Ubuntu, Mac OS an Windows.

Tests have to be run with ipython, as the magic relies on IPython instance being available:

python3 setup.py install
ipython -m pytest -- --cov=.

jupyter-manim's People

Contributors

cjds avatar darylgolden avatar krassowski avatar remisphere avatar scoder12 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jupyter-manim's Issues

jupyter-manim and ManimCommunity

Hi @krassowski,
At the moment, some people (including me) are hardly working on making manim more user-friendly in the manim-community repo: https://github.com/ManimCommunity/manim

We are actively developing new versions which are quite good by now, and we are also thinking about improving the jupyter notebook support.
Therefore I have an unusual question:
How would you feel to transfer the jupyter-manim repo to the manim-community Github Organization?
We would then try to enhance some features, and even maybe add a new java-script renderer.
Self-explanatory we would give you credits and that you created this repo first place, and keep a MIT license.

Problems with Windows

First of all, I want to congratulate you for your incredible work, it works great on Mac, but I have problems with Windows.
Mac:
Captura de pantalla 2019-05-25 a la(s) 03 15 32

I'm not an advanced user of jupyter, do you think this can be solved if I install anaconda?
Windows:

jupyter

Thank you!

adding ä,ö,ü,etc

How do i add additional letters. I think i have to change the tex.template.tex. but it doesnt work

Feature request for exporting the current namespace

Hi, thanks for the great job for bring manim to jupyter!

I'm just wondering if we can have the feature as written in the readme?

In future, an option to export the current namespace (or specific variables) will be added. It could be implemented by pickling the Python locals and globals and then pre-pending the cell with an un-pickling script

It will be very helpful!

Manim gifs not showing in builded jupyter book

Describe the problem

I'm pretty new to the Jupyter Notebook/Book universe, so I'm sorry if this question seems silly.

I'm trying to integrate Manim animations into my Jupyter Book rendering my animations as gifs. But somehow I don't get an output when I open my build in Chrome. I just get the message:

<IPython.core.display.Image object>

I've tried to dig into different posts but nothing really seems to work. The funny thing is when I open my .ipynb file in jupyter-notebook and run my code-snip there is no problem. The problem occurs when I build my Jupyter Book.

This is the following code I'm able to execute in jupyter-notebook but somehow not when building the Jupyter Book:

`from manim import *

%%manim -v WARNING --progress_bar None -r 400,200 --format=gif --disable_caching HelloManim
class HelloManim(Scene):
def construct(self):
self.camera.background_color = "#ece6e2"
banner_large = ManimBanner(dark_theme=False).scale(0.7)
self.play(banner_large.create())
self.play(banner_large.expand())`

Has anyone else tried this and knows why this issue occur?

Steps to reproduce

  1. Construct new Jupyter notebook-file
  2. Paste the code provided into a new code cell
  3. Execute the code cell (it's working, right?)
  4. Now build a Jupyter book with this notebook file (does it work for you?)

On google colab, blob the files instead of base64coding them.

The problem is that base64coding videos will make the webpage slower and consume way more memory, but there is a better way, blobing the files.
Here is a google colab showing it off https://colab.research.google.com/drive/1FF6AHXu0L6nQw-LF8LymRotfcKfBI5Vh?usp=sharing
if you see the url of the video, you will see that it is a blob.
https://stackoverflow.com/questions/61982233/how-to-get-url-from-google-collab-file-inside-the-workspace/61984094#61984094
A SE question explaining the issue too.

Alternatively, allow a callback function for the url of the file once is completed, the callback will contain the custom code.

Improve support for imports

This code works:
!python3 -m manim example_scenes.py WarpSquare -pl

This code works also in jupyter:

%%manim Shapes --base64 --low_quality
import numpy as np
class WarpSquare(Scene):
    def construct(self):
        square = Square()
        self.play(ApplyPointwiseFunction(
            lambda point: complex_to_R3(np.exp(R3_to_complex(point))),
            square
        ))
        self.wait()

But when
import numpy as np
was written in the cell before and already executed the code without this line can't find numpy:

%%manim Shapes --base64 --low_quality
class WarpSquare(Scene):
    def construct(self):
        square = Square()
        self.play(ApplyPointwiseFunction(
            lambda point: complex_to_R3(np.exp(R3_to_complex(point))),
            square
        ))
        self.wait()

Media will be written to ./media/. You can change this behavior with the --media_dir flag.

/usr/local/lib/python3.6/dist-packages/jupyter_manim/init.py:259: UserWarning: Could not find path in the manim output
warn('Could not find path in the manim output')
/tmp/tmp0wa7hz1o.py:15: UserWarning: Import from notebook: name already in the globals(), skipping
warn('Import from notebook: ' + name + ' already in the globals(), skipping')

Shapes is not in the script

Traceback (most recent call last):
File "/content/manim/manimlib/extract_scene.py", line 155, in main
scene = SceneClass(**scene_kwargs)
File "/content/manim/manimlib/scene/scene.py", line 53, in init
self.construct()
File "/tmp/tmp0wa7hz1o.py", line 25, in construct
square
File "/content/manim/manimlib/scene/scene.py", line 406, in wrapper
func(self, *args, **kwargs)
File "/content/manim/manimlib/scene/scene.py", line 462, in play
self.begin_animations(animations)
File "/content/manim/manimlib/scene/scene.py", line 415, in begin_animations
animation.begin()
File "/content/manim/manimlib/animation/transform.py", line 46, in begin
self.target_mobject = self.create_target()
File "/content/manim/manimlib/animation/transform.py", line 182, in create_target
method.func(target, *args, **method_kwargs)
File "/content/manim/manimlib/mobject/types/vectorized_mobject.py", line 564, in apply_function
Mobject.apply_function(self, function)
File "/content/manim/manimlib/mobject/mobject.py", line 280, in apply_function
**kwargs
File "/content/manim/manimlib/mobject/mobject.py", line 360, in apply_points_function_about_point
mob.points = func(mob.points)
File "/content/manim/manimlib/mobject/mobject.py", line 279, in
lambda points: np.apply_along_axis(function, 1, points),
File "<array_function internals>", line 6, in apply_along_axis
File "/usr/local/lib/python3.6/dist-packages/numpy/lib/shape_base.py", line 379, in apply_along_axis
for ind in inds:
File "/tmp/tmp0wa7hz1o.py", line 24, in
lambda point: complex_to_R3(np.exp(R3_to_complex(point))),
NameError: name 'np' is not defined

Why doesn't the cell with magic %%manim find already imported libraries?

Print statements

Is it possible to see print statements which were defined in def construct(self) next to playback window in Jupyter?

AttributeError: can't set attribute

I see an error in line image = ImageMobject("/content/manim/Pol/1.png"). The same code works perfectly with command line !python3 -m manim example.py mainScene -r 1080,1920.

/tmp/tmpl8hjzawx.py:15: UserWarning: Import from notebook: name already in the globals(), skipping
  warn('Import from notebook: ' + name + ' already in the globals(), skipping')
Traceback (most recent call last):
  File "/content/manim/manimlib/extract_scene.py", line 155, in main
    scene = SceneClass(**scene_kwargs)
  File "/content/manim/manimlib/scene/scene.py", line 53, in __init__
    self.construct()
  File "/tmp/tmpl8hjzawx.py", line 34, in construct
    image = ImageMobject("/content/manim/Pol/1.png")
  File "/content/manim/manimlib/mobject/types/image_mobject.py", line 56, in __init__
    image = Image.open(path).convert(self.image_mode)
  File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 2804, in open
    init()
  File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 2790, in _open_core
  File "/usr/local/lib/python3.6/dist-packages/PIL/ImageFile.py", line 102, in __init__
    self._open()
  File "/usr/local/lib/python3.6/dist-packages/PIL/PngImagePlugin.py", line 579, in _open
    self.size = self.png.im_size
AttributeError: can't set attribute

Adding a loop video option

Hello,
I do not know if that is even possible, but it sure would be great to have a looping option for the video player.

UsageError: Line magic function `%%manim` not found.

I completed the installation:

pip.exe install manimlib
pip.exe install jupyter_manim

when I executed this code:

from manimlib.imports import *
import jupyter_manim
%%manim Shapes

class Shapes(Scene):

    def construct(self):
        circle = Circle()
        self.play(ShowCreation(circle))

The jupyter console displays this error: UsageError: Line magic function %%manim not found.

How do I fix it?

Windows 10

Visual Studio Code

Python 3.9

Embedded player shows duplicates.

If you stop playback and try move frame by frame with arrows on the keyboard you will see duplicate frames. The same file in other player has no duplicates. I'm not sure is this a bug or not, but it's inconvenient.

Manim cell output doesn't play using `<source>` tag in vscode v1.59.1

Description

I don't know if this only happens to me.
Given this example, when I run the code. The video output doesn't play.
Screenshot from 2021-08-31 19-13-22

Reproduce

  1. Install or open vscode v1.59.1 (I don't know if this only happens in this version)
  2. Write some code for manim
  3. Then execute cells

The video file output is present and can be played but in jupyter manim output, it doesn't play. I test by removing source from video tag and adding src attribute in video tag and it works. I'll make a PR for this issue if this issue is valid.

Screenshot from 2021-08-31 19-15-02

Expected behavior

The manim output should play.

Context

  • Operating System and version: Linux Ubuntu 20.04
  • Browser and version: Chrome 92.0.4515.159 (Official Build) (64-bit)
  • JupyterLab version: Not Installed, using jupyter extension from vscode
jupyter core     : 4.7.0
jupyter-notebook : 6.1.6
qtconsole        : 5.0.1
ipython          : 7.19.0
ipykernel        : 5.4.2
jupyter client   : 6.1.7
jupyter lab      : not installed
nbconvert        : 6.0.7
ipywidgets       : 7.6.2
nbformat         : 5.0.8
traitlets        : 5.0.5

Doesn't work with Text object

%%manim Shapes --base64 --low_quality --verbose

class Demo(Scene):
  def construct(self):
    text = Text('Hello, world!')
    self.play(Write(text))

Media will be written to ./media/. You can change this behavior with the --media_dir flag.

/tmp/tmpgj01amj5.py:15: UserWarning: Import from notebook: name already in the globals(), skipping
warn('Import from notebook: ' + name + ' already in the globals(), skipping')

Shapes is not in the script

Traceback (most recent call last):
File "/content/manim/manimlib/extract_scene.py", line 155, in main
scene = SceneClass(**scene_kwargs)
File "/content/manim/manimlib/scene/scene.py", line 53, in init
self.construct()
File "/tmp/tmpgj01amj5.py", line 23, in construct
text = Text('Hello, world!')
TypeError: init() takes 1 positional argument but 2 were given

It works with command line:
!python3 -m manim example_scenes.py Demo -pl

Problems with \sum

When I run this piece of code, I get an unexpected result. It should be writing out \sum_{j=0}^\infty x_j fully then after two seconds adding brackets around it, but it draws half of the left one first and then completes it. In fact, the subscript of x doesn't get written out initially.

%%manim clttest --low_quality

class clttest(Scene):
    def construct(self):
        xj = TexMobject(
        r"""\left[""",
        r"""\sum_{j=0}^\infty x_j""",
        r"""\right]"""
        )
        self.play(Write(xj[1]))
        self.wait(2)
        self.play(Write(xj[0]),
                  Write(xj[2]))

KFITQW5BmB

The problem lies with \sum I assume because running this works just fine

%%manim clttest --low_quality

class clttest(Scene):
    def construct(self):
        xj = TexMobject(
        r"""\left[""",
        r"""x_j""",
        r"""\right]"""
        )
        self.play(Write(xj[1]))
        self.wait(2)
        self.play(Write(xj[0]),
                  Write(xj[2]))

HQMU5rPIyZ

Media-folder placed outside _build/html folder

Hi,

First of all, what a wonderful extension! I'm new to this Jupyter-world, but not so much for manim, but I was wondering how one could integrate manim into a Jupyter-book and not a Jupyter-notebook. Also, the animation should auto-render when building the book. Is this possible?

Thank you in advance :-)

Error in creating tex objects in jupyter_manim

Hi, I'm using jupyter_manim and it has issues in creating texobjects. I have already installed the latex distribution for my Linux operating system, but it's still showing Latex error converting to dvi. Please help me out.

Cannot play video on Colab

Hi, I try runing your git on colab. Then it shows this error. Can you show me how to fix it.
This is what I have on colab

###### cloning manim repo #######
!git clone https://github.com/3b1b/manim.git

###### installing requirements #####
%cd manim
#for fixing pycairo installing error: failed building wheel
!sudo apt install libcairo2-dev pkg-config python3-dev 
!pip3 install -r requirements.txt 

##### installing jupyter_manim #####
!pip3 install jupyter-manim

import jupyter_manim

%%manim Shapes --low_quality

from manimlib.imports import *

class Shapes(Scene):

    def construct(self):
        circle = Circle()
        self.play(ShowCreation(circle))

image

can't build pycario on google colab

Hi, I run pip3 install jupyter_manim, everything is installed successfully but below

Building wheels for collected packages: pycairo
Building wheel for pycairo (PEP 517) ... error
ERROR: Failed building wheel for pycairo
Running setup.py clean for pycairo
Failed to build pycairo
ERROR: Could not build wheels for pycairo which use PEP 517 and cannot be installed directly

I've checked travis.yml, which I guess what "pip3 install jupyter_manim" follow. Google colab provide linux vm so I think it shouldn't install pycario?

Anyway, Should I run command line by line like this

%%manim not found

I properly installed manim and can get the example_scenes.py to render.
I properly installed jupyter manim and conda list shows it:
jupyter-manim 0.11 pypi_0 pypi

I started jupyter notebook from a terminal initiated from within the manim folder
I entered this in the first cell:

%%manim Shapes
from manimlib.scene.scene import Scene
from manimlib.mobject.geometry import Circle
from manimlib.animation.creation import ShowCreation

class Shapes(Scene):

    def construct(self):
        circle = Circle()
        self.play(ShowCreation(circle))

and got
UsageError: Cell magic %%manim not found.

Embedded player lags on playback

Hi! I've been trying jupyter-manim, and it's awesome, so thank you very much for building it!

However, I've noticed the embedded player seems to lag quite a bit on playback, so animations look a bit choppy. The rendering itself works great, and if I download the animation and play the file with Quicktime it also plays perfectly. I'm wondering if this is a known issue, or something going on with my setup. I'm running on a core i9 so I don't think my hardware would be the cause.

Not working on Ubuntu 18.04

I just get the error message


~/anaconda3/lib/python3.7/site-packages/manimlib-0.1.4-py3.7.egg/manimlib/__init__.py in <module>
      1 #!/usr/bin/env python
      2 import manimlib.config
----> 3 import manimlib.extract_scene
      4 import manimlib.stream_starter
      5 

~/anaconda3/lib/python3.7/site-packages/manimlib-0.1.4-py3.7.egg/manimlib/extract_scene.py in <module>
      7 import traceback
      8 
----> 9 from manimlib.scene.scene import Scene
     10 from manimlib.utils.sounds import play_error_sound
     11 from manimlib.utils.sounds import play_finish_sound

~/anaconda3/lib/python3.7/site-packages/manimlib-0.1.4-py3.7.egg/manimlib/scene/scene.py in <module>
      9 from manimlib.animation.creation import Write
     10 from manimlib.animation.transform import MoveToTarget, ApplyMethod
---> 11 from manimlib.camera.camera import Camera
     12 from manimlib.constants import *
     13 from manimlib.container.container import Container

~/anaconda3/lib/python3.7/site-packages/manimlib-0.1.4-py3.7.egg/manimlib/camera/camera.py in <module>
      7 from PIL import Image
      8 from scipy.spatial.distance import pdist
----> 9 import cairo
     10 import numpy as np
     11 

ModuleNotFoundError: No module named 'cairo'

But manim is working properly on my system.
I guess there is some kind of version conflict.
Is this project acutally still in development?
I think the idea of connecting manim with notebooks is beautiful and has a lot of potential!

binder build for jupyter-manim

I was trying to create a binder build for jupyter-manim. I was expecting to execute all custom animation which I create with 3b1b/manim. As it need a lot of installation (for me, I was unable to set up manim in my Ubuntu 20.04), I was thinking to use binder as an alternative. But I got the following error when I use %%manim CustomAnimation:

/home/jovyan/jupyter_manim/__init__.py:302: UserWarning: Could not find path in the manim output
  warn('Could not find path in the manim output')
/tmp/tmpb6smavun.py:39: UserWarning: Import from notebook: name already in the globals(), skipping
  warn('Import from notebook: ' + name + ' already in the globals(), skipping')
Traceback (most recent call last):
  File "/srv/conda/envs/notebook/lib/python3.7/site-packages/manimlib/extract_scene.py", line 155, in main
    scene = SceneClass(**scene_kwargs)
  File "/srv/conda/envs/notebook/lib/python3.7/site-packages/manimlib/scene/scene.py", line 75, in __init__
    self.construct()
  File "/tmp/tmpb6smavun.py", line 94, in construct
    title = TextMobject("Simpsons Rule").scale(2)
  File "/srv/conda/envs/notebook/lib/python3.7/site-packages/manimlib/mobject/svg/tex_mobject.py", line 148, in __init__
    self, self.arg_separator.join(tex_strings), **kwargs
  File "/srv/conda/envs/notebook/lib/python3.7/site-packages/manimlib/mobject/svg/tex_mobject.py", line 44, in __init__
    self.template_tex_file_body
  File "/srv/conda/envs/notebook/lib/python3.7/site-packages/manimlib/utils/tex_file_writing.py", line 21, in tex_to_svg_file
    dvi_file = tex_to_dvi(tex_file)
  File "/srv/conda/envs/notebook/lib/python3.7/site-packages/manimlib/utils/tex_file_writing.py", line 72, in tex_to_dvi
    "See log output above or the log file: %s" % log_file)
Exception: Latex error converting to dvi. See log output above or the log file: /home/jovyan/media/Tex/fa57ceb432d6961f.log

Is there any way to build this as ManimCommunity has one. I have some discussion on this on Reddit

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.