Show More
@@ -1,159 +1,158 b'' | |||||
1 | """ A minimal application using the Qt console-style IPython frontend. |
|
1 | """ A minimal application using the Qt console-style IPython frontend. | |
2 | """ |
|
2 | """ | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Imports |
|
5 | # Imports | |
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | # Systemm library imports |
|
8 | # Systemm library imports | |
9 | from PyQt4 import QtGui |
|
9 | from PyQt4 import QtGui | |
10 |
|
10 | |||
11 | # Local imports |
|
11 | # Local imports | |
12 | from IPython.external.argparse import ArgumentParser |
|
12 | from IPython.external.argparse import ArgumentParser | |
13 | from IPython.frontend.qt.console.frontend_widget import FrontendWidget |
|
13 | from IPython.frontend.qt.console.frontend_widget import FrontendWidget | |
14 | from IPython.frontend.qt.console.ipython_widget import IPythonWidget |
|
14 | from IPython.frontend.qt.console.ipython_widget import IPythonWidget | |
15 | from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget |
|
15 | from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget | |
16 | from IPython.frontend.qt.kernelmanager import QtKernelManager |
|
16 | from IPython.frontend.qt.kernelmanager import QtKernelManager | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Constants |
|
19 | # Constants | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | LOCALHOST = '127.0.0.1' |
|
22 | LOCALHOST = '127.0.0.1' | |
23 |
|
23 | |||
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | # Classes |
|
25 | # Classes | |
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 |
|
27 | |||
28 | class MainWindow(QtGui.QMainWindow): |
|
28 | class MainWindow(QtGui.QMainWindow): | |
29 |
|
29 | |||
30 | #--------------------------------------------------------------------------- |
|
30 | #--------------------------------------------------------------------------- | |
31 | # 'object' interface |
|
31 | # 'object' interface | |
32 | #--------------------------------------------------------------------------- |
|
32 | #--------------------------------------------------------------------------- | |
33 |
|
33 | |||
34 | def __init__(self, app, frontend, existing=False): |
|
34 | def __init__(self, app, frontend, existing=False): | |
35 | """ Create a MainWindow for the specified FrontendWidget. |
|
35 | """ Create a MainWindow for the specified FrontendWidget. | |
36 |
|
36 | |||
|
37 | The app is passed as an argument to allow for different | |||
|
38 | closing behavior depending on whether we are the Kernel's parent. | |||
|
39 | ||||
37 | If existing is True, then this Window does not own the Kernel. |
|
40 | If existing is True, then this Window does not own the Kernel. | |
38 | """ |
|
41 | """ | |
39 | super(MainWindow, self).__init__() |
|
42 | super(MainWindow, self).__init__() | |
40 | self._app = app |
|
43 | self._app = app | |
41 | self._frontend = frontend |
|
44 | self._frontend = frontend | |
42 | self._existing = existing |
|
45 | self._existing = existing | |
43 | self._frontend.exit_requested.connect(self.close) |
|
46 | self._frontend.exit_requested.connect(self.close) | |
44 | self.setCentralWidget(frontend) |
|
47 | self.setCentralWidget(frontend) | |
45 |
|
48 | |||
46 | #--------------------------------------------------------------------------- |
|
49 | #--------------------------------------------------------------------------- | |
47 | # QWidget interface |
|
50 | # QWidget interface | |
48 | #--------------------------------------------------------------------------- |
|
51 | #--------------------------------------------------------------------------- | |
49 |
|
52 | |||
50 | def closeEvent(self, event): |
|
53 | def closeEvent(self, event): | |
51 | """ Reimplemented to prompt the user and close the kernel cleanly. |
|
54 | """ Reimplemented to prompt the user and close the kernel cleanly. | |
52 | """ |
|
55 | """ | |
53 | kernel_manager = self._frontend.kernel_manager |
|
56 | kernel_manager = self._frontend.kernel_manager | |
54 | if kernel_manager and kernel_manager.channels_running: |
|
57 | if kernel_manager and kernel_manager.channels_running: | |
55 | title = self.window().windowTitle() |
|
58 | title = self.window().windowTitle() | |
56 | reply = QtGui.QMessageBox.question(self, title, |
|
59 | reply = QtGui.QMessageBox.question(self, title, | |
57 | "Close just this console, or shutdown the kernel and close "+ |
|
60 | "Close just this console, or shutdown the kernel and close "+ | |
58 | "all windows attached to it?", |
|
61 | "all windows attached to it?", | |
59 | 'Cancel', 'Close Console', 'Close All') |
|
62 | 'Cancel', 'Close Console', 'Close All') | |
60 | if reply == 2: |
|
63 | if reply == 2: # close All | |
61 | kernel_manager.shutdown_kernel() |
|
64 | kernel_manager.shutdown_kernel() | |
62 | #kernel_manager.stop_channels() |
|
65 | #kernel_manager.stop_channels() | |
63 | event.accept() |
|
66 | event.accept() | |
64 | elif reply == 1: |
|
67 | elif reply == 1: # close Console | |
65 | if self._existing: |
|
68 | if not self._existing: | |
66 |
# I |
|
69 | # I have the kernel: don't quit, just close the window | |
67 | event.accept() |
|
|||
68 | else: |
|
|||
69 | # only destroy the Window, save the Kernel |
|
|||
70 | self._app.setQuitOnLastWindowClosed(False) |
|
70 | self._app.setQuitOnLastWindowClosed(False) | |
71 |
|
|
71 | event.accept() | |
72 | event.ignore() |
|
|||
73 | else: |
|
72 | else: | |
74 | event.ignore() |
|
73 | event.ignore() | |
75 |
|
74 | |||
76 | #----------------------------------------------------------------------------- |
|
75 | #----------------------------------------------------------------------------- | |
77 | # Main entry point |
|
76 | # Main entry point | |
78 | #----------------------------------------------------------------------------- |
|
77 | #----------------------------------------------------------------------------- | |
79 |
|
78 | |||
80 | def main(): |
|
79 | def main(): | |
81 | """ Entry point for application. |
|
80 | """ Entry point for application. | |
82 | """ |
|
81 | """ | |
83 | # Parse command line arguments. |
|
82 | # Parse command line arguments. | |
84 | parser = ArgumentParser() |
|
83 | parser = ArgumentParser() | |
85 | kgroup = parser.add_argument_group('kernel options') |
|
84 | kgroup = parser.add_argument_group('kernel options') | |
86 | kgroup.add_argument('-e', '--existing', action='store_true', |
|
85 | kgroup.add_argument('-e', '--existing', action='store_true', | |
87 | help='connect to an existing kernel') |
|
86 | help='connect to an existing kernel') | |
88 | kgroup.add_argument('--ip', type=str, default=LOCALHOST, |
|
87 | kgroup.add_argument('--ip', type=str, default=LOCALHOST, | |
89 | help='set the kernel\'s IP address [default localhost]') |
|
88 | help='set the kernel\'s IP address [default localhost]') | |
90 | kgroup.add_argument('--xreq', type=int, metavar='PORT', default=0, |
|
89 | kgroup.add_argument('--xreq', type=int, metavar='PORT', default=0, | |
91 | help='set the XREQ channel port [default random]') |
|
90 | help='set the XREQ channel port [default random]') | |
92 | kgroup.add_argument('--sub', type=int, metavar='PORT', default=0, |
|
91 | kgroup.add_argument('--sub', type=int, metavar='PORT', default=0, | |
93 | help='set the SUB channel port [default random]') |
|
92 | help='set the SUB channel port [default random]') | |
94 | kgroup.add_argument('--rep', type=int, metavar='PORT', default=0, |
|
93 | kgroup.add_argument('--rep', type=int, metavar='PORT', default=0, | |
95 | help='set the REP channel port [default random]') |
|
94 | help='set the REP channel port [default random]') | |
96 | kgroup.add_argument('--hb', type=int, metavar='PORT', default=0, |
|
95 | kgroup.add_argument('--hb', type=int, metavar='PORT', default=0, | |
97 | help='set the heartbeat port [default: random]') |
|
96 | help='set the heartbeat port [default: random]') | |
98 |
|
97 | |||
99 | egroup = kgroup.add_mutually_exclusive_group() |
|
98 | egroup = kgroup.add_mutually_exclusive_group() | |
100 | egroup.add_argument('--pure', action='store_true', help = \ |
|
99 | egroup.add_argument('--pure', action='store_true', help = \ | |
101 | 'use a pure Python kernel instead of an IPython kernel') |
|
100 | 'use a pure Python kernel instead of an IPython kernel') | |
102 | egroup.add_argument('--pylab', type=str, metavar='GUI', nargs='?', |
|
101 | egroup.add_argument('--pylab', type=str, metavar='GUI', nargs='?', | |
103 | const='auto', help = \ |
|
102 | const='auto', help = \ | |
104 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ |
|
103 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ | |
105 | given, the GUI backend is matplotlib's, otherwise use one of: \ |
|
104 | given, the GUI backend is matplotlib's, otherwise use one of: \ | |
106 | ['tk', 'gtk', 'qt', 'wx', 'inline'].") |
|
105 | ['tk', 'gtk', 'qt', 'wx', 'inline'].") | |
107 |
|
106 | |||
108 | wgroup = parser.add_argument_group('widget options') |
|
107 | wgroup = parser.add_argument_group('widget options') | |
109 | wgroup.add_argument('--paging', type=str, default='inside', |
|
108 | wgroup.add_argument('--paging', type=str, default='inside', | |
110 | choices = ['inside', 'hsplit', 'vsplit', 'none'], |
|
109 | choices = ['inside', 'hsplit', 'vsplit', 'none'], | |
111 | help='set the paging style [default inside]') |
|
110 | help='set the paging style [default inside]') | |
112 | wgroup.add_argument('--rich', action='store_true', |
|
111 | wgroup.add_argument('--rich', action='store_true', | |
113 | help='enable rich text support') |
|
112 | help='enable rich text support') | |
114 | wgroup.add_argument('--gui-completion', action='store_true', |
|
113 | wgroup.add_argument('--gui-completion', action='store_true', | |
115 | help='use a GUI widget for tab completion') |
|
114 | help='use a GUI widget for tab completion') | |
116 |
|
115 | |||
117 | args = parser.parse_args() |
|
116 | args = parser.parse_args() | |
118 |
|
117 | |||
119 | # Don't let Qt or ZMQ swallow KeyboardInterupts. |
|
118 | # Don't let Qt or ZMQ swallow KeyboardInterupts. | |
120 | import signal |
|
119 | import signal | |
121 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
|
120 | signal.signal(signal.SIGINT, signal.SIG_DFL) | |
122 |
|
121 | |||
123 | # Create a KernelManager and start a kernel. |
|
122 | # Create a KernelManager and start a kernel. | |
124 | kernel_manager = QtKernelManager(xreq_address=(args.ip, args.xreq), |
|
123 | kernel_manager = QtKernelManager(xreq_address=(args.ip, args.xreq), | |
125 | sub_address=(args.ip, args.sub), |
|
124 | sub_address=(args.ip, args.sub), | |
126 | rep_address=(args.ip, args.rep), |
|
125 | rep_address=(args.ip, args.rep), | |
127 | hb_address=(args.ip, args.hb)) |
|
126 | hb_address=(args.ip, args.hb)) | |
128 | if args.ip == LOCALHOST and not args.existing: |
|
127 | if args.ip == LOCALHOST and not args.existing: | |
129 | if args.pure: |
|
128 | if args.pure: | |
130 | kernel_manager.start_kernel(ipython=False) |
|
129 | kernel_manager.start_kernel(ipython=False) | |
131 | elif args.pylab: |
|
130 | elif args.pylab: | |
132 | kernel_manager.start_kernel(pylab=args.pylab) |
|
131 | kernel_manager.start_kernel(pylab=args.pylab) | |
133 | else: |
|
132 | else: | |
134 | kernel_manager.start_kernel() |
|
133 | kernel_manager.start_kernel() | |
135 | kernel_manager.start_channels() |
|
134 | kernel_manager.start_channels() | |
136 |
|
135 | |||
137 | # Create the widget. |
|
136 | # Create the widget. | |
138 | app = QtGui.QApplication([]) |
|
137 | app = QtGui.QApplication([]) | |
139 | if args.pure: |
|
138 | if args.pure: | |
140 | kind = 'rich' if args.rich else 'plain' |
|
139 | kind = 'rich' if args.rich else 'plain' | |
141 | widget = FrontendWidget(kind=kind, paging=args.paging) |
|
140 | widget = FrontendWidget(kind=kind, paging=args.paging) | |
142 | elif args.rich or args.pylab: |
|
141 | elif args.rich or args.pylab: | |
143 | widget = RichIPythonWidget(paging=args.paging) |
|
142 | widget = RichIPythonWidget(paging=args.paging) | |
144 | else: |
|
143 | else: | |
145 | widget = IPythonWidget(paging=args.paging) |
|
144 | widget = IPythonWidget(paging=args.paging) | |
146 | widget.gui_completion = args.gui_completion |
|
145 | widget.gui_completion = args.gui_completion | |
147 | widget.kernel_manager = kernel_manager |
|
146 | widget.kernel_manager = kernel_manager | |
148 |
|
147 | |||
149 | # Create the main window. |
|
148 | # Create the main window. | |
150 | window = MainWindow(app, widget, args.existing) |
|
149 | window = MainWindow(app, widget, args.existing) | |
151 | window.setWindowTitle('Python' if args.pure else 'IPython') |
|
150 | window.setWindowTitle('Python' if args.pure else 'IPython') | |
152 | window.show() |
|
151 | window.show() | |
153 |
|
152 | |||
154 | # Start the application main loop. |
|
153 | # Start the application main loop. | |
155 | app.exec_() |
|
154 | app.exec_() | |
156 |
|
155 | |||
157 |
|
156 | |||
158 | if __name__ == '__main__': |
|
157 | if __name__ == '__main__': | |
159 | main() |
|
158 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now