diff options
author | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-03-03 17:14:48 +0300 |
---|---|---|
committer | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-03-03 17:14:48 +0300 |
commit | 96015f265031dff57a3a8b3a70b5c097deff41bb (patch) | |
tree | eceeea82f7b8926e66613a9e27916bfd9f9c0452 /contrib/python/ipython/py3/IPython/core/magic.py | |
parent | 809c8cd1eb0536e15a8b84feb92c8b87042cb81a (diff) | |
download | ydb-96015f265031dff57a3a8b3a70b5c097deff41bb.tar.gz |
intermediate changes
ref:ac22a335bf0b52ff7f18a64a33cc4300ee0659de
Diffstat (limited to 'contrib/python/ipython/py3/IPython/core/magic.py')
-rw-r--r-- | contrib/python/ipython/py3/IPython/core/magic.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/contrib/python/ipython/py3/IPython/core/magic.py b/contrib/python/ipython/py3/IPython/core/magic.py index bc51677f08..63b6bec685 100644 --- a/contrib/python/ipython/py3/IPython/core/magic.py +++ b/contrib/python/ipython/py3/IPython/core/magic.py @@ -310,6 +310,34 @@ class MagicsManager(Configurable): # holding the actual callable object as value. This is the dict used for # magic function dispatch magics = Dict() + lazy_magics = Dict( + help=""" + Mapping from magic names to modules to load. + + This can be used in IPython/IPykernel configuration to declare lazy magics + that will only be imported/registered on first use. + + For example:: + + c.MagicsManger.lazy_magics = { + "my_magic": "slow.to.import", + "my_other_magic": "also.slow", + } + + On first invocation of `%my_magic`, `%%my_magic`, `%%my_other_magic` or + `%%my_other_magic`, the corresponding module will be loaded as an ipython + extensions as if you had previously done `%load_ext ipython`. + + Magics names should be without percent(s) as magics can be both cell + and line magics. + + Lazy loading happen relatively late in execution process, and + complex extensions that manipulate Python/IPython internal state or global state + might not support lazy loading. + """ + ).tag( + config=True, + ) # A registry of the original objects that we've been given holding magics. registry = Dict() @@ -374,6 +402,24 @@ class MagicsManager(Configurable): docs[m_type] = m_docs return docs + def register_lazy(self, name: str, fully_qualified_name: str): + """ + Lazily register a magic via an extension. + + + Parameters + ---------- + name : str + Name of the magic you wish to register. + fully_qualified_name : + Fully qualified name of the module/submodule that should be loaded + as an extensions when the magic is first called. + It is assumed that loading this extensions will register the given + magic. + """ + + self.lazy_magics[name] = fully_qualified_name + def register(self, *magic_objects): """Register one or more instances of Magics. |