##// END OF EJS Templates
BUG: GUI integration broken in the in-process kernel.
epatters -
Show More
@@ -17,10 +17,12 b' import logging'
17 import sys
17 import sys
18
18
19 # Local imports.
19 # Local imports.
20 from IPython.core.interactiveshell import InteractiveShellABC
20 from IPython.inprocess.socket import DummySocket
21 from IPython.inprocess.socket import DummySocket
21 from IPython.utils.jsonutil import json_clean
22 from IPython.utils.jsonutil import json_clean
22 from IPython.utils.traitlets import Any, Instance, List
23 from IPython.utils.traitlets import Any, Enum, Instance, List, Type
23 from IPython.zmq.ipkernel import Kernel
24 from IPython.zmq.ipkernel import Kernel
25 from IPython.zmq.zmqshell import ZMQInteractiveShell
24
26
25 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
26 # Main kernel class
28 # Main kernel class
@@ -32,9 +34,17 b' class InProcessKernel(Kernel):'
32 # InProcessKernel interface
34 # InProcessKernel interface
33 #-------------------------------------------------------------------------
35 #-------------------------------------------------------------------------
34
36
37 # The frontends connected to this kernel.
35 frontends = List(
38 frontends = List(
36 Instance('IPython.inprocess.kernelmanager.InProcessKernelManager'))
39 Instance('IPython.inprocess.kernelmanager.InProcessKernelManager'))
37
40
41 # The GUI environment that the kernel is running under. This need not be
42 # specified for the normal operation for the kernel, but is required for
43 # IPython's GUI support (including pylab). The default is 'inline' because
44 # it is safe under all GUI toolkits.
45 gui = Enum(('tk', 'gtk', 'wx', 'qt', 'qt4', 'inline'),
46 default_value='inline')
47
38 raw_input_str = Any()
48 raw_input_str = Any()
39 stdout = Any()
49 stdout = Any()
40 stderr = Any()
50 stderr = Any()
@@ -43,6 +53,7 b' class InProcessKernel(Kernel):'
43 # Kernel interface
53 # Kernel interface
44 #-------------------------------------------------------------------------
54 #-------------------------------------------------------------------------
45
55
56 shell_class = Type()
46 shell_streams = List()
57 shell_streams = List()
47 control_stream = Any()
58 control_stream = Any()
48 iopub_socket = Instance(DummySocket, ())
59 iopub_socket = Instance(DummySocket, ())
@@ -55,6 +66,7 b' class InProcessKernel(Kernel):'
55 super(InProcessKernel, self).__init__(**traits)
66 super(InProcessKernel, self).__init__(**traits)
56
67
57 self.iopub_socket.on_trait_change(self._io_dispatch, 'message_sent')
68 self.iopub_socket.on_trait_change(self._io_dispatch, 'message_sent')
69 self.shell.kernel = self
58
70
59 def execute_request(self, stream, ident, parent):
71 def execute_request(self, stream, ident, parent):
60 """ Override for temporary IO redirection. """
72 """ Override for temporary IO redirection. """
@@ -122,6 +134,9 b' class InProcessKernel(Kernel):'
122 from IPython.zmq.session import Session
134 from IPython.zmq.session import Session
123 return Session(config=self.config)
135 return Session(config=self.config)
124
136
137 def _shell_class_default(self):
138 return InProcessInteractiveShell
139
125 def _stdout_default(self):
140 def _stdout_default(self):
126 from IPython.zmq.iostream import OutStream
141 from IPython.zmq.iostream import OutStream
127 return OutStream(self.session, self.iopub_socket, u'stdout')
142 return OutStream(self.session, self.iopub_socket, u'stdout')
@@ -129,3 +144,33 b' class InProcessKernel(Kernel):'
129 def _stderr_default(self):
144 def _stderr_default(self):
130 from IPython.zmq.iostream import OutStream
145 from IPython.zmq.iostream import OutStream
131 return OutStream(self.session, self.iopub_socket, u'stderr')
146 return OutStream(self.session, self.iopub_socket, u'stderr')
147
148 #-----------------------------------------------------------------------------
149 # Interactive shell subclass
150 #-----------------------------------------------------------------------------
151
152 class InProcessInteractiveShell(ZMQInteractiveShell):
153
154 kernel = Instance('IPython.inprocess.ipkernel.InProcessKernel')
155
156 #-------------------------------------------------------------------------
157 # InteractiveShell interface
158 #-------------------------------------------------------------------------
159
160 def enable_gui(self, gui=None):
161 """ Enable GUI integration for the kernel.
162 """
163 from IPython.zmq.eventloops import enable_gui
164 if not gui:
165 gui = self.kernel.gui
166 enable_gui(gui, kernel=self.kernel)
167
168 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
169 """ Activate pylab support at runtime.
170 """
171 if not gui:
172 gui = self.kernel.gui
173 super(InProcessInteractiveShell, self).enable_pylab(gui, import_all,
174 welcome_message)
175
176 InteractiveShellABC.register(InProcessInteractiveShell)
@@ -47,7 +47,8 b' from IPython.utils import py3compat'
47 from IPython.utils.frame import extract_module_locals
47 from IPython.utils.frame import extract_module_locals
48 from IPython.utils.jsonutil import json_clean
48 from IPython.utils.jsonutil import json_clean
49 from IPython.utils.traitlets import (
49 from IPython.utils.traitlets import (
50 Any, Instance, Float, Dict, CaselessStrEnum, List, Set, Integer, Unicode
50 Any, Instance, Float, Dict, CaselessStrEnum, List, Set, Integer, Unicode,
51 Type
51 )
52 )
52
53
53 from entry_point import base_launch_kernel
54 from entry_point import base_launch_kernel
@@ -75,6 +76,8 b' class Kernel(Configurable):'
75 loop.add_timeout(time.time()+0.1, self.enter_eventloop)
76 loop.add_timeout(time.time()+0.1, self.enter_eventloop)
76
77
77 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
78 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
79 shell_class = Type(ZMQInteractiveShell)
80
78 session = Instance(Session)
81 session = Instance(Session)
79 profile_dir = Instance('IPython.core.profiledir.ProfileDir')
82 profile_dir = Instance('IPython.core.profiledir.ProfileDir')
80 shell_streams = List()
83 shell_streams = List()
@@ -137,7 +140,7 b' class Kernel(Configurable):'
137 super(Kernel, self).__init__(**kwargs)
140 super(Kernel, self).__init__(**kwargs)
138
141
139 # Initialize the InteractiveShell subclass
142 # Initialize the InteractiveShell subclass
140 self.shell = ZMQInteractiveShell.instance(config=self.config,
143 self.shell = self.shell_class.instance(config=self.config,
141 profile_dir = self.profile_dir,
144 profile_dir = self.profile_dir,
142 user_module = self.user_module,
145 user_module = self.user_module,
143 user_ns = self.user_ns,
146 user_ns = self.user_ns,
@@ -14,8 +14,18 b' from IPython.lib import guisupport'
14 def main():
14 def main():
15 app = guisupport.get_app_qt4()
15 app = guisupport.get_app_qt4()
16
16
17 # Create a kernel and populate the namespace.
17 # Create a kernel.
18 kernel = InProcessKernel()
18 #
19 # Setting the GUI is not necessary for the normal operation of the kernel,
20 # but it is used for IPython GUI's integration, particularly in pylab. By
21 # default, the inline backend is used, which is safe under all toolkits.
22 #
23 # WARNING: Under no circumstances should another GUI toolkit, like wx, be
24 # used when running a Qt application. This will lead to unexpected behavior,
25 # including segfaults.
26 kernel = InProcessKernel(gui='qt4')
27
28 # Populate the kernel's namespace.
19 kernel.shell.push({'x': 0, 'y': 1, 'z': 2})
29 kernel.shell.push({'x': 0, 'y': 1, 'z': 2})
20
30
21 # Create a kernel manager for the frontend and register it with the kernel.
31 # Create a kernel manager for the frontend and register it with the kernel.
General Comments 0
You need to be logged in to leave comments. Login now