##// END OF EJS Templates
adjust embed_kernel signature...
MinRK -
Show More
@@ -58,13 +58,29 b' for author, email in release.authors.itervalues():'
58 __license__ = release.license
58 __license__ = release.license
59 __version__ = release.version
59 __version__ = release.version
60
60
61 def embed_kernel(module=None, local_ns=None):
61 def embed_kernel(module=None, local_ns=None, **kwargs):
62 """Call this to embed an IPython kernel at the current point in your program. """
62 """Embed and start an IPython kernel in a given scope.
63
64 Parameters
65 ----------
66 module : ModuleType, optional
67 The module to load into IPython globals (default: caller)
68 local_ns : dict, optional
69 The namespace to load into IPython user namespace (default: caller)
70
71 kwargs : various, optional
72 Further keyword args are relayed to the KernelApp constructor,
73 allowing configuration of the Kernel. Will only have an effect
74 on the first embed_kernel call for a given process.
75
76 """
77
63 (caller_module, caller_locals) = extract_module_locals(1)
78 (caller_module, caller_locals) = extract_module_locals(1)
64 if module is None:
79 if module is None:
65 module = caller_module
80 module = caller_module
66 if local_ns is None:
81 if local_ns is None:
67 local_ns = caller_locals
82 local_ns = caller_locals
83
68 # Only import .zmq when we really need it
84 # Only import .zmq when we really need it
69 from .zmq.ipkernel import embed_kernel as real_embed_kernel
85 from .zmq.ipkernel import embed_kernel as real_embed_kernel
70 real_embed_kernel(module, local_ns)
86 real_embed_kernel(module=module, local_ns=local_ns, **kwargs)
@@ -39,6 +39,7 b' from IPython.core.shellapp import ('
39 )
39 )
40 from IPython.utils import io
40 from IPython.utils import io
41 from IPython.utils import py3compat
41 from IPython.utils import py3compat
42 from IPython.utils.frame import extract_module_locals
42 from IPython.utils.jsonutil import json_clean
43 from IPython.utils.jsonutil import json_clean
43 from IPython.utils.traitlets import (
44 from IPython.utils.traitlets import (
44 Any, Instance, Float, Dict, CaselessStrEnum
45 Any, Instance, Float, Dict, CaselessStrEnum
@@ -70,8 +71,17 b' class Kernel(Configurable):'
70 iopub_socket = Instance('zmq.Socket')
71 iopub_socket = Instance('zmq.Socket')
71 stdin_socket = Instance('zmq.Socket')
72 stdin_socket = Instance('zmq.Socket')
72 log = Instance(logging.Logger)
73 log = Instance(logging.Logger)
74
73 user_module = Instance('types.ModuleType')
75 user_module = Instance('types.ModuleType')
74 user_ns = Dict(default_value=None)
76 def _user_module_changed(self, name, old, new):
77 if self.shell is not None:
78 self.shell.user_module = new
79
80 user_ns = Dict(default_value=None)
81 def _user_ns_changed(self, name, old, new):
82 if self.shell is not None:
83 self.shell.user_ns = new
84 self.shell.init_user_ns()
75
85
76 # Private interface
86 # Private interface
77
87
@@ -566,6 +576,7 b' class IPKernelApp(KernelApp, InteractiveShellApp):'
566 aliases = Dict(aliases)
576 aliases = Dict(aliases)
567 flags = Dict(flags)
577 flags = Dict(flags)
568 classes = [Kernel, ZMQInteractiveShell, ProfileDir, Session]
578 classes = [Kernel, ZMQInteractiveShell, ProfileDir, Session]
579
569 # configurables
580 # configurables
570 pylab = CaselessStrEnum(['tk', 'qt', 'wx', 'gtk', 'osx', 'inline', 'auto'],
581 pylab = CaselessStrEnum(['tk', 'qt', 'wx', 'gtk', 'osx', 'inline', 'auto'],
571 config=True,
582 config=True,
@@ -589,8 +600,6 b' class IPKernelApp(KernelApp, InteractiveShellApp):'
589 stdin_socket=self.stdin_socket,
600 stdin_socket=self.stdin_socket,
590 log=self.log,
601 log=self.log,
591 profile_dir=self.profile_dir,
602 profile_dir=self.profile_dir,
592 user_module=self.user_module,
593 user_ns=self.user_ns,
594 )
603 )
595 self.kernel = kernel
604 self.kernel = kernel
596 kernel.record_ports(self.ports)
605 kernel.record_ports(self.ports)
@@ -645,9 +654,39 b' def launch_kernel(*args, **kwargs):'
645 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
654 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
646 *args, **kwargs)
655 *args, **kwargs)
647
656
648 def embed_kernel(module, local_ns):
657
649 app = IPKernelApp.instance(user_module=module, user_ns=local_ns)
658 def embed_kernel(module=None, local_ns=None, **kwargs):
650 app.initialize([])
659 """Embed and start an IPython kernel in a given scope.
660
661 Parameters
662 ----------
663 module : ModuleType, optional
664 The module to load into IPython globals (default: caller)
665 local_ns : dict, optional
666 The namespace to load into IPython user namespace (default: caller)
667
668 kwargs : various, optional
669 Further keyword args are relayed to the KernelApp constructor,
670 allowing configuration of the Kernel. Will only have an effect
671 on the first embed_kernel call for a given process.
672
673 """
674 # get the app if it exists, or set it up if it doesn't
675 if IPKernelApp.initialized():
676 app = IPKernelApp.instance()
677 else:
678 app = IPKernelApp.instance(**kwargs)
679 app.initialize([])
680
681 # load the calling scope if not given
682 (caller_module, caller_locals) = extract_module_locals(1)
683 if module is None:
684 module = caller_module
685 if local_ns is None:
686 local_ns = caller_locals
687
688 app.kernel.user_module = module
689 app.kernel.user_ns = local_ns
651 app.start()
690 app.start()
652
691
653 def main():
692 def main():
General Comments 0
You need to be logged in to leave comments. Login now