Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

BUG: GUI integration broken in the in-process kernel.

  • Loading branch information...
commit 6aaeb873ab31decda94ac9367871bb845f473773 1 parent d2ab68e
@epatters epatters authored
View
47 IPython/inprocess/ipkernel.py
@@ -17,10 +17,12 @@
import sys
# Local imports.
+from IPython.core.interactiveshell import InteractiveShellABC
from IPython.inprocess.socket import DummySocket
from IPython.utils.jsonutil import json_clean
-from IPython.utils.traitlets import Any, Instance, List
+from IPython.utils.traitlets import Any, Enum, Instance, List, Type
from IPython.zmq.ipkernel import Kernel
+from IPython.zmq.zmqshell import ZMQInteractiveShell
#-----------------------------------------------------------------------------
# Main kernel class
@@ -32,9 +34,17 @@ class InProcessKernel(Kernel):
# InProcessKernel interface
#-------------------------------------------------------------------------
+ # The frontends connected to this kernel.
frontends = List(
Instance('IPython.inprocess.kernelmanager.InProcessKernelManager'))
+ # The GUI environment that the kernel is running under. This need not be
+ # specified for the normal operation for the kernel, but is required for
+ # IPython's GUI support (including pylab). The default is 'inline' because
+ # it is safe under all GUI toolkits.
+ gui = Enum(('tk', 'gtk', 'wx', 'qt', 'qt4', 'inline'),
+ default_value='inline')
+
raw_input_str = Any()
stdout = Any()
stderr = Any()
@@ -43,6 +53,7 @@ class InProcessKernel(Kernel):
# Kernel interface
#-------------------------------------------------------------------------
+ shell_class = Type()
shell_streams = List()
control_stream = Any()
iopub_socket = Instance(DummySocket, ())
@@ -55,6 +66,7 @@ def __init__(self, **traits):
super(InProcessKernel, self).__init__(**traits)
self.iopub_socket.on_trait_change(self._io_dispatch, 'message_sent')
+ self.shell.kernel = self
def execute_request(self, stream, ident, parent):
""" Override for temporary IO redirection. """
@@ -122,6 +134,9 @@ def _session_default(self):
from IPython.zmq.session import Session
return Session(config=self.config)
+ def _shell_class_default(self):
+ return InProcessInteractiveShell
+
def _stdout_default(self):
from IPython.zmq.iostream import OutStream
return OutStream(self.session, self.iopub_socket, u'stdout')
@@ -129,3 +144,33 @@ def _stdout_default(self):
def _stderr_default(self):
from IPython.zmq.iostream import OutStream
return OutStream(self.session, self.iopub_socket, u'stderr')
+
+#-----------------------------------------------------------------------------
+# Interactive shell subclass
+#-----------------------------------------------------------------------------
+
+class InProcessInteractiveShell(ZMQInteractiveShell):
+
+ kernel = Instance('IPython.inprocess.ipkernel.InProcessKernel')
+
+ #-------------------------------------------------------------------------
+ # InteractiveShell interface
+ #-------------------------------------------------------------------------
+
+ def enable_gui(self, gui=None):
+ """ Enable GUI integration for the kernel.
+ """
+ from IPython.zmq.eventloops import enable_gui
+ if not gui:
+ gui = self.kernel.gui
+ enable_gui(gui, kernel=self.kernel)
+
+ def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
+ """ Activate pylab support at runtime.
+ """
+ if not gui:
+ gui = self.kernel.gui
+ super(InProcessInteractiveShell, self).enable_pylab(gui, import_all,
+ welcome_message)
+
+InteractiveShellABC.register(InProcessInteractiveShell)
View
7 IPython/zmq/ipkernel.py
@@ -47,7 +47,8 @@
from IPython.utils.frame import extract_module_locals
from IPython.utils.jsonutil import json_clean
from IPython.utils.traitlets import (
- Any, Instance, Float, Dict, CaselessStrEnum, List, Set, Integer, Unicode
+ Any, Instance, Float, Dict, CaselessStrEnum, List, Set, Integer, Unicode,
+ Type
)
from entry_point import base_launch_kernel
@@ -75,6 +76,8 @@ def _eventloop_changed(self, name, old, new):
loop.add_timeout(time.time()+0.1, self.enter_eventloop)
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
+ shell_class = Type(ZMQInteractiveShell)
+
session = Instance(Session)
profile_dir = Instance('IPython.core.profiledir.ProfileDir')
shell_streams = List()
@@ -137,7 +140,7 @@ def __init__(self, **kwargs):
super(Kernel, self).__init__(**kwargs)
# Initialize the InteractiveShell subclass
- self.shell = ZMQInteractiveShell.instance(config=self.config,
+ self.shell = self.shell_class.instance(config=self.config,
profile_dir = self.profile_dir,
user_module = self.user_module,
user_ns = self.user_ns,
View
14 docs/examples/frontend/inprocess_qtconsole.py
@@ -14,8 +14,18 @@
def main():
app = guisupport.get_app_qt4()
- # Create a kernel and populate the namespace.
- kernel = InProcessKernel()
+ # Create a kernel.
+ #
+ # Setting the GUI is not necessary for the normal operation of the kernel,
+ # but it is used for IPython GUI's integration, particularly in pylab. By
+ # default, the inline backend is used, which is safe under all toolkits.
+ #
+ # WARNING: Under no circumstances should another GUI toolkit, like wx, be
+ # used when running a Qt application. This will lead to unexpected behavior,
+ # including segfaults.
+ kernel = InProcessKernel(gui='qt4')
+
+ # Populate the kernel's namespace.
kernel.shell.push({'x': 0, 'y': 1, 'z': 2})
# Create a kernel manager for the frontend and register it with the kernel.
Please sign in to comment.
Something went wrong with that request. Please try again.