##// END OF EJS Templates
finish kernel shims
finish kernel shims

File last commit:

r20955:c71dcbcd
r20956:ad9f42c5
Show More
embed.py
57 lines | 2.0 KiB | text/x-python | PythonLexer
MinRK
move IPKernelApp from zmq.ipkernel to zmq.kernelapp...
r9357 """Simple function for embedding an IPython kernel
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import sys
from IPython.utils.frame import extract_module_locals
Thomas Kluyver
Use explicit relative imports...
r13347 from .kernelapp import IPKernelApp
MinRK
move IPKernelApp from zmq.ipkernel to zmq.kernelapp...
r9357
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
def embed_kernel(module=None, local_ns=None, **kwargs):
"""Embed and start an IPython kernel in a given scope.
Min RK
bigsplit: ipython_kernel
r20955
MinRK
move IPKernelApp from zmq.ipkernel to zmq.kernelapp...
r9357 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)
Min RK
bigsplit: ipython_kernel
r20955
MinRK
move IPKernelApp from zmq.ipkernel to zmq.kernelapp...
r9357 kwargs : various, optional
MinRK
fix a few remaining KernelApp/IPKernelApp changes
r9516 Further keyword args are relayed to the IPKernelApp constructor,
MinRK
move IPKernelApp from zmq.ipkernel to zmq.kernelapp...
r9357 allowing configuration of the Kernel. Will only have an effect
on the first embed_kernel call for a given process.
Min RK
bigsplit: ipython_kernel
r20955
MinRK
move IPKernelApp from zmq.ipkernel to zmq.kernelapp...
r9357 """
# get the app if it exists, or set it up if it doesn't
if IPKernelApp.initialized():
app = IPKernelApp.instance()
else:
app = IPKernelApp.instance(**kwargs)
app.initialize([])
# Undo unnecessary sys module mangling from init_sys_modules.
# This would not be necessary if we could prevent it
# in the first place by using a different InteractiveShell
# subclass, as in the regular embed case.
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
# load the calling scope if not given
(caller_module, caller_locals) = extract_module_locals(1)
if module is None:
module = caller_module
if local_ns is None:
local_ns = caller_locals
Min RK
bigsplit: ipython_kernel
r20955
MinRK
move IPKernelApp from zmq.ipkernel to zmq.kernelapp...
r9357 app.kernel.user_module = module
app.kernel.user_ns = local_ns
app.shell.set_completer_frame()
app.start()