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

Automatically import Plugins #967

Merged
merged 15 commits into from
Jan 28, 2021
Merged

Conversation

naveen521kk
Copy link
Member

@naveen521kk naveen521kk commented Jan 26, 2021

Motivation

Specify plugins using CLI and config file
This was discussed on Discord

  • Automatically import plugins so that they that users don't need to do them.
  • Specifying plugins using config files as well as CLI.
  • Slight changes in the entry point procedure

Overview / Explanation for Changes

With manim_plugintemplate it can be used as

from manim import *
class Plugin(Scene):
    def construct(self):
        dot=DotGrid()
        self.play(FadeIn(dot))

previously this needs manual import of manim_plugintemplate

Oneline Summary of Changes

- Automatically Import Plugins (:pr:`967`)

Testing Status

Locally worked with a slight modification to plugin_template. Will make a PR there also. See ManimCommunity/manim-plugintemplate#6

cc @jsonvillanueva

Acknowledgements

Reviewer Checklist

  • Newly added functions/classes are either private or have a docstring
  • Newly added functions/classes have tests added and (optional) examples in the docs
  • Newly added documentation builds, looks correctly formatted, and adds no additional build warnings
  • The oneline summary has been included in the wiki

Specify plugins using CLI and config file

Signed-off-by: Naveen M K <[email protected]>
Copy link
Member

@friedkeenan friedkeenan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very knowledgeable about how plugins work and all that, so please bear with me if that shows overmuch. Generally I don't think I'm on board with plugins sharing manim's namespace; I think their own namespaces should be preserved somehow, but if that's not possible then I can live with that.

manim/_config/utils.py Outdated Show resolved Hide resolved
manim/plugins/import_plugins.py Outdated Show resolved Hide resolved
# it is a module so it can't be called
# see if __all__ is defined
# if it is defined use that to load all the modules necessary
# essentially this would be similar to `from plugin import *``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the plugins' namespaces have to be eradicated? Because I'd rather they be preserved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like that to happen :)
Because users can also import plugins manually if there want its namespace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the result of the discussion on Discord was that we should use a config file to determine the default plugins that are imported/used (as well as manually using --plugins to specify which plugins to use/import in the entry point)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm just not a fan of the possible namespace thrashing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind the potential namespace thrashing (some plugins like the manim-onlinetex @Aathish04 rely on this); however, I do mind the syntax highlighting issue this presents and I think others would too -- which is why I would opt for CLI to change the plugins/__init__.py file which wouldn't hinder syntax highlighting

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for CLI to change the plugins/init.py file which wouldn't hinder syntax highlighting

That is bad practice. Some package managers checksum the files it installs and doing so will break it. And other times things installed from the package manager isn't usually writable and it would need to run in sudo mode which may create unnecessary problems.

manim/plugins/import_plugins.py Outdated Show resolved Hide resolved
manim/plugins/import_plugins.py Outdated Show resolved Hide resolved
manim/plugins/import_plugins.py Outdated Show resolved Hide resolved
# if not just import the module with the plugin name
if hasattr(loaded_plugin, "__all__"):
for thing in loaded_plugin.__all__:
exec(f"{thing}=loaded_plugin.{thing}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried searching for a cleaner way to set local variables with dynamic names, unfortunately could not find one, and exec seems to be the cleanest way. I'm not sure of the purpose of this review comment, I guess a general lamentation and a note to any others who might know of a cleaner way. Writing to locals(), vars(), or globals() is explicitly discouraged by the python docs, so that's not an option. I'm not sure if maybe preserving plugins' namespaces would make this unnecessary?

@naveen521kk
Copy link
Member Author

I will add a test for these soon.

@naveen521kk
Copy link
Member Author

This is ready with a Test :)



def cfg_file_create(cfg_file_contents, path):
file_loc = (path / "manim.cfg").absolute()
Copy link
Member

@friedkeenan friedkeenan Jan 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally not too big a fan of the / operator on paths, I like to make it more explicit like Path(path, "manim.cfg") or you could use the .joinpath method, but the way it currently is is still fine.

I'd point out the other places but I accidentally hit "Add single comment" on my first comment and now feel it might be weird to start a whole new separate review but also don't want to spam this so yeah

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this way though...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean yeah fair enough I'm just not too big a fan of it, I'm not at all staunchly against the / operator here. Feel perfectly free to not change anything unless maybe someone else wants to throw in an opinion I guess

Copy link
Member

@jsonvillanueva jsonvillanueva Jan 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the library seems to use
Path(your_file_name) / "rest" / "of" / "path"

So I would just go for consistency here and do whatever is done mostly elsewhere in the codebase. Although, in general we should just have one style for these sorts of things.

Edit: This might not be mostly done elsewhere, but it is another way I've seen this done in the library.
In particular see: manim_directive.py, utils.py, sounds.py, make_and_open_docs.py, test_copy.py, conftest.py, and text_mobject.py
In other files, it seems to use @naveen521kk preferred syntax: test_img_and_svg.py and test_cli_flags.py.

@jsonvillanueva
Copy link
Member

jsonvillanueva commented Jan 28, 2021

Just tested and it works:
image

However, keep in mind that using this feature will break syntax highlighting in the VS Code editor. If syntax highlighting is necessary, it's better to instead have this file (which defeats the purpose of this PR):

from manim import *
from manim_plugintemplate.mobjects.dotgrid import DotGrid

class MyScene(Scene):
    def construct(self):
        grid = DotGrid()
        self.add(grid)
        self.wait()

@naveen521kk naveen521kk requested a review from leotrs January 28, 2021 14:33
tests/test_plugins/simple_scenes.py Show resolved Hide resolved
tests/test_plugins/test_plugins.py Show resolved Hide resolved
manim/_config/utils.py Outdated Show resolved Hide resolved
manim/_config/utils.py Outdated Show resolved Hide resolved
@naveen521kk naveen521kk merged commit 65240b8 into ManimCommunity:master Jan 28, 2021
@naveen521kk naveen521kk deleted the plugins branch January 28, 2021 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants