Show More
@@ -58,13 +58,29 b' for author, email in release.authors.itervalues():' | |||
|
58 | 58 | __license__ = release.license |
|
59 | 59 | __version__ = release.version |
|
60 | 60 | |
|
61 | def embed_kernel(module=None, local_ns=None): | |
|
62 | """Call this to embed an IPython kernel at the current point in your program. """ | |
|
61 | def embed_kernel(module=None, local_ns=None, **kwargs): | |
|
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 | 78 | (caller_module, caller_locals) = extract_module_locals(1) |
|
64 | 79 | if module is None: |
|
65 | 80 | module = caller_module |
|
66 | 81 | if local_ns is None: |
|
67 | 82 | local_ns = caller_locals |
|
83 | ||
|
68 | 84 | # Only import .zmq when we really need it |
|
69 | 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 | 40 | from IPython.utils import io |
|
41 | 41 | from IPython.utils import py3compat |
|
42 | from IPython.utils.frame import extract_module_locals | |
|
42 | 43 | from IPython.utils.jsonutil import json_clean |
|
43 | 44 | from IPython.utils.traitlets import ( |
|
44 | 45 | Any, Instance, Float, Dict, CaselessStrEnum |
@@ -70,8 +71,17 b' class Kernel(Configurable):' | |||
|
70 | 71 | iopub_socket = Instance('zmq.Socket') |
|
71 | 72 | stdin_socket = Instance('zmq.Socket') |
|
72 | 73 | log = Instance(logging.Logger) |
|
74 | ||
|
73 | 75 | user_module = Instance('types.ModuleType') |
|
76 | def _user_module_changed(self, name, old, new): | |
|
77 | if self.shell is not None: | |
|
78 | self.shell.user_module = new | |
|
79 | ||
|
74 | 80 |
user_ns |
|
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 | 86 | # Private interface |
|
77 | 87 | |
@@ -566,6 +576,7 b' class IPKernelApp(KernelApp, InteractiveShellApp):' | |||
|
566 | 576 | aliases = Dict(aliases) |
|
567 | 577 | flags = Dict(flags) |
|
568 | 578 | classes = [Kernel, ZMQInteractiveShell, ProfileDir, Session] |
|
579 | ||
|
569 | 580 | # configurables |
|
570 | 581 | pylab = CaselessStrEnum(['tk', 'qt', 'wx', 'gtk', 'osx', 'inline', 'auto'], |
|
571 | 582 | config=True, |
@@ -589,8 +600,6 b' class IPKernelApp(KernelApp, InteractiveShellApp):' | |||
|
589 | 600 | stdin_socket=self.stdin_socket, |
|
590 | 601 | log=self.log, |
|
591 | 602 | profile_dir=self.profile_dir, |
|
592 | user_module=self.user_module, | |
|
593 | user_ns=self.user_ns, | |
|
594 | 603 | ) |
|
595 | 604 | self.kernel = kernel |
|
596 | 605 | kernel.record_ports(self.ports) |
@@ -645,9 +654,39 b' def launch_kernel(*args, **kwargs):' | |||
|
645 | 654 | return base_launch_kernel('from IPython.zmq.ipkernel import main; main()', |
|
646 | 655 | *args, **kwargs) |
|
647 | 656 | |
|
648 | def embed_kernel(module, local_ns): | |
|
649 | app = IPKernelApp.instance(user_module=module, user_ns=local_ns) | |
|
657 | ||
|
658 | def embed_kernel(module=None, local_ns=None, **kwargs): | |
|
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) | |
|
650 | 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 | 690 | app.start() |
|
652 | 691 | |
|
653 | 692 | def main(): |
General Comments 0
You need to be logged in to leave comments.
Login now