| """Simple function for embedding an IPython kernel""" |
| |
| |
| |
|
|
| import sys |
|
|
| from IPython.utils.frame import extract_module_locals |
|
|
| from .kernelapp import IPKernelApp |
|
|
| |
| |
| |
|
|
|
|
| def embed_kernel(module=None, local_ns=None, **kwargs): |
| """Embed and start an IPython kernel in a given scope. |
| |
| Parameters |
| ---------- |
| module : ModuleType, optional |
| The module to load into IPython globals (default: caller) |
| local_ns : dict, optional |
| The namespace to load into IPython user namespace (default: caller) |
| kwargs : dict, optional |
| Further keyword args are relayed to the IPKernelApp constructor, |
| allowing configuration of the Kernel. Will only have an effect |
| on the first embed_kernel call for a given process. |
| |
| """ |
| |
| if IPKernelApp.initialized(): |
| app = IPKernelApp.instance() |
| else: |
| app = IPKernelApp.instance(**kwargs) |
| app.initialize([]) |
| |
| |
| |
| |
| main = app.kernel.shell._orig_sys_modules_main_mod |
| if main is not None: |
| sys.modules[app.kernel.shell._orig_sys_modules_main_name] = main |
|
|
| |
| (caller_module, caller_locals) = extract_module_locals(1) |
| if module is None: |
| module = caller_module |
| if local_ns is None: |
| local_ns = dict(**caller_locals) |
|
|
| app.kernel.user_module = module |
| assert isinstance(local_ns, dict) |
| app.kernel.user_ns = local_ns |
| app.shell.set_completer_frame() |
| app.start() |
|
|