Show More
@@ -0,0 +1,99 b'' | |||||
|
1 | """Utilities for connecting to kernels | |||
|
2 | ||||
|
3 | Authors: | |||
|
4 | ||||
|
5 | * Min Ragan-Kelley | |||
|
6 | ||||
|
7 | """ | |||
|
8 | ||||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | # Copyright (C) 2011 The IPython Development Team | |||
|
11 | # | |||
|
12 | # Distributed under the terms of the BSD License. The full license is in | |||
|
13 | # the file COPYING, distributed as part of this software. | |||
|
14 | #----------------------------------------------------------------------------- | |||
|
15 | ||||
|
16 | #----------------------------------------------------------------------------- | |||
|
17 | # Imports | |||
|
18 | #----------------------------------------------------------------------------- | |||
|
19 | ||||
|
20 | import json | |||
|
21 | import sys | |||
|
22 | from subprocess import Popen, PIPE | |||
|
23 | ||||
|
24 | from IPython.utils.path import filefind | |||
|
25 | from IPython.utils.py3compat import str_to_bytes | |||
|
26 | ||||
|
27 | ||||
|
28 | #----------------------------------------------------------------------------- | |||
|
29 | # Functions | |||
|
30 | #----------------------------------------------------------------------------- | |||
|
31 | ||||
|
32 | def get_connection_file(app=None): | |||
|
33 | """Return the path to the connection file of an app | |||
|
34 | ||||
|
35 | Parameters | |||
|
36 | ---------- | |||
|
37 | app : KernelApp instance [optional] | |||
|
38 | If unspecified, the currently running app will be used | |||
|
39 | """ | |||
|
40 | if app is None: | |||
|
41 | from IPython.zmq.kernelapp import KernelApp | |||
|
42 | if not KernelApp.initialized(): | |||
|
43 | raise RuntimeError("app not specified, and not in a running Kernel") | |||
|
44 | ||||
|
45 | app = KernelApp.instance() | |||
|
46 | return filefind(app.connection_file, ['.', app.profile_dir.security_dir]) | |||
|
47 | ||||
|
48 | def get_connection_info(unpack=False): | |||
|
49 | """Return the connection information for the current Kernel. | |||
|
50 | ||||
|
51 | Parameters | |||
|
52 | ---------- | |||
|
53 | unpack : bool [default: False] | |||
|
54 | if True, return the unpacked dict, otherwise just the string contents | |||
|
55 | of the file. | |||
|
56 | ||||
|
57 | Returns | |||
|
58 | ------- | |||
|
59 | The connection dictionary of the current kernel, as string or dict, | |||
|
60 | depending on `unpack`. | |||
|
61 | """ | |||
|
62 | cf = get_connection_file() | |||
|
63 | with open(cf) as f: | |||
|
64 | info = f.read() | |||
|
65 | ||||
|
66 | if unpack: | |||
|
67 | info = json.loads(info) | |||
|
68 | # ensure key is bytes: | |||
|
69 | info['key'] = str_to_bytes(info.get('key', '')) | |||
|
70 | return info | |||
|
71 | ||||
|
72 | def connect_qtconsole(argv=None): | |||
|
73 | """Connect a qtconsole to the current kernel. | |||
|
74 | ||||
|
75 | This is useful for connecting a second qtconsole to a kernel, or to a | |||
|
76 | local notebook. | |||
|
77 | ||||
|
78 | Parameters | |||
|
79 | ---------- | |||
|
80 | argv : list [optional] | |||
|
81 | Any extra args to be passed to the console. | |||
|
82 | ||||
|
83 | Returns | |||
|
84 | ------- | |||
|
85 | subprocess.Popen instance running the qtconsole frontend | |||
|
86 | """ | |||
|
87 | argv = [] if argv is None else argv | |||
|
88 | ||||
|
89 | # get connection file from current kernel | |||
|
90 | cf = get_connection_file() | |||
|
91 | ||||
|
92 | cmd = ';'.join([ | |||
|
93 | "from IPython.frontend.qt.console import qtconsoleapp", | |||
|
94 | "qtconsoleapp.main()" | |||
|
95 | ]) | |||
|
96 | ||||
|
97 | return Popen([sys.executable, '-c', cmd, '--existing', cf] + argv, stdout=PIPE, stderr=PIPE) | |||
|
98 | ||||
|
99 |
@@ -1,497 +1,489 b'' | |||||
1 | """A ZMQ-based subclass of InteractiveShell. |
|
1 | """A ZMQ-based subclass of InteractiveShell. | |
2 |
|
2 | |||
3 | This code is meant to ease the refactoring of the base InteractiveShell into |
|
3 | This code is meant to ease the refactoring of the base InteractiveShell into | |
4 | something with a cleaner architecture for 2-process use, without actually |
|
4 | something with a cleaner architecture for 2-process use, without actually | |
5 | breaking InteractiveShell itself. So we're doing something a bit ugly, where |
|
5 | breaking InteractiveShell itself. So we're doing something a bit ugly, where | |
6 | we subclass and override what we want to fix. Once this is working well, we |
|
6 | we subclass and override what we want to fix. Once this is working well, we | |
7 | can go back to the base class and refactor the code for a cleaner inheritance |
|
7 | can go back to the base class and refactor the code for a cleaner inheritance | |
8 | implementation that doesn't rely on so much monkeypatching. |
|
8 | implementation that doesn't rely on so much monkeypatching. | |
9 |
|
9 | |||
10 | But this lets us maintain a fully working IPython as we develop the new |
|
10 | But this lets us maintain a fully working IPython as we develop the new | |
11 | machinery. This should thus be thought of as scaffolding. |
|
11 | machinery. This should thus be thought of as scaffolding. | |
12 | """ |
|
12 | """ | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from __future__ import print_function |
|
16 | from __future__ import print_function | |
17 |
|
17 | |||
18 | # Stdlib |
|
18 | # Stdlib | |
19 | import inspect |
|
19 | import inspect | |
20 | import os |
|
20 | import os | |
21 | import sys |
|
21 | import sys | |
22 | from subprocess import Popen, PIPE |
|
22 | from subprocess import Popen, PIPE | |
23 |
|
23 | |||
24 | # Our own |
|
24 | # Our own | |
25 | from IPython.core.interactiveshell import ( |
|
25 | from IPython.core.interactiveshell import ( | |
26 | InteractiveShell, InteractiveShellABC |
|
26 | InteractiveShell, InteractiveShellABC | |
27 | ) |
|
27 | ) | |
28 | from IPython.core import page |
|
28 | from IPython.core import page | |
29 | from IPython.core.autocall import ZMQExitAutocall |
|
29 | from IPython.core.autocall import ZMQExitAutocall | |
30 | from IPython.core.displaypub import DisplayPublisher |
|
30 | from IPython.core.displaypub import DisplayPublisher | |
31 | from IPython.core.macro import Macro |
|
31 | from IPython.core.macro import Macro | |
32 | from IPython.core.magic import MacroToEdit |
|
32 | from IPython.core.magic import MacroToEdit | |
33 | from IPython.core.payloadpage import install_payload_page |
|
33 | from IPython.core.payloadpage import install_payload_page | |
|
34 | from IPython.lib.kernel import ( | |||
|
35 | get_connection_file, get_connection_info, connect_qtconsole | |||
|
36 | ) | |||
34 | from IPython.utils import io |
|
37 | from IPython.utils import io | |
35 | from IPython.utils.jsonutil import json_clean |
|
38 | from IPython.utils.jsonutil import json_clean | |
36 | from IPython.utils.path import get_py_filename |
|
39 | from IPython.utils.path import get_py_filename | |
|
40 | from IPython.utils.process import arg_split | |||
37 | from IPython.utils.traitlets import Instance, Type, Dict, CBool |
|
41 | from IPython.utils.traitlets import Instance, Type, Dict, CBool | |
38 | from IPython.utils.warn import warn, error |
|
42 | from IPython.utils.warn import warn, error | |
39 | from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_binary |
|
43 | from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_binary | |
40 | from IPython.zmq.session import extract_header |
|
44 | from IPython.zmq.session import extract_header | |
41 | from session import Session |
|
45 | from session import Session | |
42 |
|
46 | |||
43 | #----------------------------------------------------------------------------- |
|
47 | #----------------------------------------------------------------------------- | |
44 | # Globals and side-effects |
|
48 | # Globals and side-effects | |
45 | #----------------------------------------------------------------------------- |
|
49 | #----------------------------------------------------------------------------- | |
46 |
|
50 | |||
47 | # Install the payload version of page. |
|
51 | # Install the payload version of page. | |
48 | install_payload_page() |
|
52 | install_payload_page() | |
49 |
|
53 | |||
50 | #----------------------------------------------------------------------------- |
|
54 | #----------------------------------------------------------------------------- | |
51 | # Functions and classes |
|
55 | # Functions and classes | |
52 | #----------------------------------------------------------------------------- |
|
56 | #----------------------------------------------------------------------------- | |
53 |
|
57 | |||
54 | class ZMQDisplayPublisher(DisplayPublisher): |
|
58 | class ZMQDisplayPublisher(DisplayPublisher): | |
55 | """A display publisher that publishes data using a ZeroMQ PUB socket.""" |
|
59 | """A display publisher that publishes data using a ZeroMQ PUB socket.""" | |
56 |
|
60 | |||
57 | session = Instance(Session) |
|
61 | session = Instance(Session) | |
58 | pub_socket = Instance('zmq.Socket') |
|
62 | pub_socket = Instance('zmq.Socket') | |
59 | parent_header = Dict({}) |
|
63 | parent_header = Dict({}) | |
60 |
|
64 | |||
61 | def set_parent(self, parent): |
|
65 | def set_parent(self, parent): | |
62 | """Set the parent for outbound messages.""" |
|
66 | """Set the parent for outbound messages.""" | |
63 | self.parent_header = extract_header(parent) |
|
67 | self.parent_header = extract_header(parent) | |
64 |
|
68 | |||
65 | def publish(self, source, data, metadata=None): |
|
69 | def publish(self, source, data, metadata=None): | |
66 | if metadata is None: |
|
70 | if metadata is None: | |
67 | metadata = {} |
|
71 | metadata = {} | |
68 | self._validate_data(source, data, metadata) |
|
72 | self._validate_data(source, data, metadata) | |
69 | content = {} |
|
73 | content = {} | |
70 | content['source'] = source |
|
74 | content['source'] = source | |
71 | _encode_binary(data) |
|
75 | _encode_binary(data) | |
72 | content['data'] = data |
|
76 | content['data'] = data | |
73 | content['metadata'] = metadata |
|
77 | content['metadata'] = metadata | |
74 | self.session.send( |
|
78 | self.session.send( | |
75 | self.pub_socket, u'display_data', json_clean(content), |
|
79 | self.pub_socket, u'display_data', json_clean(content), | |
76 | parent=self.parent_header |
|
80 | parent=self.parent_header | |
77 | ) |
|
81 | ) | |
78 |
|
82 | |||
79 |
|
83 | |||
80 | class ZMQInteractiveShell(InteractiveShell): |
|
84 | class ZMQInteractiveShell(InteractiveShell): | |
81 | """A subclass of InteractiveShell for ZMQ.""" |
|
85 | """A subclass of InteractiveShell for ZMQ.""" | |
82 |
|
86 | |||
83 | displayhook_class = Type(ZMQShellDisplayHook) |
|
87 | displayhook_class = Type(ZMQShellDisplayHook) | |
84 | display_pub_class = Type(ZMQDisplayPublisher) |
|
88 | display_pub_class = Type(ZMQDisplayPublisher) | |
85 |
|
89 | |||
86 | # Override the traitlet in the parent class, because there's no point using |
|
90 | # Override the traitlet in the parent class, because there's no point using | |
87 | # readline for the kernel. Can be removed when the readline code is moved |
|
91 | # readline for the kernel. Can be removed when the readline code is moved | |
88 | # to the terminal frontend. |
|
92 | # to the terminal frontend. | |
89 | colors_force = CBool(True) |
|
93 | colors_force = CBool(True) | |
90 | readline_use = CBool(False) |
|
94 | readline_use = CBool(False) | |
91 | # autoindent has no meaning in a zmqshell, and attempting to enable it |
|
95 | # autoindent has no meaning in a zmqshell, and attempting to enable it | |
92 | # will print a warning in the absence of readline. |
|
96 | # will print a warning in the absence of readline. | |
93 | autoindent = CBool(False) |
|
97 | autoindent = CBool(False) | |
94 |
|
98 | |||
95 | exiter = Instance(ZMQExitAutocall) |
|
99 | exiter = Instance(ZMQExitAutocall) | |
96 | def _exiter_default(self): |
|
100 | def _exiter_default(self): | |
97 | return ZMQExitAutocall(self) |
|
101 | return ZMQExitAutocall(self) | |
98 |
|
102 | |||
99 | keepkernel_on_exit = None |
|
103 | keepkernel_on_exit = None | |
100 |
|
104 | |||
101 | def init_environment(self): |
|
105 | def init_environment(self): | |
102 | """Configure the user's environment. |
|
106 | """Configure the user's environment. | |
103 |
|
107 | |||
104 | """ |
|
108 | """ | |
105 | env = os.environ |
|
109 | env = os.environ | |
106 | # These two ensure 'ls' produces nice coloring on BSD-derived systems |
|
110 | # These two ensure 'ls' produces nice coloring on BSD-derived systems | |
107 | env['TERM'] = 'xterm-color' |
|
111 | env['TERM'] = 'xterm-color' | |
108 | env['CLICOLOR'] = '1' |
|
112 | env['CLICOLOR'] = '1' | |
109 | # Since normal pagers don't work at all (over pexpect we don't have |
|
113 | # Since normal pagers don't work at all (over pexpect we don't have | |
110 | # single-key control of the subprocess), try to disable paging in |
|
114 | # single-key control of the subprocess), try to disable paging in | |
111 | # subprocesses as much as possible. |
|
115 | # subprocesses as much as possible. | |
112 | env['PAGER'] = 'cat' |
|
116 | env['PAGER'] = 'cat' | |
113 | env['GIT_PAGER'] = 'cat' |
|
117 | env['GIT_PAGER'] = 'cat' | |
114 |
|
118 | |||
115 | def auto_rewrite_input(self, cmd): |
|
119 | def auto_rewrite_input(self, cmd): | |
116 | """Called to show the auto-rewritten input for autocall and friends. |
|
120 | """Called to show the auto-rewritten input for autocall and friends. | |
117 |
|
121 | |||
118 | FIXME: this payload is currently not correctly processed by the |
|
122 | FIXME: this payload is currently not correctly processed by the | |
119 | frontend. |
|
123 | frontend. | |
120 | """ |
|
124 | """ | |
121 | new = self.displayhook.prompt1.auto_rewrite() + cmd |
|
125 | new = self.displayhook.prompt1.auto_rewrite() + cmd | |
122 | payload = dict( |
|
126 | payload = dict( | |
123 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.auto_rewrite_input', |
|
127 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.auto_rewrite_input', | |
124 | transformed_input=new, |
|
128 | transformed_input=new, | |
125 | ) |
|
129 | ) | |
126 | self.payload_manager.write_payload(payload) |
|
130 | self.payload_manager.write_payload(payload) | |
127 |
|
131 | |||
128 | def ask_exit(self): |
|
132 | def ask_exit(self): | |
129 | """Engage the exit actions.""" |
|
133 | """Engage the exit actions.""" | |
130 | payload = dict( |
|
134 | payload = dict( | |
131 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit', |
|
135 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit', | |
132 | exit=True, |
|
136 | exit=True, | |
133 | keepkernel=self.keepkernel_on_exit, |
|
137 | keepkernel=self.keepkernel_on_exit, | |
134 | ) |
|
138 | ) | |
135 | self.payload_manager.write_payload(payload) |
|
139 | self.payload_manager.write_payload(payload) | |
136 |
|
140 | |||
137 | def _showtraceback(self, etype, evalue, stb): |
|
141 | def _showtraceback(self, etype, evalue, stb): | |
138 |
|
142 | |||
139 | exc_content = { |
|
143 | exc_content = { | |
140 | u'traceback' : stb, |
|
144 | u'traceback' : stb, | |
141 | u'ename' : unicode(etype.__name__), |
|
145 | u'ename' : unicode(etype.__name__), | |
142 | u'evalue' : unicode(evalue) |
|
146 | u'evalue' : unicode(evalue) | |
143 | } |
|
147 | } | |
144 |
|
148 | |||
145 | dh = self.displayhook |
|
149 | dh = self.displayhook | |
146 | # Send exception info over pub socket for other clients than the caller |
|
150 | # Send exception info over pub socket for other clients than the caller | |
147 | # to pick up |
|
151 | # to pick up | |
148 | exc_msg = dh.session.send(dh.pub_socket, u'pyerr', json_clean(exc_content), dh.parent_header) |
|
152 | exc_msg = dh.session.send(dh.pub_socket, u'pyerr', json_clean(exc_content), dh.parent_header) | |
149 |
|
153 | |||
150 | # FIXME - Hack: store exception info in shell object. Right now, the |
|
154 | # FIXME - Hack: store exception info in shell object. Right now, the | |
151 | # caller is reading this info after the fact, we need to fix this logic |
|
155 | # caller is reading this info after the fact, we need to fix this logic | |
152 | # to remove this hack. Even uglier, we need to store the error status |
|
156 | # to remove this hack. Even uglier, we need to store the error status | |
153 | # here, because in the main loop, the logic that sets it is being |
|
157 | # here, because in the main loop, the logic that sets it is being | |
154 | # skipped because runlines swallows the exceptions. |
|
158 | # skipped because runlines swallows the exceptions. | |
155 | exc_content[u'status'] = u'error' |
|
159 | exc_content[u'status'] = u'error' | |
156 | self._reply_content = exc_content |
|
160 | self._reply_content = exc_content | |
157 | # /FIXME |
|
161 | # /FIXME | |
158 |
|
162 | |||
159 | return exc_content |
|
163 | return exc_content | |
160 |
|
164 | |||
161 | #------------------------------------------------------------------------ |
|
165 | #------------------------------------------------------------------------ | |
162 | # Magic overrides |
|
166 | # Magic overrides | |
163 | #------------------------------------------------------------------------ |
|
167 | #------------------------------------------------------------------------ | |
164 | # Once the base class stops inheriting from magic, this code needs to be |
|
168 | # Once the base class stops inheriting from magic, this code needs to be | |
165 | # moved into a separate machinery as well. For now, at least isolate here |
|
169 | # moved into a separate machinery as well. For now, at least isolate here | |
166 | # the magics which this class needs to implement differently from the base |
|
170 | # the magics which this class needs to implement differently from the base | |
167 | # class, or that are unique to it. |
|
171 | # class, or that are unique to it. | |
168 |
|
172 | |||
169 | def magic_doctest_mode(self,parameter_s=''): |
|
173 | def magic_doctest_mode(self,parameter_s=''): | |
170 | """Toggle doctest mode on and off. |
|
174 | """Toggle doctest mode on and off. | |
171 |
|
175 | |||
172 | This mode is intended to make IPython behave as much as possible like a |
|
176 | This mode is intended to make IPython behave as much as possible like a | |
173 | plain Python shell, from the perspective of how its prompts, exceptions |
|
177 | plain Python shell, from the perspective of how its prompts, exceptions | |
174 | and output look. This makes it easy to copy and paste parts of a |
|
178 | and output look. This makes it easy to copy and paste parts of a | |
175 | session into doctests. It does so by: |
|
179 | session into doctests. It does so by: | |
176 |
|
180 | |||
177 | - Changing the prompts to the classic ``>>>`` ones. |
|
181 | - Changing the prompts to the classic ``>>>`` ones. | |
178 | - Changing the exception reporting mode to 'Plain'. |
|
182 | - Changing the exception reporting mode to 'Plain'. | |
179 | - Disabling pretty-printing of output. |
|
183 | - Disabling pretty-printing of output. | |
180 |
|
184 | |||
181 | Note that IPython also supports the pasting of code snippets that have |
|
185 | Note that IPython also supports the pasting of code snippets that have | |
182 | leading '>>>' and '...' prompts in them. This means that you can paste |
|
186 | leading '>>>' and '...' prompts in them. This means that you can paste | |
183 | doctests from files or docstrings (even if they have leading |
|
187 | doctests from files or docstrings (even if they have leading | |
184 | whitespace), and the code will execute correctly. You can then use |
|
188 | whitespace), and the code will execute correctly. You can then use | |
185 | '%history -t' to see the translated history; this will give you the |
|
189 | '%history -t' to see the translated history; this will give you the | |
186 | input after removal of all the leading prompts and whitespace, which |
|
190 | input after removal of all the leading prompts and whitespace, which | |
187 | can be pasted back into an editor. |
|
191 | can be pasted back into an editor. | |
188 |
|
192 | |||
189 | With these features, you can switch into this mode easily whenever you |
|
193 | With these features, you can switch into this mode easily whenever you | |
190 | need to do testing and changes to doctests, without having to leave |
|
194 | need to do testing and changes to doctests, without having to leave | |
191 | your existing IPython session. |
|
195 | your existing IPython session. | |
192 | """ |
|
196 | """ | |
193 |
|
197 | |||
194 | from IPython.utils.ipstruct import Struct |
|
198 | from IPython.utils.ipstruct import Struct | |
195 |
|
199 | |||
196 | # Shorthands |
|
200 | # Shorthands | |
197 | shell = self.shell |
|
201 | shell = self.shell | |
198 | disp_formatter = self.shell.display_formatter |
|
202 | disp_formatter = self.shell.display_formatter | |
199 | ptformatter = disp_formatter.formatters['text/plain'] |
|
203 | ptformatter = disp_formatter.formatters['text/plain'] | |
200 | # dstore is a data store kept in the instance metadata bag to track any |
|
204 | # dstore is a data store kept in the instance metadata bag to track any | |
201 | # changes we make, so we can undo them later. |
|
205 | # changes we make, so we can undo them later. | |
202 | dstore = shell.meta.setdefault('doctest_mode', Struct()) |
|
206 | dstore = shell.meta.setdefault('doctest_mode', Struct()) | |
203 | save_dstore = dstore.setdefault |
|
207 | save_dstore = dstore.setdefault | |
204 |
|
208 | |||
205 | # save a few values we'll need to recover later |
|
209 | # save a few values we'll need to recover later | |
206 | mode = save_dstore('mode', False) |
|
210 | mode = save_dstore('mode', False) | |
207 | save_dstore('rc_pprint', ptformatter.pprint) |
|
211 | save_dstore('rc_pprint', ptformatter.pprint) | |
208 | save_dstore('rc_plain_text_only',disp_formatter.plain_text_only) |
|
212 | save_dstore('rc_plain_text_only',disp_formatter.plain_text_only) | |
209 | save_dstore('xmode', shell.InteractiveTB.mode) |
|
213 | save_dstore('xmode', shell.InteractiveTB.mode) | |
210 |
|
214 | |||
211 | if mode == False: |
|
215 | if mode == False: | |
212 | # turn on |
|
216 | # turn on | |
213 | ptformatter.pprint = False |
|
217 | ptformatter.pprint = False | |
214 | disp_formatter.plain_text_only = True |
|
218 | disp_formatter.plain_text_only = True | |
215 | shell.magic_xmode('Plain') |
|
219 | shell.magic_xmode('Plain') | |
216 | else: |
|
220 | else: | |
217 | # turn off |
|
221 | # turn off | |
218 | ptformatter.pprint = dstore.rc_pprint |
|
222 | ptformatter.pprint = dstore.rc_pprint | |
219 | disp_formatter.plain_text_only = dstore.rc_plain_text_only |
|
223 | disp_formatter.plain_text_only = dstore.rc_plain_text_only | |
220 | shell.magic_xmode(dstore.xmode) |
|
224 | shell.magic_xmode(dstore.xmode) | |
221 |
|
225 | |||
222 | # Store new mode and inform on console |
|
226 | # Store new mode and inform on console | |
223 | dstore.mode = bool(1-int(mode)) |
|
227 | dstore.mode = bool(1-int(mode)) | |
224 | mode_label = ['OFF','ON'][dstore.mode] |
|
228 | mode_label = ['OFF','ON'][dstore.mode] | |
225 | print('Doctest mode is:', mode_label) |
|
229 | print('Doctest mode is:', mode_label) | |
226 |
|
230 | |||
227 | # Send the payload back so that clients can modify their prompt display |
|
231 | # Send the payload back so that clients can modify their prompt display | |
228 | payload = dict( |
|
232 | payload = dict( | |
229 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.magic_doctest_mode', |
|
233 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.magic_doctest_mode', | |
230 | mode=dstore.mode) |
|
234 | mode=dstore.mode) | |
231 | self.payload_manager.write_payload(payload) |
|
235 | self.payload_manager.write_payload(payload) | |
232 |
|
236 | |||
233 | def magic_edit(self,parameter_s='',last_call=['','']): |
|
237 | def magic_edit(self,parameter_s='',last_call=['','']): | |
234 | """Bring up an editor and execute the resulting code. |
|
238 | """Bring up an editor and execute the resulting code. | |
235 |
|
239 | |||
236 | Usage: |
|
240 | Usage: | |
237 | %edit [options] [args] |
|
241 | %edit [options] [args] | |
238 |
|
242 | |||
239 | %edit runs an external text editor. You will need to set the command for |
|
243 | %edit runs an external text editor. You will need to set the command for | |
240 | this editor via the ``TerminalInteractiveShell.editor`` option in your |
|
244 | this editor via the ``TerminalInteractiveShell.editor`` option in your | |
241 | configuration file before it will work. |
|
245 | configuration file before it will work. | |
242 |
|
246 | |||
243 | This command allows you to conveniently edit multi-line code right in |
|
247 | This command allows you to conveniently edit multi-line code right in | |
244 | your IPython session. |
|
248 | your IPython session. | |
245 |
|
249 | |||
246 | If called without arguments, %edit opens up an empty editor with a |
|
250 | If called without arguments, %edit opens up an empty editor with a | |
247 | temporary file and will execute the contents of this file when you |
|
251 | temporary file and will execute the contents of this file when you | |
248 | close it (don't forget to save it!). |
|
252 | close it (don't forget to save it!). | |
249 |
|
253 | |||
250 |
|
254 | |||
251 | Options: |
|
255 | Options: | |
252 |
|
256 | |||
253 | -n <number>: open the editor at a specified line number. By default, |
|
257 | -n <number>: open the editor at a specified line number. By default, | |
254 | the IPython editor hook uses the unix syntax 'editor +N filename', but |
|
258 | the IPython editor hook uses the unix syntax 'editor +N filename', but | |
255 | you can configure this by providing your own modified hook if your |
|
259 | you can configure this by providing your own modified hook if your | |
256 | favorite editor supports line-number specifications with a different |
|
260 | favorite editor supports line-number specifications with a different | |
257 | syntax. |
|
261 | syntax. | |
258 |
|
262 | |||
259 | -p: this will call the editor with the same data as the previous time |
|
263 | -p: this will call the editor with the same data as the previous time | |
260 | it was used, regardless of how long ago (in your current session) it |
|
264 | it was used, regardless of how long ago (in your current session) it | |
261 | was. |
|
265 | was. | |
262 |
|
266 | |||
263 | -r: use 'raw' input. This option only applies to input taken from the |
|
267 | -r: use 'raw' input. This option only applies to input taken from the | |
264 | user's history. By default, the 'processed' history is used, so that |
|
268 | user's history. By default, the 'processed' history is used, so that | |
265 | magics are loaded in their transformed version to valid Python. If |
|
269 | magics are loaded in their transformed version to valid Python. If | |
266 | this option is given, the raw input as typed as the command line is |
|
270 | this option is given, the raw input as typed as the command line is | |
267 | used instead. When you exit the editor, it will be executed by |
|
271 | used instead. When you exit the editor, it will be executed by | |
268 | IPython's own processor. |
|
272 | IPython's own processor. | |
269 |
|
273 | |||
270 | -x: do not execute the edited code immediately upon exit. This is |
|
274 | -x: do not execute the edited code immediately upon exit. This is | |
271 | mainly useful if you are editing programs which need to be called with |
|
275 | mainly useful if you are editing programs which need to be called with | |
272 | command line arguments, which you can then do using %run. |
|
276 | command line arguments, which you can then do using %run. | |
273 |
|
277 | |||
274 |
|
278 | |||
275 | Arguments: |
|
279 | Arguments: | |
276 |
|
280 | |||
277 | If arguments are given, the following possibilites exist: |
|
281 | If arguments are given, the following possibilites exist: | |
278 |
|
282 | |||
279 | - The arguments are numbers or pairs of colon-separated numbers (like |
|
283 | - The arguments are numbers or pairs of colon-separated numbers (like | |
280 | 1 4:8 9). These are interpreted as lines of previous input to be |
|
284 | 1 4:8 9). These are interpreted as lines of previous input to be | |
281 | loaded into the editor. The syntax is the same of the %macro command. |
|
285 | loaded into the editor. The syntax is the same of the %macro command. | |
282 |
|
286 | |||
283 | - If the argument doesn't start with a number, it is evaluated as a |
|
287 | - If the argument doesn't start with a number, it is evaluated as a | |
284 | variable and its contents loaded into the editor. You can thus edit |
|
288 | variable and its contents loaded into the editor. You can thus edit | |
285 | any string which contains python code (including the result of |
|
289 | any string which contains python code (including the result of | |
286 | previous edits). |
|
290 | previous edits). | |
287 |
|
291 | |||
288 | - If the argument is the name of an object (other than a string), |
|
292 | - If the argument is the name of an object (other than a string), | |
289 | IPython will try to locate the file where it was defined and open the |
|
293 | IPython will try to locate the file where it was defined and open the | |
290 | editor at the point where it is defined. You can use `%edit function` |
|
294 | editor at the point where it is defined. You can use `%edit function` | |
291 | to load an editor exactly at the point where 'function' is defined, |
|
295 | to load an editor exactly at the point where 'function' is defined, | |
292 | edit it and have the file be executed automatically. |
|
296 | edit it and have the file be executed automatically. | |
293 |
|
297 | |||
294 | If the object is a macro (see %macro for details), this opens up your |
|
298 | If the object is a macro (see %macro for details), this opens up your | |
295 | specified editor with a temporary file containing the macro's data. |
|
299 | specified editor with a temporary file containing the macro's data. | |
296 | Upon exit, the macro is reloaded with the contents of the file. |
|
300 | Upon exit, the macro is reloaded with the contents of the file. | |
297 |
|
301 | |||
298 | Note: opening at an exact line is only supported under Unix, and some |
|
302 | Note: opening at an exact line is only supported under Unix, and some | |
299 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
303 | editors (like kedit and gedit up to Gnome 2.8) do not understand the | |
300 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
304 | '+NUMBER' parameter necessary for this feature. Good editors like | |
301 | (X)Emacs, vi, jed, pico and joe all do. |
|
305 | (X)Emacs, vi, jed, pico and joe all do. | |
302 |
|
306 | |||
303 | - If the argument is not found as a variable, IPython will look for a |
|
307 | - If the argument is not found as a variable, IPython will look for a | |
304 | file with that name (adding .py if necessary) and load it into the |
|
308 | file with that name (adding .py if necessary) and load it into the | |
305 | editor. It will execute its contents with execfile() when you exit, |
|
309 | editor. It will execute its contents with execfile() when you exit, | |
306 | loading any code in the file into your interactive namespace. |
|
310 | loading any code in the file into your interactive namespace. | |
307 |
|
311 | |||
308 | After executing your code, %edit will return as output the code you |
|
312 | After executing your code, %edit will return as output the code you | |
309 | typed in the editor (except when it was an existing file). This way |
|
313 | typed in the editor (except when it was an existing file). This way | |
310 | you can reload the code in further invocations of %edit as a variable, |
|
314 | you can reload the code in further invocations of %edit as a variable, | |
311 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of |
|
315 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of | |
312 | the output. |
|
316 | the output. | |
313 |
|
317 | |||
314 | Note that %edit is also available through the alias %ed. |
|
318 | Note that %edit is also available through the alias %ed. | |
315 |
|
319 | |||
316 | This is an example of creating a simple function inside the editor and |
|
320 | This is an example of creating a simple function inside the editor and | |
317 | then modifying it. First, start up the editor: |
|
321 | then modifying it. First, start up the editor: | |
318 |
|
322 | |||
319 | In [1]: ed |
|
323 | In [1]: ed | |
320 | Editing... done. Executing edited code... |
|
324 | Editing... done. Executing edited code... | |
321 | Out[1]: 'def foo():n print "foo() was defined in an editing session"n' |
|
325 | Out[1]: 'def foo():n print "foo() was defined in an editing session"n' | |
322 |
|
326 | |||
323 | We can then call the function foo(): |
|
327 | We can then call the function foo(): | |
324 |
|
328 | |||
325 | In [2]: foo() |
|
329 | In [2]: foo() | |
326 | foo() was defined in an editing session |
|
330 | foo() was defined in an editing session | |
327 |
|
331 | |||
328 | Now we edit foo. IPython automatically loads the editor with the |
|
332 | Now we edit foo. IPython automatically loads the editor with the | |
329 | (temporary) file where foo() was previously defined: |
|
333 | (temporary) file where foo() was previously defined: | |
330 |
|
334 | |||
331 | In [3]: ed foo |
|
335 | In [3]: ed foo | |
332 | Editing... done. Executing edited code... |
|
336 | Editing... done. Executing edited code... | |
333 |
|
337 | |||
334 | And if we call foo() again we get the modified version: |
|
338 | And if we call foo() again we get the modified version: | |
335 |
|
339 | |||
336 | In [4]: foo() |
|
340 | In [4]: foo() | |
337 | foo() has now been changed! |
|
341 | foo() has now been changed! | |
338 |
|
342 | |||
339 | Here is an example of how to edit a code snippet successive |
|
343 | Here is an example of how to edit a code snippet successive | |
340 | times. First we call the editor: |
|
344 | times. First we call the editor: | |
341 |
|
345 | |||
342 | In [5]: ed |
|
346 | In [5]: ed | |
343 | Editing... done. Executing edited code... |
|
347 | Editing... done. Executing edited code... | |
344 | hello |
|
348 | hello | |
345 | Out[5]: "print 'hello'n" |
|
349 | Out[5]: "print 'hello'n" | |
346 |
|
350 | |||
347 | Now we call it again with the previous output (stored in _): |
|
351 | Now we call it again with the previous output (stored in _): | |
348 |
|
352 | |||
349 | In [6]: ed _ |
|
353 | In [6]: ed _ | |
350 | Editing... done. Executing edited code... |
|
354 | Editing... done. Executing edited code... | |
351 | hello world |
|
355 | hello world | |
352 | Out[6]: "print 'hello world'n" |
|
356 | Out[6]: "print 'hello world'n" | |
353 |
|
357 | |||
354 | Now we call it with the output #8 (stored in _8, also as Out[8]): |
|
358 | Now we call it with the output #8 (stored in _8, also as Out[8]): | |
355 |
|
359 | |||
356 | In [7]: ed _8 |
|
360 | In [7]: ed _8 | |
357 | Editing... done. Executing edited code... |
|
361 | Editing... done. Executing edited code... | |
358 | hello again |
|
362 | hello again | |
359 | Out[7]: "print 'hello again'n" |
|
363 | Out[7]: "print 'hello again'n" | |
360 | """ |
|
364 | """ | |
361 |
|
365 | |||
362 | opts,args = self.parse_options(parameter_s,'prn:') |
|
366 | opts,args = self.parse_options(parameter_s,'prn:') | |
363 |
|
367 | |||
364 | try: |
|
368 | try: | |
365 | filename, lineno, _ = self._find_edit_target(args, opts, last_call) |
|
369 | filename, lineno, _ = self._find_edit_target(args, opts, last_call) | |
366 | except MacroToEdit as e: |
|
370 | except MacroToEdit as e: | |
367 | # TODO: Implement macro editing over 2 processes. |
|
371 | # TODO: Implement macro editing over 2 processes. | |
368 | print("Macro editing not yet implemented in 2-process model.") |
|
372 | print("Macro editing not yet implemented in 2-process model.") | |
369 | return |
|
373 | return | |
370 |
|
374 | |||
371 | # Make sure we send to the client an absolute path, in case the working |
|
375 | # Make sure we send to the client an absolute path, in case the working | |
372 | # directory of client and kernel don't match |
|
376 | # directory of client and kernel don't match | |
373 | filename = os.path.abspath(filename) |
|
377 | filename = os.path.abspath(filename) | |
374 |
|
378 | |||
375 | payload = { |
|
379 | payload = { | |
376 | 'source' : 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic', |
|
380 | 'source' : 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic', | |
377 | 'filename' : filename, |
|
381 | 'filename' : filename, | |
378 | 'line_number' : lineno |
|
382 | 'line_number' : lineno | |
379 | } |
|
383 | } | |
380 | self.payload_manager.write_payload(payload) |
|
384 | self.payload_manager.write_payload(payload) | |
381 |
|
385 | |||
382 | def magic_gui(self, *args, **kwargs): |
|
386 | def magic_gui(self, *args, **kwargs): | |
383 | raise NotImplementedError( |
|
387 | raise NotImplementedError( | |
384 | 'Kernel GUI support is not implemented yet, except for --pylab.') |
|
388 | 'Kernel GUI support is not implemented yet, except for --pylab.') | |
385 |
|
389 | |||
386 | def magic_pylab(self, *args, **kwargs): |
|
390 | def magic_pylab(self, *args, **kwargs): | |
387 | raise NotImplementedError( |
|
391 | raise NotImplementedError( | |
388 | 'pylab support must be enabled in command line options.') |
|
392 | 'pylab support must be enabled in command line options.') | |
389 |
|
393 | |||
390 | # A few magics that are adapted to the specifics of using pexpect and a |
|
394 | # A few magics that are adapted to the specifics of using pexpect and a | |
391 | # remote terminal |
|
395 | # remote terminal | |
392 |
|
396 | |||
393 | def magic_clear(self, arg_s): |
|
397 | def magic_clear(self, arg_s): | |
394 | """Clear the terminal.""" |
|
398 | """Clear the terminal.""" | |
395 | if os.name == 'posix': |
|
399 | if os.name == 'posix': | |
396 | self.shell.system("clear") |
|
400 | self.shell.system("clear") | |
397 | else: |
|
401 | else: | |
398 | self.shell.system("cls") |
|
402 | self.shell.system("cls") | |
399 |
|
403 | |||
400 | if os.name == 'nt': |
|
404 | if os.name == 'nt': | |
401 | # This is the usual name in windows |
|
405 | # This is the usual name in windows | |
402 | magic_cls = magic_clear |
|
406 | magic_cls = magic_clear | |
403 |
|
407 | |||
404 | # Terminal pagers won't work over pexpect, but we do have our own pager |
|
408 | # Terminal pagers won't work over pexpect, but we do have our own pager | |
405 |
|
409 | |||
406 | def magic_less(self, arg_s): |
|
410 | def magic_less(self, arg_s): | |
407 | """Show a file through the pager. |
|
411 | """Show a file through the pager. | |
408 |
|
412 | |||
409 | Files ending in .py are syntax-highlighted.""" |
|
413 | Files ending in .py are syntax-highlighted.""" | |
410 | cont = open(arg_s).read() |
|
414 | cont = open(arg_s).read() | |
411 | if arg_s.endswith('.py'): |
|
415 | if arg_s.endswith('.py'): | |
412 | cont = self.shell.pycolorize(cont) |
|
416 | cont = self.shell.pycolorize(cont) | |
413 | page.page(cont) |
|
417 | page.page(cont) | |
414 |
|
418 | |||
415 | magic_more = magic_less |
|
419 | magic_more = magic_less | |
416 |
|
420 | |||
417 | # Man calls a pager, so we also need to redefine it |
|
421 | # Man calls a pager, so we also need to redefine it | |
418 | if os.name == 'posix': |
|
422 | if os.name == 'posix': | |
419 | def magic_man(self, arg_s): |
|
423 | def magic_man(self, arg_s): | |
420 | """Find the man page for the given command and display in pager.""" |
|
424 | """Find the man page for the given command and display in pager.""" | |
421 | page.page(self.shell.getoutput('man %s | col -b' % arg_s, |
|
425 | page.page(self.shell.getoutput('man %s | col -b' % arg_s, | |
422 | split=False)) |
|
426 | split=False)) | |
423 |
|
427 | |||
424 | # FIXME: this is specific to the GUI, so we should let the gui app load |
|
428 | # FIXME: this is specific to the GUI, so we should let the gui app load | |
425 | # magics at startup that are only for the gui. Once the gui app has proper |
|
429 | # magics at startup that are only for the gui. Once the gui app has proper | |
426 | # profile and configuration management, we can have it initialize a kernel |
|
430 | # profile and configuration management, we can have it initialize a kernel | |
427 | # with a special config file that provides these. |
|
431 | # with a special config file that provides these. | |
428 | def magic_guiref(self, arg_s): |
|
432 | def magic_guiref(self, arg_s): | |
429 | """Show a basic reference about the GUI console.""" |
|
433 | """Show a basic reference about the GUI console.""" | |
430 | from IPython.core.usage import gui_reference |
|
434 | from IPython.core.usage import gui_reference | |
431 | page.page(gui_reference, auto_html=True) |
|
435 | page.page(gui_reference, auto_html=True) | |
432 |
|
436 | |||
433 | def magic_connect_info(self, arg_s): |
|
437 | def magic_connect_info(self, arg_s): | |
434 | """Print information for connecting other clients to this kernel |
|
438 | """Print information for connecting other clients to this kernel | |
435 |
|
439 | |||
436 | It will print the contents of this session's connection file, as well as |
|
440 | It will print the contents of this session's connection file, as well as | |
437 | shortcuts for local clients. |
|
441 | shortcuts for local clients. | |
438 |
|
442 | |||
439 | In the simplest case, when called from the most recently launched kernel, |
|
443 | In the simplest case, when called from the most recently launched kernel, | |
440 | secondary clients can be connected, simply with: |
|
444 | secondary clients can be connected, simply with: | |
441 |
|
445 | |||
442 | $> ipython <app> --existing |
|
446 | $> ipython <app> --existing | |
443 |
|
447 | |||
444 | """ |
|
448 | """ | |
445 | from IPython.zmq.kernelapp import KernelApp |
|
|||
446 | if not KernelApp.initialized(): |
|
|||
447 | error("KernelApp is not initialized. I cannot find the connection info") |
|
|||
448 | return |
|
|||
449 | app = KernelApp.instance() |
|
|||
450 | try: |
|
449 | try: | |
451 | with open(app.connection_file) as f: |
|
450 | connection_file = get_connection_file() | |
452 | s = f.read() |
|
451 | info = get_connection_info(unpack=False) | |
453 | except Exception as e: |
|
452 | except Exception as e: | |
454 |
error("Could not |
|
453 | error("Could not get connection info: %r" % e) | |
455 | return |
|
454 | return | |
456 |
|
|
455 | ||
|
456 | print (info + '\n') | |||
457 | print ("Paste the above JSON into a file, and connect with:\n" |
|
457 | print ("Paste the above JSON into a file, and connect with:\n" | |
458 | " $> ipython <app> --existing <file>\n" |
|
458 | " $> ipython <app> --existing <file>\n" | |
459 | "or, if you are local, you can connect with just:\n" |
|
459 | "or, if you are local, you can connect with just:\n" | |
460 | " $> ipython <app> --existing %s\n" |
|
460 | " $> ipython <app> --existing %s\n" | |
461 | "or even just:\n" |
|
461 | "or even just:\n" | |
462 | " $> ipython <app> --existing\n" |
|
462 | " $> ipython <app> --existing\n" | |
463 | "if this is the most recent IPython session you have started." |
|
463 | "if this is the most recent IPython session you have started." | |
464 |
% os.path.basename( |
|
464 | % os.path.basename(connection_file) | |
465 | ) |
|
465 | ) | |
466 |
|
466 | |||
467 | def magic_qtconsole(self, arg_s): |
|
467 | def magic_qtconsole(self, arg_s): | |
468 | """Open a qtconsole connected to this kernel. |
|
468 | """Open a qtconsole connected to this kernel. | |
469 |
|
469 | |||
470 | Useful for connecting a qtconsole to running notebooks, for better |
|
470 | Useful for connecting a qtconsole to running notebooks, for better | |
471 | debugging. |
|
471 | debugging. | |
472 | """ |
|
472 | """ | |
473 | from IPython.zmq.kernelapp import KernelApp |
|
473 | try: | |
474 |
|
474 | p = connect_qtconsole(arg_split(arg_s, os.name=='posix')) | ||
475 | if not KernelApp.initialized(): |
|
475 | except Exception as e: | |
476 | error("KernelApp is not initialized. %qtconsole magic must be run from a Kernel") |
|
476 | error("Could not start qtconsole: %r" % e) | |
477 | return |
|
477 | return | |
478 | app = KernelApp.instance() |
|
|||
479 |
|
||||
480 | cmd = ';'.join([ |
|
|||
481 | "from IPython.frontend.qt.console import qtconsoleapp", |
|
|||
482 | "qtconsoleapp.main()" |
|
|||
483 | ]) |
|
|||
484 |
|
478 | |||
485 | return Popen([sys.executable, '-c', cmd, '--existing', app.connection_file], |
|
|||
486 | stdout=PIPE,stderr=PIPE) |
|
|||
487 |
|
479 | |||
488 | def set_next_input(self, text): |
|
480 | def set_next_input(self, text): | |
489 | """Send the specified text to the frontend to be presented at the next |
|
481 | """Send the specified text to the frontend to be presented at the next | |
490 | input cell.""" |
|
482 | input cell.""" | |
491 | payload = dict( |
|
483 | payload = dict( | |
492 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input', |
|
484 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input', | |
493 | text=text |
|
485 | text=text | |
494 | ) |
|
486 | ) | |
495 | self.payload_manager.write_payload(payload) |
|
487 | self.payload_manager.write_payload(payload) | |
496 |
|
488 | |||
497 | InteractiveShellABC.register(ZMQInteractiveShell) |
|
489 | InteractiveShellABC.register(ZMQInteractiveShell) |
General Comments 0
You need to be logged in to leave comments.
Login now