Skip to content
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

ipython_magic.py essential changes: #3628

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

lambdadotjoburg
Copy link

@lambdadotjoburg lambdadotjoburg commented Feb 20, 2024

  • Changes made to existing %manim cell magic that previously ignored config.media_embed settings after manim refactoring

  • Additional feature for %manim --quick-setup (--accept-default) line magic added to ipython_magic.py

Overview: What does this pull request change?

The current state of manim's ipython_magic.py cell magic command ignores whether or not the config switch config.media_embed is set to either true or false and embeds the media into the .ipynb notebook in any case. Whether or not the media_embed attribute is set to true or false is irrelevant to the current state of ipython_magic.py and does not respect the encoded boolean choice, thus loading the media into the .ipynb view, regardless of the user's choice media_embed = true/false. This is due to recent changes of the media_embed config option, which originally could be a None type, but is now required to be a boolean type.

Motivation and Explanation: Why and how do your changes improve the library?

The proposed changes takes care of the persisting problem that media_embed config attribute is responsible for updating the .ipynb view by simply adding the check at lines 238-239:

if embed is True: 
        display(result)

Some extras have been included which can be seen as a gif or as an .ipynb notebook file

  1. auto-populate first .ipynb cell with from manim import *
  2. new/extra custom ipython line magic for getting ipython notebook/manim users set up faster with a working project demo using %manim --quick-setup which provides a "GUI"/front-end prompt-like command-line/terminal-style project config/setup "REPL" to allow users to specify the scene options (background color, media_width etc..), project settings (project name, media directory, including video, images, tex, text, etc ...) and other scene & config options such as whether to import manimpango or not.
  3. If manim --quick-setup is completed, then the cell is populated with a demo manim screen resolution scene upon completion of prompt input, onto which the config settings from step 2 is applied and the line magic is auto-commented # %manim --quick-setup
  4. Alternatively, using the magic %manim --quick-setup --accept-default takes all the pre-defined/default config settings into account when creating a new manim project via the ipython notebook front-end and populates the .ipynb cell with the code for the demo manim screen resolution scene and auto-comments the line magic # %manim --quick-setup --accept-default

The code for the default scene is pulled from the 'local' file manim/manim/templates

Links to added or changed documentation pages

Further Information and Comments

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

lambdadotjoburg and others added 2 commits February 20, 2024 16:05
Changes made to existing cell magic that ignored config.media_embed changes after manim refactoring

Additional feature for %manim --quick-setup line magic added to ipython_magic.py
manim/utils/ipython_magic.py Fixed Show fixed Hide fixed
Comment on lines +270 to +279
# def load_ipython_extension(ipython):
# """
# Any module file that define a function named `load_ipython_extension`
# can be loaded via `%load_ext module.path` or be configured to be
# autoloaded by IPython at startup time.
# """
# # You can register the class itself without instantiating it.
# # IPython will call the default constructor on it.
# ipython.register_magics(ManimMagic)
# print("Manim Magics class imported")

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
import json
import os
import re
import sys

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'sys' is not used.
replaced inifity  by infinity - codespell/flake CI checks
removed unnecessary comments
changed `input(f"config.verbosity = ")` to `input("config.verbosity = ")`
changed `if not backgroundColor.upper() in vars(manim_colors):`

to 

`if backgroundColor.upper() not in vars(manim_colors):`
changed

`def is_percent(number):
    if "%" in number:
        return True
    else:
        return False
`

and

`def is_percent(number):
    if "%" in number:
        return True
    else:
        return False
`

to

`def is_float(number):
    return "." in number:
`
and 

`def is_percent(number):
    return "%" in number
`
manim/utils/quick_setup.py Fixed Show fixed Hide fixed
lambdadotjoburg and others added 9 commits February 20, 2024 17:33
Oops, forgot to remove : after line 334
Fix for flake8  C408 Unnecessary dict call - rewrite as a literal.
1     C408 Unnecessary dict call - rewrite as a literal.
Changes to dictionary syntax - obv!!!
Replace existing broken config_low_q.gif
Remove to replace broken file
gif illustrating use of %manim --quick-setup line magic
@MrDiver
Copy link
Collaborator

MrDiver commented Apr 1, 2024

Would you like to elaborate on your changes and why you did them?
It is helpful for us to understand why changes are made and not just what was changed.

Please note, that if a PR is not ready for review you can mark it as draft.

Thanks for participating in contributing to Manim ✨

@MrDiver MrDiver marked this pull request as draft April 1, 2024 02:48
@lambdadotjoburg lambdadotjoburg marked this pull request as ready for review April 1, 2024 09:23
@lambdadotjoburg
Copy link
Author

lambdadotjoburg commented Apr 1, 2024

@MrDiver Please see the updated markdown

@behackl
Copy link
Member

behackl commented Apr 22, 2024

Part of this PR seems to be based on a misunderstanding (caused by the media_embed config option not being documented particularly well).

The media_embed option isn't meant to control whether or not generated media is displayed in a notebook. It controls the way how exactly rendered videos are being displayed.

If config.media_embed is set to False, the HTML tag used to display the rendered video is, roughly, <video src="media/jupyter/..." ...>...</video>. Otherwise, if config.media_embed is True, the reference to an external file is avoided and the video is fully included in binary (base64-encoded) form in the video tag. For longer videos or ones with higher resolution, this substantially increases the file size of the HTML file (which is best avoided to keep the browser responsive, in general).

In the special case of config.media_embed is None (the default) we check whether the notebook has been spawned on Google Colab -- in which case media has to (or at least had to, at the time of implementation) be embedded directly to the notebook, so it acts as if the option was set to True. Otherwise, it behaves just like in the False case.

The documentation of this config option should be made more clear to reflect this.

The quick-setup utility proposed here then is again something completely different. We actually do have something like it already with our manim init CLI utility. It feels a bit weird to start a new project from an existing notebook though, and it might make more sense to put more effort into making manim init more useful.

Can you explain the usecase for the quickstart via the %%manim-magic a bit more, or share your thoughts on where manim init is lacking in comparison?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

Successfully merging this pull request may close these issues.

None yet

3 participants