Closed qo4on closed 4 years ago
import sys
and import random
do the same.
One solution would be to search through the global objects, collect modules, assemble import code and then add it to the code passed to manim. The first two steps are easy:
from types import ModuleType
@magics_class
class ManimMagics(Magics):
# ....
def extract_imports(self):
frame = find_ipython_frame(inspect.stack())
if not frame:
raise Exception('Could not find IPython frame')
globals_dict = frame[0].f_globals
modules = {
name: obj
for name, obj in globals_dict.items()
if (not name.startswith('_')) and isinstance(obj, ModuleType)
}
return '\n'.join(
assemble_import(module) for module in modules
)
but writing the assemble_import
function is difficult. It should work like this:
import statistics
from os import path
import os
sys = os.sys
import abc as xyz
assert assemble_import(statistics) = 'import statistics'
assert assemble_import(path) = 'from os import path'
assert assemble_import(sys) = 'from os import sys'
assert assemble_import(xyz) = 'import abc as xyz'
All I had time for this weekend, maybe someone else will have more ideas/time to send pick up from there and send a PR - help wanted!
Posted a question on the topic here: https://stackoverflow.com/questions/61049832/is-it-possible-to-generate-import-statement-from-the-module-file-in-python
This code works:
!python3 -m manim example_scenes.py WarpSquare -pl
This code works also in jupyter:
But when
import numpy as np
was written in the cell before and already executed the code without this line can't find numpy:Why doesn't the cell with magic
%%manim
find already imported libraries?