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