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