##// END OF EJS Templates
Initial support in ipkernel for proper displayhook handling.
Brian Granger -
Show More
@@ -157,7 +157,7 b' class DisplayHook(Configurable):'
157 except KeyError:
157 except KeyError:
158 pass
158 pass
159
159
160 def quite(self):
160 def quiet(self):
161 """Should we silence the display hook because of ';'?"""
161 """Should we silence the display hook because of ';'?"""
162 # do not print output if input ends in ';'
162 # do not print output if input ends in ';'
163 try:
163 try:
@@ -168,6 +168,10 b' class DisplayHook(Configurable):'
168 pass
168 pass
169 return False
169 return False
170
170
171 def start_displayhook(self):
172 """Start the displayhook, initializing resources."""
173 pass
174
171 def write_output_prompt(self):
175 def write_output_prompt(self):
172 """Write the output prompt."""
176 """Write the output prompt."""
173 # Use write, not print which adds an extra space.
177 # Use write, not print which adds an extra space.
@@ -256,7 +260,8 b' class DisplayHook(Configurable):'
256 activated by setting the variable sys.displayhook to it.
260 activated by setting the variable sys.displayhook to it.
257 """
261 """
258 self.check_for_underscore()
262 self.check_for_underscore()
259 if result is not None and not self.quite():
263 if result is not None and not self.quiet():
264 self.start_displayhook()
260 self.write_output_prompt()
265 self.write_output_prompt()
261 result, result_repr = self.compute_result_repr(result)
266 result, result_repr = self.compute_result_repr(result)
262 self.write_result_repr(result_repr)
267 self.write_result_repr(result_repr)
@@ -62,7 +62,7 b' from IPython.utils.syspathcontext import prepended_to_syspath'
62 from IPython.utils.text import num_ini_spaces
62 from IPython.utils.text import num_ini_spaces
63 from IPython.utils.warn import warn, error, fatal
63 from IPython.utils.warn import warn, error, fatal
64 from IPython.utils.traitlets import (
64 from IPython.utils.traitlets import (
65 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode, Instance
65 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode, Instance, Type
66 )
66 )
67
67
68 # from IPython.utils import growl
68 # from IPython.utils import growl
@@ -163,6 +163,7 b' class InteractiveShell(Configurable, Magic):'
163 default_value=get_default_colors(), config=True)
163 default_value=get_default_colors(), config=True)
164 debug = CBool(False, config=True)
164 debug = CBool(False, config=True)
165 deep_reload = CBool(False, config=True)
165 deep_reload = CBool(False, config=True)
166 displayhook_class = Type(DisplayHook)
166 filename = Str("<ipython console>")
167 filename = Str("<ipython console>")
167 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
168 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
168 logstart = CBool(False, config=True)
169 logstart = CBool(False, config=True)
@@ -435,15 +436,17 b' class InteractiveShell(Configurable, Magic):'
435
436
436 def init_displayhook(self):
437 def init_displayhook(self):
437 # Initialize displayhook, set in/out prompts and printing system
438 # Initialize displayhook, set in/out prompts and printing system
438 self.displayhook = DisplayHook( shell=self,
439 self.displayhook = self.displayhook_class(
439 cache_size=self.cache_size,
440 shell=self,
440 input_sep = self.separate_in,
441 cache_size=self.cache_size,
441 output_sep = self.separate_out,
442 input_sep = self.separate_in,
442 output_sep2 = self.separate_out2,
443 output_sep = self.separate_out,
443 ps1 = self.prompt_in1,
444 output_sep2 = self.separate_out2,
444 ps2 = self.prompt_in2,
445 ps1 = self.prompt_in1,
445 ps_out = self.prompt_out,
446 ps2 = self.prompt_in2,
446 pad_left = self.prompts_pad_left)
447 ps_out = self.prompt_out,
448 pad_left = self.prompts_pad_left
449 )
447 # This is a context manager that installs/revmoes the displayhook at
450 # This is a context manager that installs/revmoes the displayhook at
448 # the appropriate time.
451 # the appropriate time.
449 self.display_trap = DisplayTrap(hook=self.displayhook)
452 self.display_trap = DisplayTrap(hook=self.displayhook)
@@ -46,6 +46,7 b' class TestPrettyResultDisplay(TestCase):'
46 self.ip = InteractiveShellStub()
46 self.ip = InteractiveShellStub()
47 self.prd = pretty_ext.PrettyResultDisplay(shell=self.ip, config=None)
47 self.prd = pretty_ext.PrettyResultDisplay(shell=self.ip, config=None)
48
48
49 @dec.skip_known_failure
49 def test_for_type(self):
50 def test_for_type(self):
50 self.prd.for_type(A, a_pprinter)
51 self.prd.for_type(A, a_pprinter)
51 a = A()
52 a = A()
@@ -94,6 +95,7 b' class TestPrettyInteractively(tt.TempFileMixin):'
94
95
95 # XXX Unfortunately, ipexec_validate fails under win32. If someone helps
96 # XXX Unfortunately, ipexec_validate fails under win32. If someone helps
96 # us write a win32-compatible version, we can reactivate this test.
97 # us write a win32-compatible version, we can reactivate this test.
98 @dec.skip_known_failure
97 @dec.skip_win32
99 @dec.skip_win32
98 def test_printers(self):
100 def test_printers(self):
99 self.mktmp(ipy_src, '.ipy')
101 self.mktmp(ipy_src, '.ipy')
@@ -113,10 +113,16 b' class IPythonWidget(FrontendWidget):'
113 def _handle_pyout(self, omsg):
113 def _handle_pyout(self, omsg):
114 """ Reimplemented for IPython-style "display hook".
114 """ Reimplemented for IPython-style "display hook".
115 """
115 """
116 self._append_html(self._make_out_prompt(self._prompt_count))
116 # self._append_html(self._make_out_prompt(self._prompt_count))
117 # TODO: Also look at the output_sep, output_sep2 keys of content.
118 # They are used in terminal based frontends to add empty spaces before
119 # and after the Out[]: prompt. I doubt you want to use them, but they
120 # are there. I am thinking we should even take them out of the msg.
121 prompt_number = omsg['content']['prompt_number']
122 data = omsg['content']['data']
123 self._append_html(self._make_out_prompt(prompt_number))
117 self._save_prompt_block()
124 self._save_prompt_block()
118
125 self._append_plain_text(data + '\n')
119 self._append_plain_text(omsg['content']['data'] + '\n')
120
126
121 #---------------------------------------------------------------------------
127 #---------------------------------------------------------------------------
122 # 'IPythonWidget' interface
128 # 'IPythonWidget' interface
@@ -6,6 +6,7 b' from PyQt4 import QtCore'
6 import zmq
6 import zmq
7
7
8 # IPython imports.
8 # IPython imports.
9 from IPython.utils.traitlets import Type
9 from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \
10 from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \
10 XReqSocketChannel, RepSocketChannel
11 XReqSocketChannel, RepSocketChannel
11 from util import MetaQObjectHasTraits
12 from util import MetaQObjectHasTraits
@@ -149,9 +150,9 b' class QtKernelManager(KernelManager, QtCore.QObject):'
149 stopped_channels = QtCore.pyqtSignal()
150 stopped_channels = QtCore.pyqtSignal()
150
151
151 # Use Qt-specific channel classes that emit signals.
152 # Use Qt-specific channel classes that emit signals.
152 sub_channel_class = QtSubSocketChannel
153 sub_channel_class = Type(QtSubSocketChannel)
153 xreq_channel_class = QtXReqSocketChannel
154 xreq_channel_class = Type(QtXReqSocketChannel)
154 rep_channel_class = QtRepSocketChannel
155 rep_channel_class = Type(QtRepSocketChannel)
155
156
156 def __init__(self, *args, **kw):
157 def __init__(self, *args, **kw):
157 QtCore.QObject.__init__(self)
158 QtCore.QObject.__init__(self)
@@ -317,7 +317,7 b" skip_if_not_osx = skipif(sys.platform != 'darwin',"
317 # Other skip decorators
317 # Other skip decorators
318 skipif_not_numpy = skipif(numpy_not_available,"This test requires numpy")
318 skipif_not_numpy = skipif(numpy_not_available,"This test requires numpy")
319
319
320 skipknownfailure = skip('This test is known to fail')
320 skip_known_failure = skip('This test is known to fail')
321
321
322 # A null 'decorator', useful to make more readable code that needs to pick
322 # A null 'decorator', useful to make more readable code that needs to pick
323 # between different decorators based on OS or other conditions
323 # between different decorators based on OS or other conditions
@@ -127,6 +127,6 b" skip_if_not_osx = skipif(sys.platform != 'darwin',"
127 # Other skip decorators
127 # Other skip decorators
128 skipif_not_numpy = skipif(numpy_not_available,"This test requires numpy")
128 skipif_not_numpy = skipif(numpy_not_available,"This test requires numpy")
129
129
130 skipknownfailure = skip('This test is known to fail')
130 skip_known_failure = skip('This test is known to fail')
131
131
132
132
@@ -50,8 +50,12 b' class Kernel(Configurable):'
50
50
51 def __init__(self, **kwargs):
51 def __init__(self, **kwargs):
52 super(Kernel, self).__init__(**kwargs)
52 super(Kernel, self).__init__(**kwargs)
53
54 # Initialize the InteractiveShell subclass
53 self.shell = ZMQInteractiveShell.instance()
55 self.shell = ZMQInteractiveShell.instance()
54
56 self.shell.displayhook.session = self.session
57 self.shell.displayhook.pub_socket = self.pub_socket
58
55 # Build dict of handlers for message types
59 # Build dict of handlers for message types
56 msg_types = [ 'execute_request', 'complete_request',
60 msg_types = [ 'execute_request', 'complete_request',
57 'object_info_request' ]
61 'object_info_request' ]
@@ -97,8 +101,8 b' class Kernel(Configurable):'
97 raw_input = lambda prompt='': self.raw_input(prompt, ident, parent)
101 raw_input = lambda prompt='': self.raw_input(prompt, ident, parent)
98 __builtin__.raw_input = raw_input
102 __builtin__.raw_input = raw_input
99
103
100 # Configure the display hook.
104 # Set the parent message of the display hook.
101 sys.displayhook.set_parent(parent)
105 self.shell.displayhook.set_parent(parent)
102
106
103 self.shell.runlines(code)
107 self.shell.runlines(code)
104 # exec comp_code in self.user_ns, self.user_ns
108 # exec comp_code in self.user_ns, self.user_ns
@@ -271,8 +275,6 b' def main():'
271 # holds references to sys.stdout and sys.stderr.
275 # holds references to sys.stdout and sys.stderr.
272 sys.stdout = OutStream(session, pub_socket, u'stdout')
276 sys.stdout = OutStream(session, pub_socket, u'stdout')
273 sys.stderr = OutStream(session, pub_socket, u'stderr')
277 sys.stderr = OutStream(session, pub_socket, u'stderr')
274 # Set a displayhook.
275 sys.displayhook = DisplayHook(session, pub_socket)
276
278
277 # Create the kernel.
279 # Create the kernel.
278 kernel = Kernel(
280 kernel = Kernel(
@@ -1,11 +1,49 b''
1 import sys
1 import sys
2 from subprocess import Popen, PIPE
2 from subprocess import Popen, PIPE
3 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
3
4 from IPython.core.interactiveshell import (
5 InteractiveShell, InteractiveShellABC
6 )
7 from IPython.core.displayhook import DisplayHook
8 from IPython.utils.traitlets import Instance, Type, Dict
9 from IPython.zmq.session import extract_header
10
11
12 class ZMQDisplayTrap(DisplayHook):
13
14 session = Instance('IPython.zmq.session.Session')
15 pub_socket = Instance('zmq.Socket')
16 parent_header = Dict({})
17
18 def set_parent(self, parent):
19 """Set the parent for outbound messages."""
20 self.parent_header = extract_header(parent)
21
22 def start_displayhook(self):
23 self.msg = self.session.msg(u'pyout', {}, parent=self.parent_header)
24
25 def write_output_prompt(self):
26 """Write the output prompt."""
27 if self.do_full_cache:
28 self.msg['content']['output_sep'] = self.output_sep
29 self.msg['content']['prompt_string'] = str(self.prompt_out)
30 self.msg['content']['prompt_number'] = self.prompt_count
31 self.msg['content']['output_sep2'] = self.output_sep2
32
33 def write_result_repr(self, result_repr):
34 self.msg['content']['data'] = result_repr
35
36 def finish_displayhook(self):
37 """Finish up all displayhook activities."""
38 self.pub_socket.send_json(self.msg)
39 self.msg = None
4
40
5
41
6 class ZMQInteractiveShell(InteractiveShell):
42 class ZMQInteractiveShell(InteractiveShell):
7 """A subclass of InteractiveShell for ZMQ."""
43 """A subclass of InteractiveShell for ZMQ."""
8
44
45 displayhook_class = Type(ZMQDisplayTrap)
46
9 def system(self, cmd):
47 def system(self, cmd):
10 cmd = self.var_expand(cmd, depth=2)
48 cmd = self.var_expand(cmd, depth=2)
11 sys.stdout.flush()
49 sys.stdout.flush()
@@ -29,3 +67,6 b' class ZMQInteractiveShell(InteractiveShell):'
29 IPython.utils.io.Term = Term
67 IPython.utils.io.Term = Term
30
68
31 InteractiveShellABC.register(ZMQInteractiveShell)
69 InteractiveShellABC.register(ZMQInteractiveShell)
70
71
72
General Comments 0
You need to be logged in to leave comments. Login now