Show More
@@ -1,32 +1,50 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 |
|
14 | |||
|
15 | @skip_doctest | |||
14 | class Output(DOMWidget): |
|
16 | class Output(DOMWidget): | |
15 | """Displays multiple widgets in a group.""" |
|
17 | """Widget used as a context manager to display output. | |
|
18 | ||||
|
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 | |||
|
21 | manager. Any output produced while in it's context will be captured and | |||
|
22 | displayed in it instead of the standard output area. | |||
|
23 | ||||
|
24 | Example | |||
|
25 | from IPython.html import widgets | |||
|
26 | from IPython.display import display | |||
|
27 | out = widgets.Output() | |||
|
28 | display(out) | |||
|
29 | ||||
|
30 | print('prints to output area') | |||
|
31 | ||||
|
32 | with out: | |||
|
33 | print('prints to output widget')""" | |||
16 | _view_name = Unicode('OutputView', sync=True) |
|
34 | _view_name = Unicode('OutputView', sync=True) | |
17 |
|
35 | |||
18 | def clear_output(self, *pargs, **kwargs): |
|
36 | def clear_output(self, *pargs, **kwargs): | |
19 | with self: |
|
37 | with self: | |
20 | clear_output(*pargs, **kwargs) |
|
38 | clear_output(*pargs, **kwargs) | |
21 |
|
39 | |||
22 | def __enter__(self): |
|
40 | def __enter__(self): | |
23 | self._flush() |
|
41 | self._flush() | |
24 | self.send({'method': 'push'}) |
|
42 | self.send({'method': 'push'}) | |
25 |
|
43 | |||
26 | def __exit__(self, exception_type, exception_value, traceback): |
|
44 | def __exit__(self, exception_type, exception_value, traceback): | |
27 | self._flush() |
|
45 | self._flush() | |
28 | self.send({'method': 'pop'}) |
|
46 | self.send({'method': 'pop'}) | |
29 |
|
47 | |||
30 | def _flush(self): |
|
48 | def _flush(self): | |
31 | sys.stdout.flush() |
|
49 | sys.stdout.flush() | |
32 | sys.stderr.flush() |
|
50 | sys.stderr.flush() |
General Comments 0
You need to be logged in to leave comments.
Login now