Show More
@@ -1,434 +1,442 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 | This is not a complete console app, as subprocess will not be able to receive |
|
3 | This is not a complete console app, as subprocess will not be able to receive | |
4 | input, there is no real readline support, among other limitations. |
|
4 | input, there is no real readline support, among other limitations. | |
5 |
|
5 | |||
6 | Authors: |
|
6 | Authors: | |
7 |
|
7 | |||
8 | * Evan Patterson |
|
8 | * Evan Patterson | |
9 | * Min RK |
|
9 | * Min RK | |
10 | * Erik Tollerud |
|
10 | * Erik Tollerud | |
11 | * Fernando Perez |
|
11 | * Fernando Perez | |
12 |
|
12 | |||
13 | """ |
|
13 | """ | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | # stdlib imports |
|
19 | # stdlib imports | |
20 | import os |
|
20 | import os | |
21 | import signal |
|
21 | import signal | |
22 | import sys |
|
22 | import sys | |
23 |
|
23 | |||
24 | # System library imports |
|
24 | # System library imports | |
25 | from IPython.external.qt import QtGui |
|
25 | from IPython.external.qt import QtGui | |
26 | from pygments.styles import get_all_styles |
|
26 | from pygments.styles import get_all_styles | |
27 |
|
27 | |||
28 | # Local imports |
|
28 | # Local imports | |
29 | from IPython.config.application import boolean_flag |
|
29 | from IPython.config.application import boolean_flag | |
30 | from IPython.core.application import BaseIPythonApplication |
|
30 | from IPython.core.application import BaseIPythonApplication | |
31 | from IPython.core.profiledir import ProfileDir |
|
31 | from IPython.core.profiledir import ProfileDir | |
32 | from IPython.frontend.qt.console.frontend_widget import FrontendWidget |
|
32 | from IPython.frontend.qt.console.frontend_widget import FrontendWidget | |
33 | from IPython.frontend.qt.console.ipython_widget import IPythonWidget |
|
33 | from IPython.frontend.qt.console.ipython_widget import IPythonWidget | |
34 | from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget |
|
34 | from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget | |
35 | from IPython.frontend.qt.console import styles |
|
35 | from IPython.frontend.qt.console import styles | |
36 | from IPython.frontend.qt.kernelmanager import QtKernelManager |
|
36 | from IPython.frontend.qt.kernelmanager import QtKernelManager | |
37 | from IPython.utils.traitlets import ( |
|
37 | from IPython.utils.traitlets import ( | |
38 | Dict, List, Unicode, Int, CaselessStrEnum, CBool, Any |
|
38 | Dict, List, Unicode, Int, CaselessStrEnum, CBool, Any | |
39 | ) |
|
39 | ) | |
40 | from IPython.zmq.ipkernel import ( |
|
40 | from IPython.zmq.ipkernel import ( | |
41 | flags as ipkernel_flags, |
|
41 | flags as ipkernel_flags, | |
42 | aliases as ipkernel_aliases, |
|
42 | aliases as ipkernel_aliases, | |
43 | IPKernelApp |
|
43 | IPKernelApp | |
44 | ) |
|
44 | ) | |
45 | from IPython.zmq.session import Session |
|
45 | from IPython.zmq.session import Session | |
46 | from IPython.zmq.zmqshell import ZMQInteractiveShell |
|
46 | from IPython.zmq.zmqshell import ZMQInteractiveShell | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | #----------------------------------------------------------------------------- |
|
49 | #----------------------------------------------------------------------------- | |
50 | # Network Constants |
|
50 | # Network Constants | |
51 | #----------------------------------------------------------------------------- |
|
51 | #----------------------------------------------------------------------------- | |
52 |
|
52 | |||
53 | from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS |
|
53 | from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS | |
54 |
|
54 | |||
55 | #----------------------------------------------------------------------------- |
|
55 | #----------------------------------------------------------------------------- | |
56 | # Globals |
|
56 | # Globals | |
57 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
58 |
|
58 | |||
59 | _examples = """ |
|
59 | _examples = """ | |
60 | ipython qtconsole # start the qtconsole |
|
60 | ipython qtconsole # start the qtconsole | |
61 | ipython qtconsole --pylab=inline # start with pylab in inline plotting mode |
|
61 | ipython qtconsole --pylab=inline # start with pylab in inline plotting mode | |
62 | """ |
|
62 | """ | |
63 |
|
63 | |||
64 | #----------------------------------------------------------------------------- |
|
64 | #----------------------------------------------------------------------------- | |
65 | # Classes |
|
65 | # Classes | |
66 | #----------------------------------------------------------------------------- |
|
66 | #----------------------------------------------------------------------------- | |
67 |
|
67 | |||
68 | class MainWindow(QtGui.QMainWindow): |
|
68 | class MainWindow(QtGui.QMainWindow): | |
69 |
|
69 | |||
70 | #--------------------------------------------------------------------------- |
|
70 | #--------------------------------------------------------------------------- | |
71 | # 'object' interface |
|
71 | # 'object' interface | |
72 | #--------------------------------------------------------------------------- |
|
72 | #--------------------------------------------------------------------------- | |
73 |
|
73 | |||
74 | def __init__(self, app, frontend, existing=False, may_close=True, |
|
74 | def __init__(self, app, frontend, existing=False, may_close=True, | |
75 | confirm_exit=True): |
|
75 | confirm_exit=True): | |
76 | """ Create a MainWindow for the specified FrontendWidget. |
|
76 | """ Create a MainWindow for the specified FrontendWidget. | |
77 |
|
77 | |||
78 | The app is passed as an argument to allow for different |
|
78 | The app is passed as an argument to allow for different | |
79 | closing behavior depending on whether we are the Kernel's parent. |
|
79 | closing behavior depending on whether we are the Kernel's parent. | |
80 |
|
80 | |||
81 | If existing is True, then this Console does not own the Kernel. |
|
81 | If existing is True, then this Console does not own the Kernel. | |
82 |
|
82 | |||
83 | If may_close is True, then this Console is permitted to close the kernel |
|
83 | If may_close is True, then this Console is permitted to close the kernel | |
84 | """ |
|
84 | """ | |
85 | super(MainWindow, self).__init__() |
|
85 | super(MainWindow, self).__init__() | |
86 | self._app = app |
|
86 | self._app = app | |
87 | self._frontend = frontend |
|
87 | self._frontend = frontend | |
88 | self._existing = existing |
|
88 | self._existing = existing | |
89 | if existing: |
|
89 | if existing: | |
90 | self._may_close = may_close |
|
90 | self._may_close = may_close | |
91 | else: |
|
91 | else: | |
92 | self._may_close = True |
|
92 | self._may_close = True | |
93 | self._frontend.exit_requested.connect(self.close) |
|
93 | self._frontend.exit_requested.connect(self.close) | |
94 | self._confirm_exit = confirm_exit |
|
94 | self._confirm_exit = confirm_exit | |
95 | self.setCentralWidget(frontend) |
|
95 | self.setCentralWidget(frontend) | |
96 |
|
96 | |||
97 | #--------------------------------------------------------------------------- |
|
97 | #--------------------------------------------------------------------------- | |
98 | # QWidget interface |
|
98 | # QWidget interface | |
99 | #--------------------------------------------------------------------------- |
|
99 | #--------------------------------------------------------------------------- | |
100 |
|
100 | |||
101 | def closeEvent(self, event): |
|
101 | def closeEvent(self, event): | |
102 | """ Close the window and the kernel (if necessary). |
|
102 | """ Close the window and the kernel (if necessary). | |
103 |
|
103 | |||
104 | This will prompt the user if they are finished with the kernel, and if |
|
104 | This will prompt the user if they are finished with the kernel, and if | |
105 | so, closes the kernel cleanly. Alternatively, if the exit magic is used, |
|
105 | so, closes the kernel cleanly. Alternatively, if the exit magic is used, | |
106 | it closes without prompt. |
|
106 | it closes without prompt. | |
107 | """ |
|
107 | """ | |
108 | keepkernel = None #Use the prompt by default |
|
108 | keepkernel = None #Use the prompt by default | |
109 | if hasattr(self._frontend,'_keep_kernel_on_exit'): #set by exit magic |
|
109 | if hasattr(self._frontend,'_keep_kernel_on_exit'): #set by exit magic | |
110 | keepkernel = self._frontend._keep_kernel_on_exit |
|
110 | keepkernel = self._frontend._keep_kernel_on_exit | |
111 |
|
111 | |||
112 | kernel_manager = self._frontend.kernel_manager |
|
112 | kernel_manager = self._frontend.kernel_manager | |
113 |
|
113 | |||
114 | if keepkernel is None and not self._confirm_exit: |
|
114 | if keepkernel is None and not self._confirm_exit: | |
115 | # don't prompt, just terminate the kernel if we own it |
|
115 | # don't prompt, just terminate the kernel if we own it | |
116 | # or leave it alone if we don't |
|
116 | # or leave it alone if we don't | |
117 | keepkernel = not self._existing |
|
117 | keepkernel = not self._existing | |
118 |
|
118 | |||
119 | if keepkernel is None: #show prompt |
|
119 | if keepkernel is None: #show prompt | |
120 | if kernel_manager and kernel_manager.channels_running: |
|
120 | if kernel_manager and kernel_manager.channels_running: | |
121 | title = self.window().windowTitle() |
|
121 | title = self.window().windowTitle() | |
122 | cancel = QtGui.QMessageBox.Cancel |
|
122 | cancel = QtGui.QMessageBox.Cancel | |
123 | okay = QtGui.QMessageBox.Ok |
|
123 | okay = QtGui.QMessageBox.Ok | |
124 | if self._may_close: |
|
124 | if self._may_close: | |
125 | msg = "You are closing this Console window." |
|
125 | msg = "You are closing this Console window." | |
126 | info = "Would you like to quit the Kernel and all attached Consoles as well?" |
|
126 | info = "Would you like to quit the Kernel and all attached Consoles as well?" | |
127 | justthis = QtGui.QPushButton("&No, just this Console", self) |
|
127 | justthis = QtGui.QPushButton("&No, just this Console", self) | |
128 | justthis.setShortcut('N') |
|
128 | justthis.setShortcut('N') | |
129 | closeall = QtGui.QPushButton("&Yes, quit everything", self) |
|
129 | closeall = QtGui.QPushButton("&Yes, quit everything", self) | |
130 | closeall.setShortcut('Y') |
|
130 | closeall.setShortcut('Y') | |
131 | box = QtGui.QMessageBox(QtGui.QMessageBox.Question, |
|
131 | box = QtGui.QMessageBox(QtGui.QMessageBox.Question, | |
132 | title, msg) |
|
132 | title, msg) | |
133 | box.setInformativeText(info) |
|
133 | box.setInformativeText(info) | |
134 | box.addButton(cancel) |
|
134 | box.addButton(cancel) | |
135 | box.addButton(justthis, QtGui.QMessageBox.NoRole) |
|
135 | box.addButton(justthis, QtGui.QMessageBox.NoRole) | |
136 | box.addButton(closeall, QtGui.QMessageBox.YesRole) |
|
136 | box.addButton(closeall, QtGui.QMessageBox.YesRole) | |
137 | box.setDefaultButton(closeall) |
|
137 | box.setDefaultButton(closeall) | |
138 | box.setEscapeButton(cancel) |
|
138 | box.setEscapeButton(cancel) | |
139 | reply = box.exec_() |
|
139 | reply = box.exec_() | |
140 | if reply == 1: # close All |
|
140 | if reply == 1: # close All | |
141 | kernel_manager.shutdown_kernel() |
|
141 | kernel_manager.shutdown_kernel() | |
142 | #kernel_manager.stop_channels() |
|
142 | #kernel_manager.stop_channels() | |
143 | event.accept() |
|
143 | event.accept() | |
144 | elif reply == 0: # close Console |
|
144 | elif reply == 0: # close Console | |
145 | if not self._existing: |
|
145 | if not self._existing: | |
146 | # Have kernel: don't quit, just close the window |
|
146 | # Have kernel: don't quit, just close the window | |
147 | self._app.setQuitOnLastWindowClosed(False) |
|
147 | self._app.setQuitOnLastWindowClosed(False) | |
148 | self.deleteLater() |
|
148 | self.deleteLater() | |
149 | event.accept() |
|
149 | event.accept() | |
150 | else: |
|
150 | else: | |
151 | event.ignore() |
|
151 | event.ignore() | |
152 | else: |
|
152 | else: | |
153 | reply = QtGui.QMessageBox.question(self, title, |
|
153 | reply = QtGui.QMessageBox.question(self, title, | |
154 | "Are you sure you want to close this Console?"+ |
|
154 | "Are you sure you want to close this Console?"+ | |
155 | "\nThe Kernel and other Consoles will remain active.", |
|
155 | "\nThe Kernel and other Consoles will remain active.", | |
156 | okay|cancel, |
|
156 | okay|cancel, | |
157 | defaultButton=okay |
|
157 | defaultButton=okay | |
158 | ) |
|
158 | ) | |
159 | if reply == okay: |
|
159 | if reply == okay: | |
160 | event.accept() |
|
160 | event.accept() | |
161 | else: |
|
161 | else: | |
162 | event.ignore() |
|
162 | event.ignore() | |
163 | elif keepkernel: #close console but leave kernel running (no prompt) |
|
163 | elif keepkernel: #close console but leave kernel running (no prompt) | |
164 | if kernel_manager and kernel_manager.channels_running: |
|
164 | if kernel_manager and kernel_manager.channels_running: | |
165 | if not self._existing: |
|
165 | if not self._existing: | |
166 | # I have the kernel: don't quit, just close the window |
|
166 | # I have the kernel: don't quit, just close the window | |
167 | self._app.setQuitOnLastWindowClosed(False) |
|
167 | self._app.setQuitOnLastWindowClosed(False) | |
168 | event.accept() |
|
168 | event.accept() | |
169 | else: #close console and kernel (no prompt) |
|
169 | else: #close console and kernel (no prompt) | |
170 | if kernel_manager and kernel_manager.channels_running: |
|
170 | if kernel_manager and kernel_manager.channels_running: | |
171 | kernel_manager.shutdown_kernel() |
|
171 | kernel_manager.shutdown_kernel() | |
172 | event.accept() |
|
172 | event.accept() | |
173 |
|
173 | |||
174 | #----------------------------------------------------------------------------- |
|
174 | #----------------------------------------------------------------------------- | |
175 | # Aliases and Flags |
|
175 | # Aliases and Flags | |
176 | #----------------------------------------------------------------------------- |
|
176 | #----------------------------------------------------------------------------- | |
177 |
|
177 | |||
178 | flags = dict(ipkernel_flags) |
|
178 | flags = dict(ipkernel_flags) | |
179 |
|
179 | |||
180 | flags.update({ |
|
180 | flags.update({ | |
181 | 'existing' : ({'IPythonQtConsoleApp' : {'existing' : True}}, |
|
181 | 'existing' : ({'IPythonQtConsoleApp' : {'existing' : True}}, | |
182 | "Connect to an existing kernel."), |
|
182 | "Connect to an existing kernel."), | |
183 | 'pure' : ({'IPythonQtConsoleApp' : {'pure' : True}}, |
|
183 | 'pure' : ({'IPythonQtConsoleApp' : {'pure' : True}}, | |
184 | "Use a pure Python kernel instead of an IPython kernel."), |
|
184 | "Use a pure Python kernel instead of an IPython kernel."), | |
185 | 'plain' : ({'ConsoleWidget' : {'kind' : 'plain'}}, |
|
185 | 'plain' : ({'ConsoleWidget' : {'kind' : 'plain'}}, | |
186 | "Disable rich text support."), |
|
186 | "Disable rich text support."), | |
187 | }) |
|
187 | }) | |
188 | flags.update(boolean_flag( |
|
188 | flags.update(boolean_flag( | |
189 | 'gui-completion', 'ConsoleWidget.gui_completion', |
|
189 | 'gui-completion', 'ConsoleWidget.gui_completion', | |
190 | "use a GUI widget for tab completion", |
|
190 | "use a GUI widget for tab completion", | |
191 | "use plaintext output for completion" |
|
191 | "use plaintext output for completion" | |
192 | )) |
|
192 | )) | |
193 | flags.update(boolean_flag( |
|
193 | flags.update(boolean_flag( | |
194 | 'confirm-exit', 'IPythonQtConsoleApp.confirm_exit', |
|
194 | 'confirm-exit', 'IPythonQtConsoleApp.confirm_exit', | |
195 | """Set to display confirmation dialog on exit. You can always use 'exit' or 'quit', |
|
195 | """Set to display confirmation dialog on exit. You can always use 'exit' or 'quit', | |
196 | to force a direct exit without any confirmation. |
|
196 | to force a direct exit without any confirmation. | |
197 | """, |
|
197 | """, | |
198 | """Don't prompt the user when exiting. This will terminate the kernel |
|
198 | """Don't prompt the user when exiting. This will terminate the kernel | |
199 | if it is owned by the frontend, and leave it alive if it is external. |
|
199 | if it is owned by the frontend, and leave it alive if it is external. | |
200 | """ |
|
200 | """ | |
201 | )) |
|
201 | )) | |
202 | # the flags that are specific to the frontend |
|
202 | # the flags that are specific to the frontend | |
203 | # these must be scrubbed before being passed to the kernel, |
|
203 | # these must be scrubbed before being passed to the kernel, | |
204 | # or it will raise an error on unrecognized flags |
|
204 | # or it will raise an error on unrecognized flags | |
205 | qt_flags = ['existing', 'pure', 'plain', 'gui-completion', 'no-gui-completion', |
|
205 | qt_flags = ['existing', 'pure', 'plain', 'gui-completion', 'no-gui-completion', | |
206 | 'confirm-exit', 'no-confirm-exit'] |
|
206 | 'confirm-exit', 'no-confirm-exit'] | |
207 |
|
207 | |||
208 | aliases = dict(ipkernel_aliases) |
|
208 | aliases = dict(ipkernel_aliases) | |
209 |
|
209 | |||
210 | aliases.update(dict( |
|
210 | aliases.update(dict( | |
211 | hb = 'IPythonQtConsoleApp.hb_port', |
|
211 | hb = 'IPythonQtConsoleApp.hb_port', | |
212 | shell = 'IPythonQtConsoleApp.shell_port', |
|
212 | shell = 'IPythonQtConsoleApp.shell_port', | |
213 | iopub = 'IPythonQtConsoleApp.iopub_port', |
|
213 | iopub = 'IPythonQtConsoleApp.iopub_port', | |
214 | stdin = 'IPythonQtConsoleApp.stdin_port', |
|
214 | stdin = 'IPythonQtConsoleApp.stdin_port', | |
215 | ip = 'IPythonQtConsoleApp.ip', |
|
215 | ip = 'IPythonQtConsoleApp.ip', | |
216 |
|
216 | |||
217 | plain = 'IPythonQtConsoleApp.plain', |
|
217 | plain = 'IPythonQtConsoleApp.plain', | |
218 | pure = 'IPythonQtConsoleApp.pure', |
|
218 | pure = 'IPythonQtConsoleApp.pure', | |
219 | gui_completion = 'ConsoleWidget.gui_completion', |
|
|||
220 | style = 'IPythonWidget.syntax_style', |
|
219 | style = 'IPythonWidget.syntax_style', | |
221 | stylesheet = 'IPythonQtConsoleApp.stylesheet', |
|
220 | stylesheet = 'IPythonQtConsoleApp.stylesheet', | |
222 | colors = 'ZMQInteractiveShell.colors', |
|
221 | colors = 'ZMQInteractiveShell.colors', | |
223 |
|
222 | |||
224 | editor = 'IPythonWidget.editor', |
|
223 | editor = 'IPythonWidget.editor', | |
|
224 | paging = 'ConsoleWidget.paging', | |||
225 | )) |
|
225 | )) | |
|
226 | aliases['gui-completion'] = 'ConsoleWidget.gui_completion' | |||
|
227 | ||||
226 |
|
228 | |||
227 | #----------------------------------------------------------------------------- |
|
229 | #----------------------------------------------------------------------------- | |
228 | # IPythonQtConsole |
|
230 | # IPythonQtConsole | |
229 | #----------------------------------------------------------------------------- |
|
231 | #----------------------------------------------------------------------------- | |
230 |
|
232 | |||
231 |
|
233 | |||
232 | class IPythonQtConsoleApp(BaseIPythonApplication): |
|
234 | class IPythonQtConsoleApp(BaseIPythonApplication): | |
233 | name = 'ipython-qtconsole' |
|
235 | name = 'ipython-qtconsole' | |
234 | default_config_file_name='ipython_config.py' |
|
236 | default_config_file_name='ipython_config.py' | |
235 |
|
237 | |||
236 | description = """ |
|
238 | description = """ | |
237 | The IPython QtConsole. |
|
239 | The IPython QtConsole. | |
238 |
|
240 | |||
239 | This launches a Console-style application using Qt. It is not a full |
|
241 | This launches a Console-style application using Qt. It is not a full | |
240 |
console, in that launched terminal subprocesses will not |
|
242 | console, in that launched terminal subprocesses will not be able to accept | |
|
243 | input. | |||
|
244 | ||||
|
245 | The QtConsole supports various extra features beyond the Terminal IPython | |||
|
246 | shell, such as inline plotting with matplotlib, via: | |||
|
247 | ||||
|
248 | ipython qtconsole --pylab=inline | |||
241 |
|
249 | |||
242 | The QtConsole supports various extra features beyond the |
|
250 | as well as saving your session as HTML, and printing the output. | |
243 |
|
251 | |||
244 | """ |
|
252 | """ | |
245 | examples = _examples |
|
253 | examples = _examples | |
246 |
|
254 | |||
247 | classes = [IPKernelApp, IPythonWidget, ZMQInteractiveShell, ProfileDir, Session] |
|
255 | classes = [IPKernelApp, IPythonWidget, ZMQInteractiveShell, ProfileDir, Session] | |
248 | flags = Dict(flags) |
|
256 | flags = Dict(flags) | |
249 | aliases = Dict(aliases) |
|
257 | aliases = Dict(aliases) | |
250 |
|
258 | |||
251 | kernel_argv = List(Unicode) |
|
259 | kernel_argv = List(Unicode) | |
252 |
|
260 | |||
253 | # connection info: |
|
261 | # connection info: | |
254 | ip = Unicode(LOCALHOST, config=True, |
|
262 | ip = Unicode(LOCALHOST, config=True, | |
255 | help="""Set the kernel\'s IP address [default localhost]. |
|
263 | help="""Set the kernel\'s IP address [default localhost]. | |
256 | If the IP address is something other than localhost, then |
|
264 | If the IP address is something other than localhost, then | |
257 | Consoles on other machines will be able to connect |
|
265 | Consoles on other machines will be able to connect | |
258 | to the Kernel, so be careful!""" |
|
266 | to the Kernel, so be careful!""" | |
259 | ) |
|
267 | ) | |
260 | hb_port = Int(0, config=True, |
|
268 | hb_port = Int(0, config=True, | |
261 | help="set the heartbeat port [default: random]") |
|
269 | help="set the heartbeat port [default: random]") | |
262 | shell_port = Int(0, config=True, |
|
270 | shell_port = Int(0, config=True, | |
263 | help="set the shell (XREP) port [default: random]") |
|
271 | help="set the shell (XREP) port [default: random]") | |
264 | iopub_port = Int(0, config=True, |
|
272 | iopub_port = Int(0, config=True, | |
265 | help="set the iopub (PUB) port [default: random]") |
|
273 | help="set the iopub (PUB) port [default: random]") | |
266 | stdin_port = Int(0, config=True, |
|
274 | stdin_port = Int(0, config=True, | |
267 | help="set the stdin (XREQ) port [default: random]") |
|
275 | help="set the stdin (XREQ) port [default: random]") | |
268 |
|
276 | |||
269 | existing = CBool(False, config=True, |
|
277 | existing = CBool(False, config=True, | |
270 | help="Whether to connect to an already running Kernel.") |
|
278 | help="Whether to connect to an already running Kernel.") | |
271 |
|
279 | |||
272 | stylesheet = Unicode('', config=True, |
|
280 | stylesheet = Unicode('', config=True, | |
273 | help="path to a custom CSS stylesheet") |
|
281 | help="path to a custom CSS stylesheet") | |
274 |
|
282 | |||
275 | pure = CBool(False, config=True, |
|
283 | pure = CBool(False, config=True, | |
276 | help="Use a pure Python kernel instead of an IPython kernel.") |
|
284 | help="Use a pure Python kernel instead of an IPython kernel.") | |
277 | plain = CBool(False, config=True, |
|
285 | plain = CBool(False, config=True, | |
278 | help="Use a plaintext widget instead of rich text (plain can't print/save).") |
|
286 | help="Use a plaintext widget instead of rich text (plain can't print/save).") | |
279 |
|
287 | |||
280 | def _pure_changed(self, name, old, new): |
|
288 | def _pure_changed(self, name, old, new): | |
281 | kind = 'plain' if self.plain else 'rich' |
|
289 | kind = 'plain' if self.plain else 'rich' | |
282 | self.config.ConsoleWidget.kind = kind |
|
290 | self.config.ConsoleWidget.kind = kind | |
283 | if self.pure: |
|
291 | if self.pure: | |
284 | self.widget_factory = FrontendWidget |
|
292 | self.widget_factory = FrontendWidget | |
285 | elif self.plain: |
|
293 | elif self.plain: | |
286 | self.widget_factory = IPythonWidget |
|
294 | self.widget_factory = IPythonWidget | |
287 | else: |
|
295 | else: | |
288 | self.widget_factory = RichIPythonWidget |
|
296 | self.widget_factory = RichIPythonWidget | |
289 |
|
297 | |||
290 | _plain_changed = _pure_changed |
|
298 | _plain_changed = _pure_changed | |
291 |
|
299 | |||
292 | confirm_exit = CBool(True, config=True, |
|
300 | confirm_exit = CBool(True, config=True, | |
293 | help=""" |
|
301 | help=""" | |
294 | Set to display confirmation dialog on exit. You can always use 'exit' or 'quit', |
|
302 | Set to display confirmation dialog on exit. You can always use 'exit' or 'quit', | |
295 | to force a direct exit without any confirmation.""", |
|
303 | to force a direct exit without any confirmation.""", | |
296 | ) |
|
304 | ) | |
297 |
|
305 | |||
298 | # the factory for creating a widget |
|
306 | # the factory for creating a widget | |
299 | widget_factory = Any(RichIPythonWidget) |
|
307 | widget_factory = Any(RichIPythonWidget) | |
300 |
|
308 | |||
301 | def parse_command_line(self, argv=None): |
|
309 | def parse_command_line(self, argv=None): | |
302 | super(IPythonQtConsoleApp, self).parse_command_line(argv) |
|
310 | super(IPythonQtConsoleApp, self).parse_command_line(argv) | |
303 | if argv is None: |
|
311 | if argv is None: | |
304 | argv = sys.argv[1:] |
|
312 | argv = sys.argv[1:] | |
305 |
|
313 | |||
306 | self.kernel_argv = list(argv) # copy |
|
314 | self.kernel_argv = list(argv) # copy | |
307 | # kernel should inherit default config file from frontend |
|
315 | # kernel should inherit default config file from frontend | |
308 | self.kernel_argv.append("--KernelApp.parent_appname='%s'"%self.name) |
|
316 | self.kernel_argv.append("--KernelApp.parent_appname='%s'"%self.name) | |
309 | # scrub frontend-specific flags |
|
317 | # scrub frontend-specific flags | |
310 | for a in argv: |
|
318 | for a in argv: | |
311 | if a.startswith('-') and a.lstrip('-') in qt_flags: |
|
319 | if a.startswith('-') and a.lstrip('-') in qt_flags: | |
312 | self.kernel_argv.remove(a) |
|
320 | self.kernel_argv.remove(a) | |
313 |
|
321 | |||
314 | def init_kernel_manager(self): |
|
322 | def init_kernel_manager(self): | |
315 | # Don't let Qt or ZMQ swallow KeyboardInterupts. |
|
323 | # Don't let Qt or ZMQ swallow KeyboardInterupts. | |
316 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
|
324 | signal.signal(signal.SIGINT, signal.SIG_DFL) | |
317 |
|
325 | |||
318 | # Create a KernelManager and start a kernel. |
|
326 | # Create a KernelManager and start a kernel. | |
319 | self.kernel_manager = QtKernelManager( |
|
327 | self.kernel_manager = QtKernelManager( | |
320 | shell_address=(self.ip, self.shell_port), |
|
328 | shell_address=(self.ip, self.shell_port), | |
321 | sub_address=(self.ip, self.iopub_port), |
|
329 | sub_address=(self.ip, self.iopub_port), | |
322 | stdin_address=(self.ip, self.stdin_port), |
|
330 | stdin_address=(self.ip, self.stdin_port), | |
323 | hb_address=(self.ip, self.hb_port), |
|
331 | hb_address=(self.ip, self.hb_port), | |
324 | config=self.config |
|
332 | config=self.config | |
325 | ) |
|
333 | ) | |
326 | # start the kernel |
|
334 | # start the kernel | |
327 | if not self.existing: |
|
335 | if not self.existing: | |
328 | kwargs = dict(ip=self.ip, ipython=not self.pure) |
|
336 | kwargs = dict(ip=self.ip, ipython=not self.pure) | |
329 | kwargs['extra_arguments'] = self.kernel_argv |
|
337 | kwargs['extra_arguments'] = self.kernel_argv | |
330 | self.kernel_manager.start_kernel(**kwargs) |
|
338 | self.kernel_manager.start_kernel(**kwargs) | |
331 | self.kernel_manager.start_channels() |
|
339 | self.kernel_manager.start_channels() | |
332 |
|
340 | |||
333 |
|
341 | |||
334 | def init_qt_elements(self): |
|
342 | def init_qt_elements(self): | |
335 | # Create the widget. |
|
343 | # Create the widget. | |
336 | self.app = QtGui.QApplication([]) |
|
344 | self.app = QtGui.QApplication([]) | |
337 | local_kernel = (not self.existing) or self.ip in LOCAL_IPS |
|
345 | local_kernel = (not self.existing) or self.ip in LOCAL_IPS | |
338 | self.widget = self.widget_factory(config=self.config, |
|
346 | self.widget = self.widget_factory(config=self.config, | |
339 | local_kernel=local_kernel) |
|
347 | local_kernel=local_kernel) | |
340 | self.widget.kernel_manager = self.kernel_manager |
|
348 | self.widget.kernel_manager = self.kernel_manager | |
341 | self.window = MainWindow(self.app, self.widget, self.existing, |
|
349 | self.window = MainWindow(self.app, self.widget, self.existing, | |
342 | may_close=local_kernel, |
|
350 | may_close=local_kernel, | |
343 | confirm_exit=self.confirm_exit) |
|
351 | confirm_exit=self.confirm_exit) | |
344 | self.window.setWindowTitle('Python' if self.pure else 'IPython') |
|
352 | self.window.setWindowTitle('Python' if self.pure else 'IPython') | |
345 |
|
353 | |||
346 | def init_colors(self): |
|
354 | def init_colors(self): | |
347 | """Configure the coloring of the widget""" |
|
355 | """Configure the coloring of the widget""" | |
348 | # Note: This will be dramatically simplified when colors |
|
356 | # Note: This will be dramatically simplified when colors | |
349 | # are removed from the backend. |
|
357 | # are removed from the backend. | |
350 |
|
358 | |||
351 | if self.pure: |
|
359 | if self.pure: | |
352 | # only IPythonWidget supports styling |
|
360 | # only IPythonWidget supports styling | |
353 | return |
|
361 | return | |
354 |
|
362 | |||
355 | # parse the colors arg down to current known labels |
|
363 | # parse the colors arg down to current known labels | |
356 | try: |
|
364 | try: | |
357 | colors = self.config.ZMQInteractiveShell.colors |
|
365 | colors = self.config.ZMQInteractiveShell.colors | |
358 | except AttributeError: |
|
366 | except AttributeError: | |
359 | colors = None |
|
367 | colors = None | |
360 | try: |
|
368 | try: | |
361 | style = self.config.IPythonWidget.colors |
|
369 | style = self.config.IPythonWidget.colors | |
362 | except AttributeError: |
|
370 | except AttributeError: | |
363 | style = None |
|
371 | style = None | |
364 |
|
372 | |||
365 | # find the value for colors: |
|
373 | # find the value for colors: | |
366 | if colors: |
|
374 | if colors: | |
367 | colors=colors.lower() |
|
375 | colors=colors.lower() | |
368 | if colors in ('lightbg', 'light'): |
|
376 | if colors in ('lightbg', 'light'): | |
369 | colors='lightbg' |
|
377 | colors='lightbg' | |
370 | elif colors in ('dark', 'linux'): |
|
378 | elif colors in ('dark', 'linux'): | |
371 | colors='linux' |
|
379 | colors='linux' | |
372 | else: |
|
380 | else: | |
373 | colors='nocolor' |
|
381 | colors='nocolor' | |
374 | elif style: |
|
382 | elif style: | |
375 | if style=='bw': |
|
383 | if style=='bw': | |
376 | colors='nocolor' |
|
384 | colors='nocolor' | |
377 | elif styles.dark_style(style): |
|
385 | elif styles.dark_style(style): | |
378 | colors='linux' |
|
386 | colors='linux' | |
379 | else: |
|
387 | else: | |
380 | colors='lightbg' |
|
388 | colors='lightbg' | |
381 | else: |
|
389 | else: | |
382 | colors=None |
|
390 | colors=None | |
383 |
|
391 | |||
384 | # Configure the style. |
|
392 | # Configure the style. | |
385 | widget = self.widget |
|
393 | widget = self.widget | |
386 | if style: |
|
394 | if style: | |
387 | widget.style_sheet = styles.sheet_from_template(style, colors) |
|
395 | widget.style_sheet = styles.sheet_from_template(style, colors) | |
388 | widget.syntax_style = style |
|
396 | widget.syntax_style = style | |
389 | widget._syntax_style_changed() |
|
397 | widget._syntax_style_changed() | |
390 | widget._style_sheet_changed() |
|
398 | widget._style_sheet_changed() | |
391 | elif colors: |
|
399 | elif colors: | |
392 | # use a default style |
|
400 | # use a default style | |
393 | widget.set_default_style(colors=colors) |
|
401 | widget.set_default_style(colors=colors) | |
394 | else: |
|
402 | else: | |
395 | # this is redundant for now, but allows the widget's |
|
403 | # this is redundant for now, but allows the widget's | |
396 | # defaults to change |
|
404 | # defaults to change | |
397 | widget.set_default_style() |
|
405 | widget.set_default_style() | |
398 |
|
406 | |||
399 | if self.stylesheet: |
|
407 | if self.stylesheet: | |
400 | # we got an expicit stylesheet |
|
408 | # we got an expicit stylesheet | |
401 | if os.path.isfile(self.stylesheet): |
|
409 | if os.path.isfile(self.stylesheet): | |
402 | with open(self.stylesheet) as f: |
|
410 | with open(self.stylesheet) as f: | |
403 | sheet = f.read() |
|
411 | sheet = f.read() | |
404 | widget.style_sheet = sheet |
|
412 | widget.style_sheet = sheet | |
405 | widget._style_sheet_changed() |
|
413 | widget._style_sheet_changed() | |
406 | else: |
|
414 | else: | |
407 | raise IOError("Stylesheet %r not found."%self.stylesheet) |
|
415 | raise IOError("Stylesheet %r not found."%self.stylesheet) | |
408 |
|
416 | |||
409 | def initialize(self, argv=None): |
|
417 | def initialize(self, argv=None): | |
410 | super(IPythonQtConsoleApp, self).initialize(argv) |
|
418 | super(IPythonQtConsoleApp, self).initialize(argv) | |
411 | self.init_kernel_manager() |
|
419 | self.init_kernel_manager() | |
412 | self.init_qt_elements() |
|
420 | self.init_qt_elements() | |
413 | self.init_colors() |
|
421 | self.init_colors() | |
414 |
|
422 | |||
415 | def start(self): |
|
423 | def start(self): | |
416 |
|
424 | |||
417 | # draw the window |
|
425 | # draw the window | |
418 | self.window.show() |
|
426 | self.window.show() | |
419 |
|
427 | |||
420 | # Start the application main loop. |
|
428 | # Start the application main loop. | |
421 | self.app.exec_() |
|
429 | self.app.exec_() | |
422 |
|
430 | |||
423 | #----------------------------------------------------------------------------- |
|
431 | #----------------------------------------------------------------------------- | |
424 | # Main entry point |
|
432 | # Main entry point | |
425 | #----------------------------------------------------------------------------- |
|
433 | #----------------------------------------------------------------------------- | |
426 |
|
434 | |||
427 | def main(): |
|
435 | def main(): | |
428 | app = IPythonQtConsoleApp() |
|
436 | app = IPythonQtConsoleApp() | |
429 | app.initialize() |
|
437 | app.initialize() | |
430 | app.start() |
|
438 | app.start() | |
431 |
|
439 | |||
432 |
|
440 | |||
433 | if __name__ == '__main__': |
|
441 | if __name__ == '__main__': | |
434 | main() |
|
442 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now