##// END OF EJS Templates
Add support for Message class.
Jonathan Frederic -
Show More
@@ -1,78 +1,79 b''
1 """Output class.
1 """Output class.
2
2
3 Represents a widget that can be used to display output within the widget area.
3 Represents a widget that can be used to display output within the widget area.
4 """
4 """
5
5
6 # Copyright (c) IPython Development Team.
6 # Copyright (c) IPython Development Team.
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8
8
9 from .widget import DOMWidget
9 from .widget import DOMWidget
10 import sys
10 import sys
11 from IPython.utils.traitlets import Unicode, List
11 from IPython.utils.traitlets import Unicode, List
12 from IPython.display import clear_output
12 from IPython.display import clear_output
13 from IPython.testing.skipdoctest import skip_doctest
13 from IPython.testing.skipdoctest import skip_doctest
14 from IPython.kernel.zmq.session import Message
14
15
15 @skip_doctest
16 @skip_doctest
16 class Output(DOMWidget):
17 class Output(DOMWidget):
17 """Widget used as a context manager to display output.
18 """Widget used as a context manager to display output.
18
19
19 This widget can capture and display stdout, stderr, and rich output. To use
20 This widget can capture and display stdout, stderr, and rich output. To use
20 it, create an instance of it and display it. Then use it as a context
21 it, create an instance of it and display it. Then use it as a context
21 manager. Any output produced while in it's context will be captured and
22 manager. Any output produced while in it's context will be captured and
22 displayed in it instead of the standard output area.
23 displayed in it instead of the standard output area.
23
24
24 Example
25 Example
25 from IPython.html import widgets
26 from IPython.html import widgets
26 from IPython.display import display
27 from IPython.display import display
27 out = widgets.Output()
28 out = widgets.Output()
28 display(out)
29 display(out)
29
30
30 print('prints to output area')
31 print('prints to output area')
31
32
32 with out:
33 with out:
33 print('prints to output widget')"""
34 print('prints to output widget')"""
34 _view_name = Unicode('OutputView', sync=True)
35 _view_name = Unicode('OutputView', sync=True)
35
36
36 def clear_output(self, *pargs, **kwargs):
37 def clear_output(self, *pargs, **kwargs):
37 with self:
38 with self:
38 clear_output(*pargs, **kwargs)
39 clear_output(*pargs, **kwargs)
39
40
40 def __enter__(self):
41 def __enter__(self):
41 """Called upon entering output widget context manager."""
42 """Called upon entering output widget context manager."""
42 self._flush()
43 self._flush()
43 kernel = get_ipython().kernel
44 kernel = get_ipython().kernel
44 session = kernel.session
45 session = kernel.session
45 send = session.send
46 send = session.send
46 self._original_send = send
47 self._original_send = send
47 self._session = session
48 self._session = session
48
49
49 def send_hook(stream, msg_or_type, content=None, parent=None, ident=None,
50 def send_hook(stream, msg_or_type, content=None, parent=None, ident=None,
50 buffers=None, track=False, header=None, metadata=None):
51 buffers=None, track=False, header=None, metadata=None):
51
52
52 # Handle both prebuild messages and unbuilt messages.
53 # Handle both prebuild messages and unbuilt messages.
53 if isinstance(msg_or_type, dict):
54 if isinstance(msg_or_type, (Message, dict)):
54 msg_type = msg_or_type['msg_type']
55 msg_type = msg_or_type['msg_type']
55 msg = msg_or_type
56 msg = dict(msg_or_type)
56 else:
57 else:
57 msg_type = msg_or_type
58 msg_type = msg_or_type
58 msg = session.msg(msg_type, content=content, parent=parent,
59 msg = session.msg(msg_type, content=content, parent=parent,
59 header=header, metadata=metadata)
60 header=header, metadata=metadata)
60
61
61 # If this is a message type that we want to forward, forward it.
62 # If this is a message type that we want to forward, forward it.
62 if stream is kernel.iopub_socket and msg_type in ['clear_output', 'stream', 'display_data']:
63 if stream is kernel.iopub_socket and msg_type in ['clear_output', 'stream', 'display_data']:
63 self.send(msg)
64 self.send(msg)
64 else:
65 else:
65 send(stream, msg_or_type, content=content, parent=parent, ident=ident,
66 send(stream, msg_or_type, content=content, parent=parent, ident=ident,
66 buffers=buffers, track=track, header=header, metadata=metadata)
67 buffers=buffers, track=track, header=header, metadata=metadata)
67
68
68 session.send = send_hook
69 session.send = send_hook
69
70
70 def __exit__(self, exception_type, exception_value, traceback):
71 def __exit__(self, exception_type, exception_value, traceback):
71 """Called upon exiting output widget context manager."""
72 """Called upon exiting output widget context manager."""
72 self._flush()
73 self._flush()
73 self._session.send = self._original_send
74 self._session.send = self._original_send
74
75
75 def _flush(self):
76 def _flush(self):
76 """Flush stdout and stderr buffers."""
77 """Flush stdout and stderr buffers."""
77 sys.stdout.flush()
78 sys.stdout.flush()
78 sys.stderr.flush()
79 sys.stderr.flush()
General Comments 0
You need to be logged in to leave comments. Login now