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