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