Show More
@@ -1,78 +1,82 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 | from IPython.kernel.zmq.session import Message | |
15 |
|
15 | |||
16 | @skip_doctest |
|
16 | @skip_doctest | |
17 | class Output(DOMWidget): |
|
17 | class Output(DOMWidget): | |
18 | """Widget used as a context manager to display output. |
|
18 | """Widget used as a context manager to display output. | |
19 |
|
19 | |||
20 | 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 | |
21 | 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 | |
22 | 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 | |
23 | displayed in it instead of the standard output area. |
|
23 | displayed in it instead of the standard output area. | |
24 |
|
24 | |||
25 | Example |
|
25 | Example | |
26 | from IPython.html import widgets |
|
26 | from IPython.html import widgets | |
27 | from IPython.display import display |
|
27 | from IPython.display import display | |
28 | out = widgets.Output() |
|
28 | out = widgets.Output() | |
29 | display(out) |
|
29 | display(out) | |
30 |
|
30 | |||
31 | print('prints to output area') |
|
31 | print('prints to output area') | |
32 |
|
32 | |||
33 | with out: |
|
33 | with out: | |
34 | print('prints to output widget')""" |
|
34 | print('prints to output widget')""" | |
35 | _view_name = Unicode('OutputView', sync=True) |
|
35 | _view_name = Unicode('OutputView', sync=True) | |
36 |
|
36 | |||
|
37 | def __init__(self, *args, **kwargs): | |||
|
38 | super(Output, self).__init__(*args, **kwargs) | |||
|
39 | self._ip = get_ipython() | |||
|
40 | ||||
37 | def clear_output(self, *pargs, **kwargs): |
|
41 | def clear_output(self, *pargs, **kwargs): | |
38 | with self: |
|
42 | with self: | |
39 | clear_output(*pargs, **kwargs) |
|
43 | clear_output(*pargs, **kwargs) | |
40 |
|
44 | |||
41 | def __enter__(self): |
|
45 | def __enter__(self): | |
42 | """Called upon entering output widget context manager.""" |
|
46 | """Called upon entering output widget context manager.""" | |
43 | self._flush() |
|
47 | self._flush() | |
44 |
kernel = |
|
48 | kernel = self._ip.kernel | |
45 | session = kernel.session |
|
49 | session = kernel.session | |
46 | send = session.send |
|
50 | send = session.send | |
47 | self._original_send = send |
|
51 | self._original_send = send | |
48 | self._session = session |
|
52 | self._session = session | |
49 |
|
53 | |||
50 | def send_hook(stream, msg_or_type, content=None, parent=None, ident=None, |
|
54 | def send_hook(stream, msg_or_type, content=None, parent=None, ident=None, | |
51 | buffers=None, track=False, header=None, metadata=None): |
|
55 | buffers=None, track=False, header=None, metadata=None): | |
52 |
|
56 | |||
53 | # Handle both prebuild messages and unbuilt messages. |
|
57 | # Handle both prebuild messages and unbuilt messages. | |
54 | if isinstance(msg_or_type, (Message, dict)): |
|
58 | if isinstance(msg_or_type, (Message, dict)): | |
55 | msg_type = msg_or_type['msg_type'] |
|
59 | msg_type = msg_or_type['msg_type'] | |
56 | msg = dict(msg_or_type) |
|
60 | msg = dict(msg_or_type) | |
57 | else: |
|
61 | else: | |
58 | msg_type = msg_or_type |
|
62 | msg_type = msg_or_type | |
59 | msg = session.msg(msg_type, content=content, parent=parent, |
|
63 | msg = session.msg(msg_type, content=content, parent=parent, | |
60 | header=header, metadata=metadata) |
|
64 | header=header, metadata=metadata) | |
61 |
|
65 | |||
62 | # If this is a message type that we want to forward, forward it. |
|
66 | # If this is a message type that we want to forward, forward it. | |
63 | if stream is kernel.iopub_socket and msg_type in ['clear_output', 'stream', 'display_data']: |
|
67 | if stream is kernel.iopub_socket and msg_type in ['clear_output', 'stream', 'display_data']: | |
64 | self.send(msg) |
|
68 | self.send(msg) | |
65 | else: |
|
69 | else: | |
66 | send(stream, msg, ident=ident, buffers=buffers, track=track) |
|
70 | send(stream, msg, ident=ident, buffers=buffers, track=track) | |
67 |
|
71 | |||
68 | session.send = send_hook |
|
72 | session.send = send_hook | |
69 |
|
73 | |||
70 | def __exit__(self, exception_type, exception_value, traceback): |
|
74 | def __exit__(self, exception_type, exception_value, traceback): | |
71 | """Called upon exiting output widget context manager.""" |
|
75 | """Called upon exiting output widget context manager.""" | |
72 | self._flush() |
|
76 | self._flush() | |
73 | self._session.send = self._original_send |
|
77 | self._session.send = self._original_send | |
74 |
|
78 | |||
75 | def _flush(self): |
|
79 | def _flush(self): | |
76 | """Flush stdout and stderr buffers.""" |
|
80 | """Flush stdout and stderr buffers.""" | |
77 | sys.stdout.flush() |
|
81 | sys.stdout.flush() | |
78 | sys.stderr.flush() |
|
82 | sys.stderr.flush() |
General Comments 0
You need to be logged in to leave comments.
Login now