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