##// END OF EJS Templates
added more info to --ip in ipython-qtconsole -h
MinRK -
Show More
@@ -1,182 +1,186 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.Cancel, QtGui.QMessageBox.Ok
87 87 )
88 88 if reply == QtGui.QMessageBox.Ok:
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 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 112 kgroup.add_argument('--xreq', type=int, metavar='PORT', default=0,
109 113 help='set the XREQ channel port [default random]')
110 114 kgroup.add_argument('--sub', type=int, metavar='PORT', default=0,
111 115 help='set the SUB channel port [default random]')
112 116 kgroup.add_argument('--rep', type=int, metavar='PORT', default=0,
113 117 help='set the REP channel port [default random]')
114 118 kgroup.add_argument('--hb', type=int, metavar='PORT', default=0,
115 119 help='set the heartbeat port [default: random]')
116 120
117 121 egroup = kgroup.add_mutually_exclusive_group()
118 122 egroup.add_argument('--pure', action='store_true', help = \
119 123 'use a pure Python kernel instead of an IPython kernel')
120 124 egroup.add_argument('--pylab', type=str, metavar='GUI', nargs='?',
121 125 const='auto', help = \
122 126 "Pre-load matplotlib and numpy for interactive use. If GUI is not \
123 127 given, the GUI backend is matplotlib's, otherwise use one of: \
124 128 ['tk', 'gtk', 'qt', 'wx', 'inline'].")
125 129
126 130 wgroup = parser.add_argument_group('widget options')
127 131 wgroup.add_argument('--paging', type=str, default='inside',
128 132 choices = ['inside', 'hsplit', 'vsplit', 'none'],
129 133 help='set the paging style [default inside]')
130 134 wgroup.add_argument('--rich', action='store_true',
131 135 help='enable rich text support')
132 136 wgroup.add_argument('--gui-completion', action='store_true',
133 137 help='use a GUI widget for tab completion')
134 138
135 139 args = parser.parse_args()
136 140
137 141 # Don't let Qt or ZMQ swallow KeyboardInterupts.
138 142 import signal
139 143 signal.signal(signal.SIGINT, signal.SIG_DFL)
140 144
141 145 # Create a KernelManager and start a kernel.
142 146 kernel_manager = QtKernelManager(xreq_address=(args.ip, args.xreq),
143 147 sub_address=(args.ip, args.sub),
144 148 rep_address=(args.ip, args.rep),
145 149 hb_address=(args.ip, args.hb))
146 150 if not args.existing:
147 151 # if not args.ip in LOCAL_IPS+ALL_ALIAS:
148 152 # raise ValueError("Must bind a local ip, such as: %s"%LOCAL_IPS)
149 153
150 154 kwargs = dict(ip=args.ip)
151 155 if args.pure:
152 156 kwargs['ipython']=False
153 157 elif args.pylab:
154 158 kwargs['pylab']=args.pylab
155 159
156 160 kernel_manager.start_kernel(**kwargs)
157 161 kernel_manager.start_channels()
158 162
159 163 local_kernel = (not args.existing) or args.ip in LOCAL_IPS
160 164 # Create the widget.
161 165 app = QtGui.QApplication([])
162 166 if args.pure:
163 167 kind = 'rich' if args.rich else 'plain'
164 168 widget = FrontendWidget(kind=kind, paging=args.paging, local_kernel=local_kernel)
165 169 elif args.rich or args.pylab:
166 170 widget = RichIPythonWidget(paging=args.paging, local_kernel=local_kernel)
167 171 else:
168 172 widget = IPythonWidget(paging=args.paging, local_kernel=local_kernel)
169 173 widget.gui_completion = args.gui_completion
170 174 widget.kernel_manager = kernel_manager
171 175
172 176 # Create the main window.
173 177 window = MainWindow(app, widget, args.existing, may_close=local_kernel)
174 178 window.setWindowTitle('Python' if args.pure else 'IPython')
175 179 window.show()
176 180
177 181 # Start the application main loop.
178 182 app.exec_()
179 183
180 184
181 185 if __name__ == '__main__':
182 186 main()
General Comments 0
You need to be logged in to leave comments. Login now