Show More
@@ -0,0 +1,80 | |||
|
1 | # Local imports. | |
|
2 | from frontend_widget import FrontendWidget | |
|
3 | ||
|
4 | ||
|
5 | class IPythonWidget(FrontendWidget): | |
|
6 | """ A FrontendWidget for an IPython kernel. | |
|
7 | """ | |
|
8 | ||
|
9 | #--------------------------------------------------------------------------- | |
|
10 | # 'FrontendWidget' interface | |
|
11 | #--------------------------------------------------------------------------- | |
|
12 | ||
|
13 | def __init__(self, kernel_manager, parent=None): | |
|
14 | super(IPythonWidget, self).__init__(kernel_manager, parent) | |
|
15 | ||
|
16 | self._magic_overrides = {} | |
|
17 | ||
|
18 | def execute_source(self, source, hidden=False, interactive=False): | |
|
19 | """ Reimplemented to override magic commands. | |
|
20 | """ | |
|
21 | magic_source = source.strip() | |
|
22 | if magic_source.startswith('%'): | |
|
23 | magic_source = magic_source[1:] | |
|
24 | magic, sep, arguments = magic_source.partition(' ') | |
|
25 | if not magic: | |
|
26 | magic = magic_source | |
|
27 | ||
|
28 | callback = self._magic_overrides.get(magic) | |
|
29 | if callback: | |
|
30 | output = callback(arguments) | |
|
31 | if output: | |
|
32 | self.appendPlainText(output) | |
|
33 | self._show_prompt('>>> ') | |
|
34 | return True | |
|
35 | else: | |
|
36 | return super(IPythonWidget, self).execute_source(source, hidden, | |
|
37 | interactive) | |
|
38 | ||
|
39 | #--------------------------------------------------------------------------- | |
|
40 | # 'IPythonWidget' interface | |
|
41 | #--------------------------------------------------------------------------- | |
|
42 | ||
|
43 | def set_magic_override(self, magic, callback): | |
|
44 | """ Overrides an IPython magic command. This magic will be intercepted | |
|
45 | by the frontend rather than passed on to the kernel and 'callback' | |
|
46 | will be called with a single argument: a string of argument(s) for | |
|
47 | the magic. The callback can (optionally) return text to print to the | |
|
48 | console. | |
|
49 | """ | |
|
50 | self._magic_overrides[magic] = callback | |
|
51 | ||
|
52 | def remove_magic_override(self, magic): | |
|
53 | """ Removes the override for the specified magic, if there is one. | |
|
54 | """ | |
|
55 | try: | |
|
56 | del self._magic_overrides[magic] | |
|
57 | except KeyError: | |
|
58 | pass | |
|
59 | ||
|
60 | ||
|
61 | if __name__ == '__main__': | |
|
62 | import sys | |
|
63 | from IPython.frontend.qt.kernelmanager import QtKernelManager | |
|
64 | ||
|
65 | # Create KernelManager | |
|
66 | xreq_addr = ('127.0.0.1', 5575) | |
|
67 | sub_addr = ('127.0.0.1', 5576) | |
|
68 | rep_addr = ('127.0.0.1', 5577) | |
|
69 | kernel_manager = QtKernelManager(xreq_addr, sub_addr, rep_addr) | |
|
70 | kernel_manager.sub_channel.start() | |
|
71 | kernel_manager.xreq_channel.start() | |
|
72 | ||
|
73 | # Launch application | |
|
74 | app = QtGui.QApplication(sys.argv) | |
|
75 | widget = IPythonWidget(kernel_manager) | |
|
76 | widget.setWindowTitle('Python') | |
|
77 | widget.resize(640, 480) | |
|
78 | widget.show() | |
|
79 | sys.exit(app.exec_()) | |
|
80 |
@@ -47,7 +47,7 class FrontendHighlighter(PygmentsHighlighter): | |||
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | class FrontendWidget(HistoryConsoleWidget): |
|
50 |
""" A Qt frontend for an |
|
|
50 | """ A Qt frontend for a generic Python kernel. | |
|
51 | 51 | """ |
|
52 | 52 | |
|
53 | 53 | # Emitted when an 'execute_reply' is received from the kernel. |
@@ -63,7 +63,7 class FrontendWidget(HistoryConsoleWidget): | |||
|
63 | 63 | self._call_tip_widget = CallTipWidget(self) |
|
64 | 64 | self._compile = CommandCompiler() |
|
65 | 65 | self._completion_lexer = CompletionLexer(PythonLexer()) |
|
66 |
self._hidden = |
|
|
66 | self._hidden = True | |
|
67 | 67 | self._highlighter = FrontendHighlighter(self) |
|
68 | 68 | self._kernel_manager = None |
|
69 | 69 | |
@@ -127,6 +127,7 class FrontendWidget(HistoryConsoleWidget): | |||
|
127 | 127 | shown. Returns whether the source executed (i.e., returns True only |
|
128 | 128 | if no more input is necessary). |
|
129 | 129 | """ |
|
130 | # Use CommandCompiler to determine if more input is needed. | |
|
130 | 131 | try: |
|
131 | 132 | code = self._compile(source, symbol='single') |
|
132 | 133 | except (OverflowError, SyntaxError, ValueError): |
@@ -281,7 +282,7 class FrontendWidget(HistoryConsoleWidget): | |||
|
281 | 282 | elif status == 'aborted': |
|
282 | 283 | text = "ERROR: ABORTED\n" |
|
283 | 284 | self.appendPlainText(text) |
|
284 |
self._hidden = |
|
|
285 | self._hidden = True | |
|
285 | 286 | self._show_prompt('>>> ') |
|
286 | 287 | self.executed.emit(rep) |
|
287 | 288 | |
@@ -300,25 +301,3 class FrontendWidget(HistoryConsoleWidget): | |||
|
300 | 301 | doc = rep['content']['docstring'] |
|
301 | 302 | if doc: |
|
302 | 303 | self._call_tip_widget.show_tip(doc) |
|
303 | ||
|
304 | ||
|
305 | if __name__ == '__main__': | |
|
306 | import sys | |
|
307 | from IPython.frontend.qt.kernelmanager import QtKernelManager | |
|
308 | ||
|
309 | # Create KernelManager | |
|
310 | xreq_addr = ('127.0.0.1', 5575) | |
|
311 | sub_addr = ('127.0.0.1', 5576) | |
|
312 | rep_addr = ('127.0.0.1', 5577) | |
|
313 | kernel_manager = QtKernelManager(xreq_addr, sub_addr, rep_addr) | |
|
314 | kernel_manager.sub_channel.start() | |
|
315 | kernel_manager.xreq_channel.start() | |
|
316 | ||
|
317 | # Launch application | |
|
318 | app = QtGui.QApplication(sys.argv) | |
|
319 | widget = FrontendWidget(kernel_manager) | |
|
320 | widget.setWindowTitle('Python') | |
|
321 | widget.resize(640, 480) | |
|
322 | widget.show() | |
|
323 | sys.exit(app.exec_()) | |
|
324 |
@@ -118,7 +118,7 class SubSocketChannel(ZmqSocketChannel): | |||
|
118 | 118 | self._flushed = False |
|
119 | 119 | self.ioloop.add_callback(self._flush) |
|
120 | 120 | while not self._flushed: |
|
121 | time.sleep(0) | |
|
121 | time.sleep(0.01) | |
|
122 | 122 | |
|
123 | 123 | def _flush(self): |
|
124 | 124 | """Called in this thread by the IOLoop to indicate that all events have |
General Comments 0
You need to be logged in to leave comments.
Login now