Show More
@@ -1,182 +1,186 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 | # Network Constants |
|
19 | # Network Constants | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS |
|
22 | from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS | |
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, may_close=True): |
|
34 | def __init__(self, app, frontend, existing=False, may_close=True): | |
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 |
|
37 | The app is passed as an argument to allow for different | |
38 | closing behavior depending on whether we are the Kernel's parent. |
|
38 | closing behavior depending on whether we are the Kernel's parent. | |
39 |
|
39 | |||
40 | If existing is True, then this Console does not own the Kernel. |
|
40 | If existing is True, then this Console does not own the Kernel. | |
41 |
|
41 | |||
42 | If may_close is True, then this Console is permitted to close the kernel |
|
42 | If may_close is True, then this Console is permitted to close the kernel | |
43 | """ |
|
43 | """ | |
44 | super(MainWindow, self).__init__() |
|
44 | super(MainWindow, self).__init__() | |
45 | self._app = app |
|
45 | self._app = app | |
46 | self._frontend = frontend |
|
46 | self._frontend = frontend | |
47 | self._existing = existing |
|
47 | self._existing = existing | |
48 | if existing: |
|
48 | if existing: | |
49 | self._may_close = may_close |
|
49 | self._may_close = may_close | |
50 | else: |
|
50 | else: | |
51 | self._may_close = True |
|
51 | self._may_close = True | |
52 | self._frontend.exit_requested.connect(self.close) |
|
52 | self._frontend.exit_requested.connect(self.close) | |
53 | self.setCentralWidget(frontend) |
|
53 | self.setCentralWidget(frontend) | |
54 |
|
54 | |||
55 | #--------------------------------------------------------------------------- |
|
55 | #--------------------------------------------------------------------------- | |
56 | # QWidget interface |
|
56 | # QWidget interface | |
57 | #--------------------------------------------------------------------------- |
|
57 | #--------------------------------------------------------------------------- | |
58 |
|
58 | |||
59 | def closeEvent(self, event): |
|
59 | def closeEvent(self, event): | |
60 | """ Reimplemented to prompt the user and close the kernel cleanly. |
|
60 | """ Reimplemented to prompt the user and close the kernel cleanly. | |
61 | """ |
|
61 | """ | |
62 | kernel_manager = self._frontend.kernel_manager |
|
62 | kernel_manager = self._frontend.kernel_manager | |
63 | if kernel_manager and kernel_manager.channels_running: |
|
63 | if kernel_manager and kernel_manager.channels_running: | |
64 | title = self.window().windowTitle() |
|
64 | title = self.window().windowTitle() | |
65 | if self._may_close: |
|
65 | if self._may_close: | |
66 | reply = QtGui.QMessageBox.question(self, title, |
|
66 | reply = QtGui.QMessageBox.question(self, title, | |
67 | "You are closing this Console window."+ |
|
67 | "You are closing this Console window."+ | |
68 | "\nWould you like to quit the Kernel and all attached Consoles as well?", |
|
68 | "\nWould you like to quit the Kernel and all attached Consoles as well?", | |
69 | 'Cancel', 'No, just this Console', 'Yes, quit everything') |
|
69 | 'Cancel', 'No, just this Console', 'Yes, quit everything') | |
70 | if reply == 2: # close All |
|
70 | if reply == 2: # close All | |
71 | kernel_manager.shutdown_kernel() |
|
71 | kernel_manager.shutdown_kernel() | |
72 | #kernel_manager.stop_channels() |
|
72 | #kernel_manager.stop_channels() | |
73 | event.accept() |
|
73 | event.accept() | |
74 | elif reply == 1: # close Console |
|
74 | elif reply == 1: # close Console | |
75 | if not self._existing: |
|
75 | if not self._existing: | |
76 | # I have the kernel: don't quit, just close the window |
|
76 | # I have the kernel: don't quit, just close the window | |
77 | self._app.setQuitOnLastWindowClosed(False) |
|
77 | self._app.setQuitOnLastWindowClosed(False) | |
78 | self.deleteLater() |
|
78 | self.deleteLater() | |
79 | event.accept() |
|
79 | event.accept() | |
80 | else: |
|
80 | else: | |
81 | event.ignore() |
|
81 | event.ignore() | |
82 | else: |
|
82 | else: | |
83 | reply = QtGui.QMessageBox.question(self, title, |
|
83 | reply = QtGui.QMessageBox.question(self, title, | |
84 | "Are you sure you want to close this Console?\n"+ |
|
84 | "Are you sure you want to close this Console?\n"+ | |
85 | "The Kernel and other Consoles will remain active.", |
|
85 | "The Kernel and other Consoles will remain active.", | |
86 | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Ok |
|
86 | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Ok | |
87 | ) |
|
87 | ) | |
88 | if reply == QtGui.QMessageBox.Ok: |
|
88 | if reply == QtGui.QMessageBox.Ok: | |
89 | event.accept() |
|
89 | event.accept() | |
90 | else: |
|
90 | else: | |
91 | event.ignore() |
|
91 | event.ignore() | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | #----------------------------------------------------------------------------- |
|
94 | #----------------------------------------------------------------------------- | |
95 | # Main entry point |
|
95 | # Main entry point | |
96 | #----------------------------------------------------------------------------- |
|
96 | #----------------------------------------------------------------------------- | |
97 |
|
97 | |||
98 | def main(): |
|
98 | def main(): | |
99 | """ Entry point for application. |
|
99 | """ Entry point for application. | |
100 | """ |
|
100 | """ | |
101 | # Parse command line arguments. |
|
101 | # Parse command line arguments. | |
102 | parser = ArgumentParser() |
|
102 | parser = ArgumentParser() | |
103 | kgroup = parser.add_argument_group('kernel options') |
|
103 | kgroup = parser.add_argument_group('kernel options') | |
104 | kgroup.add_argument('-e', '--existing', action='store_true', |
|
104 | kgroup.add_argument('-e', '--existing', action='store_true', | |
105 | help='connect to an existing kernel') |
|
105 | help='connect to an existing kernel') | |
106 | kgroup.add_argument('--ip', type=str, default=LOCALHOST, |
|
106 | kgroup.add_argument('--ip', type=str, default=LOCALHOST, | |
107 | help='set the kernel\'s IP address [default localhost]') |
|
107 | help=\ | |
|
108 | "set the kernel\'s IP address [default localhost].\ | |||
|
109 | If the IP address is something other than localhost, then \ | |||
|
110 | Consoles on other machines will be able to connect\ | |||
|
111 | to the Kernel, so be careful!") | |||
108 | kgroup.add_argument('--xreq', type=int, metavar='PORT', default=0, |
|
112 | kgroup.add_argument('--xreq', type=int, metavar='PORT', default=0, | |
109 | help='set the XREQ channel port [default random]') |
|
113 | help='set the XREQ channel port [default random]') | |
110 | kgroup.add_argument('--sub', type=int, metavar='PORT', default=0, |
|
114 | kgroup.add_argument('--sub', type=int, metavar='PORT', default=0, | |
111 | help='set the SUB channel port [default random]') |
|
115 | help='set the SUB channel port [default random]') | |
112 | kgroup.add_argument('--rep', type=int, metavar='PORT', default=0, |
|
116 | kgroup.add_argument('--rep', type=int, metavar='PORT', default=0, | |
113 | help='set the REP channel port [default random]') |
|
117 | help='set the REP channel port [default random]') | |
114 | kgroup.add_argument('--hb', type=int, metavar='PORT', default=0, |
|
118 | kgroup.add_argument('--hb', type=int, metavar='PORT', default=0, | |
115 | help='set the heartbeat port [default: random]') |
|
119 | help='set the heartbeat port [default: random]') | |
116 |
|
120 | |||
117 | egroup = kgroup.add_mutually_exclusive_group() |
|
121 | egroup = kgroup.add_mutually_exclusive_group() | |
118 | egroup.add_argument('--pure', action='store_true', help = \ |
|
122 | egroup.add_argument('--pure', action='store_true', help = \ | |
119 | 'use a pure Python kernel instead of an IPython kernel') |
|
123 | 'use a pure Python kernel instead of an IPython kernel') | |
120 | egroup.add_argument('--pylab', type=str, metavar='GUI', nargs='?', |
|
124 | egroup.add_argument('--pylab', type=str, metavar='GUI', nargs='?', | |
121 | const='auto', help = \ |
|
125 | const='auto', help = \ | |
122 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ |
|
126 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ | |
123 | given, the GUI backend is matplotlib's, otherwise use one of: \ |
|
127 | given, the GUI backend is matplotlib's, otherwise use one of: \ | |
124 | ['tk', 'gtk', 'qt', 'wx', 'inline'].") |
|
128 | ['tk', 'gtk', 'qt', 'wx', 'inline'].") | |
125 |
|
129 | |||
126 | wgroup = parser.add_argument_group('widget options') |
|
130 | wgroup = parser.add_argument_group('widget options') | |
127 | wgroup.add_argument('--paging', type=str, default='inside', |
|
131 | wgroup.add_argument('--paging', type=str, default='inside', | |
128 | choices = ['inside', 'hsplit', 'vsplit', 'none'], |
|
132 | choices = ['inside', 'hsplit', 'vsplit', 'none'], | |
129 | help='set the paging style [default inside]') |
|
133 | help='set the paging style [default inside]') | |
130 | wgroup.add_argument('--rich', action='store_true', |
|
134 | wgroup.add_argument('--rich', action='store_true', | |
131 | help='enable rich text support') |
|
135 | help='enable rich text support') | |
132 | wgroup.add_argument('--gui-completion', action='store_true', |
|
136 | wgroup.add_argument('--gui-completion', action='store_true', | |
133 | help='use a GUI widget for tab completion') |
|
137 | help='use a GUI widget for tab completion') | |
134 |
|
138 | |||
135 | args = parser.parse_args() |
|
139 | args = parser.parse_args() | |
136 |
|
140 | |||
137 | # Don't let Qt or ZMQ swallow KeyboardInterupts. |
|
141 | # Don't let Qt or ZMQ swallow KeyboardInterupts. | |
138 | import signal |
|
142 | import signal | |
139 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
|
143 | signal.signal(signal.SIGINT, signal.SIG_DFL) | |
140 |
|
144 | |||
141 | # Create a KernelManager and start a kernel. |
|
145 | # Create a KernelManager and start a kernel. | |
142 | kernel_manager = QtKernelManager(xreq_address=(args.ip, args.xreq), |
|
146 | kernel_manager = QtKernelManager(xreq_address=(args.ip, args.xreq), | |
143 | sub_address=(args.ip, args.sub), |
|
147 | sub_address=(args.ip, args.sub), | |
144 | rep_address=(args.ip, args.rep), |
|
148 | rep_address=(args.ip, args.rep), | |
145 | hb_address=(args.ip, args.hb)) |
|
149 | hb_address=(args.ip, args.hb)) | |
146 | if not args.existing: |
|
150 | if not args.existing: | |
147 | # if not args.ip in LOCAL_IPS+ALL_ALIAS: |
|
151 | # if not args.ip in LOCAL_IPS+ALL_ALIAS: | |
148 | # raise ValueError("Must bind a local ip, such as: %s"%LOCAL_IPS) |
|
152 | # raise ValueError("Must bind a local ip, such as: %s"%LOCAL_IPS) | |
149 |
|
153 | |||
150 | kwargs = dict(ip=args.ip) |
|
154 | kwargs = dict(ip=args.ip) | |
151 | if args.pure: |
|
155 | if args.pure: | |
152 | kwargs['ipython']=False |
|
156 | kwargs['ipython']=False | |
153 | elif args.pylab: |
|
157 | elif args.pylab: | |
154 | kwargs['pylab']=args.pylab |
|
158 | kwargs['pylab']=args.pylab | |
155 |
|
159 | |||
156 | kernel_manager.start_kernel(**kwargs) |
|
160 | kernel_manager.start_kernel(**kwargs) | |
157 | kernel_manager.start_channels() |
|
161 | kernel_manager.start_channels() | |
158 |
|
162 | |||
159 | local_kernel = (not args.existing) or args.ip in LOCAL_IPS |
|
163 | local_kernel = (not args.existing) or args.ip in LOCAL_IPS | |
160 | # Create the widget. |
|
164 | # Create the widget. | |
161 | app = QtGui.QApplication([]) |
|
165 | app = QtGui.QApplication([]) | |
162 | if args.pure: |
|
166 | if args.pure: | |
163 | kind = 'rich' if args.rich else 'plain' |
|
167 | kind = 'rich' if args.rich else 'plain' | |
164 | widget = FrontendWidget(kind=kind, paging=args.paging, local_kernel=local_kernel) |
|
168 | widget = FrontendWidget(kind=kind, paging=args.paging, local_kernel=local_kernel) | |
165 | elif args.rich or args.pylab: |
|
169 | elif args.rich or args.pylab: | |
166 | widget = RichIPythonWidget(paging=args.paging, local_kernel=local_kernel) |
|
170 | widget = RichIPythonWidget(paging=args.paging, local_kernel=local_kernel) | |
167 | else: |
|
171 | else: | |
168 | widget = IPythonWidget(paging=args.paging, local_kernel=local_kernel) |
|
172 | widget = IPythonWidget(paging=args.paging, local_kernel=local_kernel) | |
169 | widget.gui_completion = args.gui_completion |
|
173 | widget.gui_completion = args.gui_completion | |
170 | widget.kernel_manager = kernel_manager |
|
174 | widget.kernel_manager = kernel_manager | |
171 |
|
175 | |||
172 | # Create the main window. |
|
176 | # Create the main window. | |
173 | window = MainWindow(app, widget, args.existing, may_close=local_kernel) |
|
177 | window = MainWindow(app, widget, args.existing, may_close=local_kernel) | |
174 | window.setWindowTitle('Python' if args.pure else 'IPython') |
|
178 | window.setWindowTitle('Python' if args.pure else 'IPython') | |
175 | window.show() |
|
179 | window.show() | |
176 |
|
180 | |||
177 | # Start the application main loop. |
|
181 | # Start the application main loop. | |
178 | app.exec_() |
|
182 | app.exec_() | |
179 |
|
183 | |||
180 |
|
184 | |||
181 | if __name__ == '__main__': |
|
185 | if __name__ == '__main__': | |
182 | main() |
|
186 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now