Show More
@@ -1,67 +1,66 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 |
|
14 | |||
15 | @skip_doctest |
|
15 | @skip_doctest | |
16 | class Output(DOMWidget): |
|
16 | class Output(DOMWidget): | |
17 | """Widget used as a context manager to display output. |
|
17 | """Widget used as a context manager to display output. | |
18 |
|
18 | |||
19 | This widget can capture and display stdout, stderr, and rich output. To use |
|
19 | 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 |
|
20 | 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 |
|
21 | manager. Any output produced while in it's context will be captured and | |
22 | displayed in it instead of the standard output area. |
|
22 | displayed in it instead of the standard output area. | |
23 |
|
23 | |||
24 | Example |
|
24 | Example | |
25 | from IPython.html import widgets |
|
25 | from IPython.html import widgets | |
26 | from IPython.display import display |
|
26 | from IPython.display import display | |
27 | out = widgets.Output() |
|
27 | out = widgets.Output() | |
28 | display(out) |
|
28 | display(out) | |
29 |
|
29 | |||
30 | print('prints to output area') |
|
30 | print('prints to output area') | |
31 |
|
31 | |||
32 | with out: |
|
32 | with out: | |
33 | print('prints to output widget')""" |
|
33 | print('prints to output widget')""" | |
34 | _view_name = Unicode('OutputView', sync=True) |
|
34 | _view_name = Unicode('OutputView', sync=True) | |
35 |
|
35 | |||
36 | def clear_output(self, *pargs, **kwargs): |
|
36 | def clear_output(self, *pargs, **kwargs): | |
37 | with self: |
|
37 | with self: | |
38 | clear_output(*pargs, **kwargs) |
|
38 | clear_output(*pargs, **kwargs) | |
39 |
|
39 | |||
40 | def __enter__(self): |
|
40 | def __enter__(self): | |
41 | """Called upon entering output widget context manager.""" |
|
41 | """Called upon entering output widget context manager.""" | |
42 | self._flush() |
|
42 | self._flush() | |
43 | kernel = get_ipython().kernel |
|
43 | kernel = get_ipython().kernel | |
44 | session = kernel.session |
|
44 | session = kernel.session | |
45 | send = session.send |
|
45 | send = session.send | |
46 | self._original_send = send |
|
46 | self._original_send = send | |
47 | self._session = session |
|
47 | self._session = session | |
48 |
|
48 | |||
49 | def send_hook(stream, msg_or_type, *args, **kwargs): |
|
49 | def send_hook(stream, msg_or_type, *args, **kwargs): | |
50 | if stream is kernel.iopub_socket and msg_or_type in ['clear_output', 'stream', 'display_data']: |
|
50 | if stream is kernel.iopub_socket and msg_or_type in ['clear_output', 'stream', 'display_data']: | |
51 | msg = {'type': msg_or_type, 'args': args, 'kwargs': kwargs} |
|
51 | msg = {'type': msg_or_type, 'args': args, 'kwargs': kwargs} | |
52 | self.send(msg) |
|
52 | self.send(msg) | |
53 | else: |
|
53 | else: | |
54 | send(stream, msg_or_type, *args, **kwargs) |
|
54 | send(stream, msg_or_type, *args, **kwargs) | |
55 | return |
|
|||
56 |
|
55 | |||
57 | session.send = send_hook |
|
56 | session.send = send_hook | |
58 |
|
57 | |||
59 | def __exit__(self, exception_type, exception_value, traceback): |
|
58 | def __exit__(self, exception_type, exception_value, traceback): | |
60 | """Called upon exiting output widget context manager.""" |
|
59 | """Called upon exiting output widget context manager.""" | |
61 | self._flush() |
|
60 | self._flush() | |
62 | self._session.send = self._original_send |
|
61 | self._session.send = self._original_send | |
63 |
|
62 | |||
64 | def _flush(self): |
|
63 | def _flush(self): | |
65 | """Flush stdout and stderr buffers.""" |
|
64 | """Flush stdout and stderr buffers.""" | |
66 | sys.stdout.flush() |
|
65 | sys.stdout.flush() | |
67 | sys.stderr.flush() |
|
66 | sys.stderr.flush() |
General Comments 0
You need to be logged in to leave comments.
Login now