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