##// END OF EJS Templates
add IPython.embed_kernel()...
Scott Tsai -
Show More
@@ -44,6 +44,10 b' from .config.loader import Config'
44 from .core import release
44 from .core import release
45 from .core.application import Application
45 from .core.application import Application
46 from .frontend.terminal.embed import embed
46 from .frontend.terminal.embed import embed
47 try:
48 from .zmq.ipkernel import embed_kernel
49 except ImportError:
50 pass
47 from .core.error import TryNext
51 from .core.error import TryNext
48 from .core.interactiveshell import InteractiveShell
52 from .core.interactiveshell import InteractiveShell
49 from .testing import test
53 from .testing import test
@@ -100,7 +100,7 b' class Kernel(Configurable):'
100
100
101
101
102
102
103 def __init__(self, **kwargs):
103 def __init__(self, user_module=None, user_ns=None, **kwargs):
104 super(Kernel, self).__init__(**kwargs)
104 super(Kernel, self).__init__(**kwargs)
105
105
106 # Before we even start up the shell, register *first* our exit handlers
106 # Before we even start up the shell, register *first* our exit handlers
@@ -110,6 +110,8 b' class Kernel(Configurable):'
110 # Initialize the InteractiveShell subclass
110 # Initialize the InteractiveShell subclass
111 self.shell = ZMQInteractiveShell.instance(config=self.config,
111 self.shell = ZMQInteractiveShell.instance(config=self.config,
112 profile_dir = self.profile_dir,
112 profile_dir = self.profile_dir,
113 user_module=user_module,
114 user_ns=user_ns,
113 )
115 )
114 self.shell.displayhook.session = self.session
116 self.shell.displayhook.session = self.session
115 self.shell.displayhook.pub_socket = self.iopub_socket
117 self.shell.displayhook.pub_socket = self.iopub_socket
@@ -585,6 +587,8 b' class IPKernelApp(KernelApp, InteractiveShellApp):'
585 stdin_socket=self.stdin_socket,
587 stdin_socket=self.stdin_socket,
586 log=self.log,
588 log=self.log,
587 profile_dir=self.profile_dir,
589 profile_dir=self.profile_dir,
590 user_module=self.user_module,
591 user_ns=self.user_ns,
588 )
592 )
589 self.kernel = kernel
593 self.kernel = kernel
590 kernel.record_ports(self.ports)
594 kernel.record_ports(self.ports)
@@ -639,12 +643,27 b' def launch_kernel(*args, **kwargs):'
639 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
643 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
640 *args, **kwargs)
644 *args, **kwargs)
641
645
646 def caller_module_and_locals():
647 """Returns (module, locals) of the caller"""
648 caller = sys._getframe(1).f_back
649 global_ns = caller.f_globals
650 module = sys.modules[global_ns['__name__']]
651 return (module, caller.f_locals)
652
653 def embed_kernel(module=None, local_ns=None):
654 """Call this to embed an IPython kernel at the current point in your program. """
655 (caller_module, caller_locals) = caller_module_and_locals()
656 if module is None:
657 module = caller_module
658 if local_ns is None:
659 local_ns = caller_locals
660 app = IPKernelApp.instance(user_module=module, user_ns=local_ns)
661 app.initialize()
662 app.start()
642
663
643 def main():
664 def main():
644 """Run an IPKernel as an application"""
665 """Run an IPKernel as an application"""
645 app = IPKernelApp.instance()
666 embed_kernel()
646 app.initialize()
647 app.start()
648
667
649
668
650 if __name__ == '__main__':
669 if __name__ == '__main__':
General Comments 0
You need to be logged in to leave comments. Login now