Show More
@@ -1,501 +1,513 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Top-level display functions for displaying object in different formats. |
|
2 | """Top-level display functions for displaying object in different formats. | |
3 |
|
3 | |||
4 | Authors: |
|
4 | Authors: | |
5 |
|
5 | |||
6 | * Brian Granger |
|
6 | * Brian Granger | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 | # Copyright (C) 2008-2011 The IPython Development Team |
|
10 | # Copyright (C) 2008-2011 The IPython Development Team | |
11 | # |
|
11 | # | |
12 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
13 | # the file COPYING, distributed as part of this software. |
|
13 | # the file COPYING, distributed as part of this software. | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | # Imports |
|
17 | # Imports | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
|
20 | from __future__ import print_function | |||
|
21 | ||||
20 | from xml.dom import minidom |
|
22 | from xml.dom import minidom | |
21 |
|
23 | |||
22 | from .displaypub import ( |
|
24 | from .displaypub import ( | |
23 | publish_pretty, publish_html, |
|
25 | publish_pretty, publish_html, | |
24 | publish_latex, publish_svg, |
|
26 | publish_latex, publish_svg, | |
25 | publish_png, publish_json, |
|
27 | publish_png, publish_json, | |
26 | publish_javascript, publish_jpeg |
|
28 | publish_javascript, publish_jpeg | |
27 | ) |
|
29 | ) | |
28 |
|
30 | |||
29 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
30 | # Main functions |
|
32 | # Main functions | |
31 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
32 |
|
34 | |||
33 | def display(*objs, **kwargs): |
|
35 | def display(*objs, **kwargs): | |
34 | """Display a Python object in all frontends. |
|
36 | """Display a Python object in all frontends. | |
35 |
|
37 | |||
36 | By default all representations will be computed and sent to the frontends. |
|
38 | By default all representations will be computed and sent to the frontends. | |
37 | Frontends can decide which representation is used and how. |
|
39 | Frontends can decide which representation is used and how. | |
38 |
|
40 | |||
39 | Parameters |
|
41 | Parameters | |
40 | ---------- |
|
42 | ---------- | |
41 | objs : tuple of objects |
|
43 | objs : tuple of objects | |
42 | The Python objects to display. |
|
44 | The Python objects to display. | |
43 | include : list or tuple, optional |
|
45 | include : list or tuple, optional | |
44 | A list of format type strings (MIME types) to include in the |
|
46 | A list of format type strings (MIME types) to include in the | |
45 | format data dict. If this is set *only* the format types included |
|
47 | format data dict. If this is set *only* the format types included | |
46 | in this list will be computed. |
|
48 | in this list will be computed. | |
47 | exclude : list or tuple, optional |
|
49 | exclude : list or tuple, optional | |
48 | A list of format type string (MIME types) to exclue in the format |
|
50 | A list of format type string (MIME types) to exclue in the format | |
49 | data dict. If this is set all format types will be computed, |
|
51 | data dict. If this is set all format types will be computed, | |
50 | except for those included in this argument. |
|
52 | except for those included in this argument. | |
51 | """ |
|
53 | """ | |
52 | include = kwargs.get('include') |
|
54 | include = kwargs.get('include') | |
53 | exclude = kwargs.get('exclude') |
|
55 | exclude = kwargs.get('exclude') | |
54 |
|
56 | |||
55 | from IPython.core.interactiveshell import InteractiveShell |
|
57 | from IPython.core.interactiveshell import InteractiveShell | |
56 | inst = InteractiveShell.instance() |
|
58 | inst = InteractiveShell.instance() | |
57 | format = inst.display_formatter.format |
|
59 | format = inst.display_formatter.format | |
58 | publish = inst.display_pub.publish |
|
60 | publish = inst.display_pub.publish | |
59 |
|
61 | |||
60 | for obj in objs: |
|
62 | for obj in objs: | |
61 | format_dict = format(obj, include=include, exclude=exclude) |
|
63 | format_dict = format(obj, include=include, exclude=exclude) | |
62 | publish('IPython.core.display.display', format_dict) |
|
64 | publish('IPython.core.display.display', format_dict) | |
63 |
|
65 | |||
64 |
|
66 | |||
65 | def display_pretty(*objs, **kwargs): |
|
67 | def display_pretty(*objs, **kwargs): | |
66 | """Display the pretty (default) representation of an object. |
|
68 | """Display the pretty (default) representation of an object. | |
67 |
|
69 | |||
68 | Parameters |
|
70 | Parameters | |
69 | ---------- |
|
71 | ---------- | |
70 | objs : tuple of objects |
|
72 | objs : tuple of objects | |
71 | The Python objects to display, or if raw=True raw text data to |
|
73 | The Python objects to display, or if raw=True raw text data to | |
72 | display. |
|
74 | display. | |
73 | raw : bool |
|
75 | raw : bool | |
74 | Are the data objects raw data or Python objects that need to be |
|
76 | Are the data objects raw data or Python objects that need to be | |
75 | formatted before display? [default: False] |
|
77 | formatted before display? [default: False] | |
76 | """ |
|
78 | """ | |
77 | raw = kwargs.pop('raw',False) |
|
79 | raw = kwargs.pop('raw',False) | |
78 | if raw: |
|
80 | if raw: | |
79 | for obj in objs: |
|
81 | for obj in objs: | |
80 | publish_pretty(obj) |
|
82 | publish_pretty(obj) | |
81 | else: |
|
83 | else: | |
82 | display(*objs, include=['text/plain']) |
|
84 | display(*objs, include=['text/plain']) | |
83 |
|
85 | |||
84 |
|
86 | |||
85 | def display_html(*objs, **kwargs): |
|
87 | def display_html(*objs, **kwargs): | |
86 | """Display the HTML representation of an object. |
|
88 | """Display the HTML representation of an object. | |
87 |
|
89 | |||
88 | Parameters |
|
90 | Parameters | |
89 | ---------- |
|
91 | ---------- | |
90 | objs : tuple of objects |
|
92 | objs : tuple of objects | |
91 | The Python objects to display, or if raw=True raw HTML data to |
|
93 | The Python objects to display, or if raw=True raw HTML data to | |
92 | display. |
|
94 | display. | |
93 | raw : bool |
|
95 | raw : bool | |
94 | Are the data objects raw data or Python objects that need to be |
|
96 | Are the data objects raw data or Python objects that need to be | |
95 | formatted before display? [default: False] |
|
97 | formatted before display? [default: False] | |
96 | """ |
|
98 | """ | |
97 | raw = kwargs.pop('raw',False) |
|
99 | raw = kwargs.pop('raw',False) | |
98 | if raw: |
|
100 | if raw: | |
99 | for obj in objs: |
|
101 | for obj in objs: | |
100 | publish_html(obj) |
|
102 | publish_html(obj) | |
101 | else: |
|
103 | else: | |
102 | display(*objs, include=['text/plain','text/html']) |
|
104 | display(*objs, include=['text/plain','text/html']) | |
103 |
|
105 | |||
104 |
|
106 | |||
105 | def display_svg(*objs, **kwargs): |
|
107 | def display_svg(*objs, **kwargs): | |
106 | """Display the SVG representation of an object. |
|
108 | """Display the SVG representation of an object. | |
107 |
|
109 | |||
108 | Parameters |
|
110 | Parameters | |
109 | ---------- |
|
111 | ---------- | |
110 | objs : tuple of objects |
|
112 | objs : tuple of objects | |
111 | The Python objects to display, or if raw=True raw svg data to |
|
113 | The Python objects to display, or if raw=True raw svg data to | |
112 | display. |
|
114 | display. | |
113 | raw : bool |
|
115 | raw : bool | |
114 | Are the data objects raw data or Python objects that need to be |
|
116 | Are the data objects raw data or Python objects that need to be | |
115 | formatted before display? [default: False] |
|
117 | formatted before display? [default: False] | |
116 | """ |
|
118 | """ | |
117 | raw = kwargs.pop('raw',False) |
|
119 | raw = kwargs.pop('raw',False) | |
118 | if raw: |
|
120 | if raw: | |
119 | for obj in objs: |
|
121 | for obj in objs: | |
120 | publish_svg(obj) |
|
122 | publish_svg(obj) | |
121 | else: |
|
123 | else: | |
122 | display(*objs, include=['text/plain','image/svg+xml']) |
|
124 | display(*objs, include=['text/plain','image/svg+xml']) | |
123 |
|
125 | |||
124 |
|
126 | |||
125 | def display_png(*objs, **kwargs): |
|
127 | def display_png(*objs, **kwargs): | |
126 | """Display the PNG representation of an object. |
|
128 | """Display the PNG representation of an object. | |
127 |
|
129 | |||
128 | Parameters |
|
130 | Parameters | |
129 | ---------- |
|
131 | ---------- | |
130 | objs : tuple of objects |
|
132 | objs : tuple of objects | |
131 | The Python objects to display, or if raw=True raw png data to |
|
133 | The Python objects to display, or if raw=True raw png data to | |
132 | display. |
|
134 | display. | |
133 | raw : bool |
|
135 | raw : bool | |
134 | Are the data objects raw data or Python objects that need to be |
|
136 | Are the data objects raw data or Python objects that need to be | |
135 | formatted before display? [default: False] |
|
137 | formatted before display? [default: False] | |
136 | """ |
|
138 | """ | |
137 | raw = kwargs.pop('raw',False) |
|
139 | raw = kwargs.pop('raw',False) | |
138 | if raw: |
|
140 | if raw: | |
139 | for obj in objs: |
|
141 | for obj in objs: | |
140 | publish_png(obj) |
|
142 | publish_png(obj) | |
141 | else: |
|
143 | else: | |
142 | display(*objs, include=['text/plain','image/png']) |
|
144 | display(*objs, include=['text/plain','image/png']) | |
143 |
|
145 | |||
144 |
|
146 | |||
145 | def display_jpeg(*objs, **kwargs): |
|
147 | def display_jpeg(*objs, **kwargs): | |
146 | """Display the JPEG representation of an object. |
|
148 | """Display the JPEG representation of an object. | |
147 |
|
149 | |||
148 | Parameters |
|
150 | Parameters | |
149 | ---------- |
|
151 | ---------- | |
150 | objs : tuple of objects |
|
152 | objs : tuple of objects | |
151 | The Python objects to display, or if raw=True raw JPEG data to |
|
153 | The Python objects to display, or if raw=True raw JPEG data to | |
152 | display. |
|
154 | display. | |
153 | raw : bool |
|
155 | raw : bool | |
154 | Are the data objects raw data or Python objects that need to be |
|
156 | Are the data objects raw data or Python objects that need to be | |
155 | formatted before display? [default: False] |
|
157 | formatted before display? [default: False] | |
156 | """ |
|
158 | """ | |
157 | raw = kwargs.pop('raw',False) |
|
159 | raw = kwargs.pop('raw',False) | |
158 | if raw: |
|
160 | if raw: | |
159 | for obj in objs: |
|
161 | for obj in objs: | |
160 | publish_jpeg(obj) |
|
162 | publish_jpeg(obj) | |
161 | else: |
|
163 | else: | |
162 | display(*objs, include=['text/plain','image/jpeg']) |
|
164 | display(*objs, include=['text/plain','image/jpeg']) | |
163 |
|
165 | |||
164 |
|
166 | |||
165 | def display_latex(*objs, **kwargs): |
|
167 | def display_latex(*objs, **kwargs): | |
166 | """Display the LaTeX representation of an object. |
|
168 | """Display the LaTeX representation of an object. | |
167 |
|
169 | |||
168 | Parameters |
|
170 | Parameters | |
169 | ---------- |
|
171 | ---------- | |
170 | objs : tuple of objects |
|
172 | objs : tuple of objects | |
171 | The Python objects to display, or if raw=True raw latex data to |
|
173 | The Python objects to display, or if raw=True raw latex data to | |
172 | display. |
|
174 | display. | |
173 | raw : bool |
|
175 | raw : bool | |
174 | Are the data objects raw data or Python objects that need to be |
|
176 | Are the data objects raw data or Python objects that need to be | |
175 | formatted before display? [default: False] |
|
177 | formatted before display? [default: False] | |
176 | """ |
|
178 | """ | |
177 | raw = kwargs.pop('raw',False) |
|
179 | raw = kwargs.pop('raw',False) | |
178 | if raw: |
|
180 | if raw: | |
179 | for obj in objs: |
|
181 | for obj in objs: | |
180 | publish_latex(obj) |
|
182 | publish_latex(obj) | |
181 | else: |
|
183 | else: | |
182 | display(*objs, include=['text/plain','text/latex']) |
|
184 | display(*objs, include=['text/plain','text/latex']) | |
183 |
|
185 | |||
184 |
|
186 | |||
185 | def display_json(*objs, **kwargs): |
|
187 | def display_json(*objs, **kwargs): | |
186 | """Display the JSON representation of an object. |
|
188 | """Display the JSON representation of an object. | |
187 |
|
189 | |||
188 | Parameters |
|
190 | Parameters | |
189 | ---------- |
|
191 | ---------- | |
190 | objs : tuple of objects |
|
192 | objs : tuple of objects | |
191 | The Python objects to display, or if raw=True raw json data to |
|
193 | The Python objects to display, or if raw=True raw json data to | |
192 | display. |
|
194 | display. | |
193 | raw : bool |
|
195 | raw : bool | |
194 | Are the data objects raw data or Python objects that need to be |
|
196 | Are the data objects raw data or Python objects that need to be | |
195 | formatted before display? [default: False] |
|
197 | formatted before display? [default: False] | |
196 | """ |
|
198 | """ | |
197 | raw = kwargs.pop('raw',False) |
|
199 | raw = kwargs.pop('raw',False) | |
198 | if raw: |
|
200 | if raw: | |
199 | for obj in objs: |
|
201 | for obj in objs: | |
200 | publish_json(obj) |
|
202 | publish_json(obj) | |
201 | else: |
|
203 | else: | |
202 | display(*objs, include=['text/plain','application/json']) |
|
204 | display(*objs, include=['text/plain','application/json']) | |
203 |
|
205 | |||
204 |
|
206 | |||
205 | def display_javascript(*objs, **kwargs): |
|
207 | def display_javascript(*objs, **kwargs): | |
206 | """Display the Javascript representation of an object. |
|
208 | """Display the Javascript representation of an object. | |
207 |
|
209 | |||
208 | Parameters |
|
210 | Parameters | |
209 | ---------- |
|
211 | ---------- | |
210 | objs : tuple of objects |
|
212 | objs : tuple of objects | |
211 | The Python objects to display, or if raw=True raw javascript data to |
|
213 | The Python objects to display, or if raw=True raw javascript data to | |
212 | display. |
|
214 | display. | |
213 | raw : bool |
|
215 | raw : bool | |
214 | Are the data objects raw data or Python objects that need to be |
|
216 | Are the data objects raw data or Python objects that need to be | |
215 | formatted before display? [default: False] |
|
217 | formatted before display? [default: False] | |
216 | """ |
|
218 | """ | |
217 | raw = kwargs.pop('raw',False) |
|
219 | raw = kwargs.pop('raw',False) | |
218 | if raw: |
|
220 | if raw: | |
219 | for obj in objs: |
|
221 | for obj in objs: | |
220 | publish_javascript(obj) |
|
222 | publish_javascript(obj) | |
221 | else: |
|
223 | else: | |
222 | display(*objs, include=['text/plain','application/javascript']) |
|
224 | display(*objs, include=['text/plain','application/javascript']) | |
223 |
|
225 | |||
224 | #----------------------------------------------------------------------------- |
|
226 | #----------------------------------------------------------------------------- | |
225 | # Smart classes |
|
227 | # Smart classes | |
226 | #----------------------------------------------------------------------------- |
|
228 | #----------------------------------------------------------------------------- | |
227 |
|
229 | |||
228 |
|
230 | |||
229 | class DisplayObject(object): |
|
231 | class DisplayObject(object): | |
230 | """An object that wraps data to be displayed.""" |
|
232 | """An object that wraps data to be displayed.""" | |
231 |
|
233 | |||
232 | _read_flags = 'r' |
|
234 | _read_flags = 'r' | |
233 |
|
235 | |||
234 | def __init__(self, data=None, url=None, filename=None): |
|
236 | def __init__(self, data=None, url=None, filename=None): | |
235 | """Create a display object given raw data. |
|
237 | """Create a display object given raw data. | |
236 |
|
238 | |||
237 | When this object is returned by an expression or passed to the |
|
239 | When this object is returned by an expression or passed to the | |
238 | display function, it will result in the data being displayed |
|
240 | display function, it will result in the data being displayed | |
239 | in the frontend. The MIME type of the data should match the |
|
241 | in the frontend. The MIME type of the data should match the | |
240 | subclasses used, so the Png subclass should be used for 'image/png' |
|
242 | subclasses used, so the Png subclass should be used for 'image/png' | |
241 | data. If the data is a URL, the data will first be downloaded |
|
243 | data. If the data is a URL, the data will first be downloaded | |
242 | and then displayed. If |
|
244 | and then displayed. If | |
243 |
|
245 | |||
244 | Parameters |
|
246 | Parameters | |
245 | ---------- |
|
247 | ---------- | |
246 | data : unicode, str or bytes |
|
248 | data : unicode, str or bytes | |
247 | The raw data or a URL to download the data from. |
|
249 | The raw data or a URL to download the data from. | |
248 | url : unicode |
|
250 | url : unicode | |
249 | A URL to download the data from. |
|
251 | A URL to download the data from. | |
250 | filename : unicode |
|
252 | filename : unicode | |
251 | Path to a local file to load the data from. |
|
253 | Path to a local file to load the data from. | |
252 | """ |
|
254 | """ | |
253 | if data is not None and data.startswith('http'): |
|
255 | if data is not None and data.startswith('http'): | |
254 | self.url = data |
|
256 | self.url = data | |
255 | self.filename = None |
|
257 | self.filename = None | |
256 | self.data = None |
|
258 | self.data = None | |
257 | else: |
|
259 | else: | |
258 | self.data = data |
|
260 | self.data = data | |
259 | self.url = url |
|
261 | self.url = url | |
260 | self.filename = None if filename is None else unicode(filename) |
|
262 | self.filename = None if filename is None else unicode(filename) | |
261 | self.reload() |
|
263 | self.reload() | |
262 |
|
264 | |||
263 | def reload(self): |
|
265 | def reload(self): | |
264 | """Reload the raw data from file or URL.""" |
|
266 | """Reload the raw data from file or URL.""" | |
265 | if self.filename is not None: |
|
267 | if self.filename is not None: | |
266 | with open(self.filename, self._read_flags) as f: |
|
268 | with open(self.filename, self._read_flags) as f: | |
267 | self.data = f.read() |
|
269 | self.data = f.read() | |
268 | elif self.url is not None: |
|
270 | elif self.url is not None: | |
269 | try: |
|
271 | try: | |
270 | import urllib2 |
|
272 | import urllib2 | |
271 | response = urllib2.urlopen(self.url) |
|
273 | response = urllib2.urlopen(self.url) | |
272 | self.data = response.read() |
|
274 | self.data = response.read() | |
273 | # extract encoding from header, if there is one: |
|
275 | # extract encoding from header, if there is one: | |
274 | encoding = None |
|
276 | encoding = None | |
275 | for sub in response.headers['content-type'].split(';'): |
|
277 | for sub in response.headers['content-type'].split(';'): | |
276 | sub = sub.strip() |
|
278 | sub = sub.strip() | |
277 | if sub.startswith('charset'): |
|
279 | if sub.startswith('charset'): | |
278 | encoding = sub.split('=')[-1].strip() |
|
280 | encoding = sub.split('=')[-1].strip() | |
279 | break |
|
281 | break | |
280 | # decode data, if an encoding was specified |
|
282 | # decode data, if an encoding was specified | |
281 | if encoding: |
|
283 | if encoding: | |
282 | self.data = self.data.decode(encoding, 'replace') |
|
284 | self.data = self.data.decode(encoding, 'replace') | |
283 | except: |
|
285 | except: | |
284 | self.data = None |
|
286 | self.data = None | |
285 |
|
287 | |||
286 | class Pretty(DisplayObject): |
|
288 | class Pretty(DisplayObject): | |
287 |
|
289 | |||
288 | def _repr_pretty_(self): |
|
290 | def _repr_pretty_(self): | |
289 | return self.data |
|
291 | return self.data | |
290 |
|
292 | |||
291 |
|
293 | |||
292 | class HTML(DisplayObject): |
|
294 | class HTML(DisplayObject): | |
293 |
|
295 | |||
294 | def _repr_html_(self): |
|
296 | def _repr_html_(self): | |
295 | return self.data |
|
297 | return self.data | |
296 |
|
298 | |||
297 |
|
299 | |||
298 | class Math(DisplayObject): |
|
300 | class Math(DisplayObject): | |
299 |
|
301 | |||
300 | def _repr_latex_(self): |
|
302 | def _repr_latex_(self): | |
301 | s = self.data.strip('$') |
|
303 | s = self.data.strip('$') | |
302 | return "$$%s$$" % s |
|
304 | return "$$%s$$" % s | |
303 |
|
305 | |||
304 |
|
306 | |||
305 | class Latex(DisplayObject): |
|
307 | class Latex(DisplayObject): | |
306 |
|
308 | |||
307 | def _repr_latex_(self): |
|
309 | def _repr_latex_(self): | |
308 | return self.data |
|
310 | return self.data | |
309 |
|
311 | |||
310 |
|
312 | |||
311 | class SVG(DisplayObject): |
|
313 | class SVG(DisplayObject): | |
312 |
|
314 | |||
313 | # wrap data in a property, which extracts the <svg> tag, discarding |
|
315 | # wrap data in a property, which extracts the <svg> tag, discarding | |
314 | # document headers |
|
316 | # document headers | |
315 | _data = None |
|
317 | _data = None | |
316 |
|
318 | |||
317 | @property |
|
319 | @property | |
318 | def data(self): |
|
320 | def data(self): | |
319 | return self._data |
|
321 | return self._data | |
320 |
|
322 | |||
321 | @data.setter |
|
323 | @data.setter | |
322 | def data(self, svg): |
|
324 | def data(self, svg): | |
323 | if svg is None: |
|
325 | if svg is None: | |
324 | self._data = None |
|
326 | self._data = None | |
325 | return |
|
327 | return | |
326 | # parse into dom object |
|
328 | # parse into dom object | |
327 | x = minidom.parseString(svg) |
|
329 | x = minidom.parseString(svg) | |
328 | # get svg tag (should be 1) |
|
330 | # get svg tag (should be 1) | |
329 | found_svg = x.getElementsByTagName('svg') |
|
331 | found_svg = x.getElementsByTagName('svg') | |
330 | if found_svg: |
|
332 | if found_svg: | |
331 | svg = found_svg[0].toxml() |
|
333 | svg = found_svg[0].toxml() | |
332 | else: |
|
334 | else: | |
333 | # fallback on the input, trust the user |
|
335 | # fallback on the input, trust the user | |
334 | # but this is probably an error. |
|
336 | # but this is probably an error. | |
335 | pass |
|
337 | pass | |
336 | self._data = svg |
|
338 | self._data = svg | |
337 |
|
339 | |||
338 | def _repr_svg_(self): |
|
340 | def _repr_svg_(self): | |
339 | return self.data |
|
341 | return self.data | |
340 |
|
342 | |||
341 |
|
343 | |||
342 | class JSON(DisplayObject): |
|
344 | class JSON(DisplayObject): | |
343 |
|
345 | |||
344 | def _repr_json_(self): |
|
346 | def _repr_json_(self): | |
345 | return self.data |
|
347 | return self.data | |
346 |
|
348 | |||
347 | css_t = """$("head").append($("<link/>").attr({ |
|
349 | css_t = """$("head").append($("<link/>").attr({ | |
348 | rel: "stylesheet", |
|
350 | rel: "stylesheet", | |
349 | type: "text/css", |
|
351 | type: "text/css", | |
350 | href: "%s" |
|
352 | href: "%s" | |
351 | })); |
|
353 | })); | |
352 | """ |
|
354 | """ | |
353 |
|
355 | |||
354 | lib_t1 = """$.getScript("%s", function () { |
|
356 | lib_t1 = """$.getScript("%s", function () { | |
355 | """ |
|
357 | """ | |
356 | lib_t2 = """}); |
|
358 | lib_t2 = """}); | |
357 | """ |
|
359 | """ | |
358 |
|
360 | |||
359 | class Javascript(DisplayObject): |
|
361 | class Javascript(DisplayObject): | |
360 |
|
362 | |||
361 | def __init__(self, data=None, url=None, filename=None, lib=None, css=None): |
|
363 | def __init__(self, data=None, url=None, filename=None, lib=None, css=None): | |
362 | """Create a Javascript display object given raw data. |
|
364 | """Create a Javascript display object given raw data. | |
363 |
|
365 | |||
364 | When this object is returned by an expression or passed to the |
|
366 | When this object is returned by an expression or passed to the | |
365 | display function, it will result in the data being displayed |
|
367 | display function, it will result in the data being displayed | |
366 | in the frontend. If the data is a URL, the data will first be |
|
368 | in the frontend. If the data is a URL, the data will first be | |
367 | downloaded and then displayed. |
|
369 | downloaded and then displayed. | |
368 |
|
370 | |||
369 | Parameters |
|
371 | Parameters | |
370 | ---------- |
|
372 | ---------- | |
371 | data : unicode, str or bytes |
|
373 | data : unicode, str or bytes | |
372 | The Javascript source code or a URL to download it from. |
|
374 | The Javascript source code or a URL to download it from. | |
373 | url : unicode |
|
375 | url : unicode | |
374 | A URL to download the data from. |
|
376 | A URL to download the data from. | |
375 | filename : unicode |
|
377 | filename : unicode | |
376 | Path to a local file to load the data from. |
|
378 | Path to a local file to load the data from. | |
377 | lib : list or str |
|
379 | lib : list or str | |
378 | A sequence of Javascript library URLs to load asynchronously before |
|
380 | A sequence of Javascript library URLs to load asynchronously before | |
379 | running the source code. The full URLs of the libraries should |
|
381 | running the source code. The full URLs of the libraries should | |
380 | be given. A single Javascript library URL can also be given as a |
|
382 | be given. A single Javascript library URL can also be given as a | |
381 | string. |
|
383 | string. | |
382 | css: : list or str |
|
384 | css: : list or str | |
383 | A sequence of css files to load before running the source code. |
|
385 | A sequence of css files to load before running the source code. | |
384 | The full URLs of the css files should be give. A single css URL |
|
386 | The full URLs of the css files should be give. A single css URL | |
385 | can also be given as a string. |
|
387 | can also be given as a string. | |
386 | """ |
|
388 | """ | |
387 | if isinstance(lib, basestring): |
|
389 | if isinstance(lib, basestring): | |
388 | lib = [lib] |
|
390 | lib = [lib] | |
389 | elif lib is None: |
|
391 | elif lib is None: | |
390 | lib = [] |
|
392 | lib = [] | |
391 | if isinstance(css, basestring): |
|
393 | if isinstance(css, basestring): | |
392 | css = [css] |
|
394 | css = [css] | |
393 | elif css is None: |
|
395 | elif css is None: | |
394 | css = [] |
|
396 | css = [] | |
395 | if not isinstance(lib, (list,tuple)): |
|
397 | if not isinstance(lib, (list,tuple)): | |
396 | raise TypeError('expected sequence, got: %r' % lib) |
|
398 | raise TypeError('expected sequence, got: %r' % lib) | |
397 | if not isinstance(css, (list,tuple)): |
|
399 | if not isinstance(css, (list,tuple)): | |
398 | raise TypeError('expected sequence, got: %r' % css) |
|
400 | raise TypeError('expected sequence, got: %r' % css) | |
399 | self.lib = lib |
|
401 | self.lib = lib | |
400 | self.css = css |
|
402 | self.css = css | |
401 | super(Javascript, self).__init__(data=data, url=url, filename=filename) |
|
403 | super(Javascript, self).__init__(data=data, url=url, filename=filename) | |
402 |
|
404 | |||
403 | def _repr_javascript_(self): |
|
405 | def _repr_javascript_(self): | |
404 | r = '' |
|
406 | r = '' | |
405 | for c in self.css: |
|
407 | for c in self.css: | |
406 | r += css_t % c |
|
408 | r += css_t % c | |
407 | for l in self.lib: |
|
409 | for l in self.lib: | |
408 | r += lib_t1 % l |
|
410 | r += lib_t1 % l | |
409 | r += self.data |
|
411 | r += self.data | |
410 | r += lib_t2*len(self.lib) |
|
412 | r += lib_t2*len(self.lib) | |
411 | return r |
|
413 | return r | |
412 |
|
414 | |||
413 |
|
415 | |||
414 | class Image(DisplayObject): |
|
416 | class Image(DisplayObject): | |
415 |
|
417 | |||
416 | _read_flags = 'rb' |
|
418 | _read_flags = 'rb' | |
417 |
|
419 | |||
418 | def __init__(self, data=None, url=None, filename=None, format=u'png', embed=False): |
|
420 | def __init__(self, data=None, url=None, filename=None, format=u'png', embed=False): | |
419 | """Create a display an PNG/JPEG image given raw data. |
|
421 | """Create a display an PNG/JPEG image given raw data. | |
420 |
|
422 | |||
421 | When this object is returned by an expression or passed to the |
|
423 | When this object is returned by an expression or passed to the | |
422 | display function, it will result in the image being displayed |
|
424 | display function, it will result in the image being displayed | |
423 | in the frontend. |
|
425 | in the frontend. | |
424 |
|
426 | |||
425 | Parameters |
|
427 | Parameters | |
426 | ---------- |
|
428 | ---------- | |
427 | data : unicode, str or bytes |
|
429 | data : unicode, str or bytes | |
428 | The raw data or a URL to download the data from. |
|
430 | The raw data or a URL to download the data from. | |
429 | url : unicode |
|
431 | url : unicode | |
430 | A URL to download the data from. |
|
432 | A URL to download the data from. | |
431 | filename : unicode |
|
433 | filename : unicode | |
432 | Path to a local file to load the data from. |
|
434 | Path to a local file to load the data from. | |
433 | format : unicode |
|
435 | format : unicode | |
434 | The format of the image data (png/jpeg/jpg). If a filename or URL is given |
|
436 | The format of the image data (png/jpeg/jpg). If a filename or URL is given | |
435 | for format will be inferred from the filename extension. |
|
437 | for format will be inferred from the filename extension. | |
436 | embed : bool |
|
438 | embed : bool | |
437 | Should the image data be embedded in the notebook using a data URI (True) |
|
439 | Should the image data be embedded in the notebook using a data URI (True) | |
438 | or be loaded using an <img> tag. Set this to True if you want the image |
|
440 | or be loaded using an <img> tag. Set this to True if you want the image | |
439 | to be viewable later with no internet connection. If a filename is given |
|
441 | to be viewable later with no internet connection. If a filename is given | |
440 | embed is always set to True. |
|
442 | embed is always set to True. | |
441 | """ |
|
443 | """ | |
442 | if filename is not None: |
|
444 | if filename is not None: | |
443 | ext = self._find_ext(filename) |
|
445 | ext = self._find_ext(filename) | |
444 | elif url is not None: |
|
446 | elif url is not None: | |
445 | ext = self._find_ext(url) |
|
447 | ext = self._find_ext(url) | |
446 | elif data.startswith('http'): |
|
448 | elif data.startswith('http'): | |
447 | ext = self._find_ext(data) |
|
449 | ext = self._find_ext(data) | |
448 | else: |
|
450 | else: | |
449 | ext = None |
|
451 | ext = None | |
450 | if ext is not None: |
|
452 | if ext is not None: | |
451 | if ext == u'jpg' or ext == u'jpeg': |
|
453 | if ext == u'jpg' or ext == u'jpeg': | |
452 | format = u'jpeg' |
|
454 | format = u'jpeg' | |
453 | if ext == u'png': |
|
455 | if ext == u'png': | |
454 | format = u'png' |
|
456 | format = u'png' | |
455 | self.format = unicode(format).lower() |
|
457 | self.format = unicode(format).lower() | |
456 | self.embed = True if filename is not None else embed |
|
458 | self.embed = True if filename is not None else embed | |
457 | super(Image, self).__init__(data=data, url=url, filename=filename) |
|
459 | super(Image, self).__init__(data=data, url=url, filename=filename) | |
458 |
|
460 | |||
459 | def reload(self): |
|
461 | def reload(self): | |
460 | """Reload the raw data from file or URL.""" |
|
462 | """Reload the raw data from file or URL.""" | |
461 | if self.embed: |
|
463 | if self.embed: | |
462 | super(Image,self).reload() |
|
464 | super(Image,self).reload() | |
463 |
|
465 | |||
464 | def _repr_html_(self): |
|
466 | def _repr_html_(self): | |
465 | if not self.embed: |
|
467 | if not self.embed: | |
466 | return u'<img src="%s" />' % self.url |
|
468 | return u'<img src="%s" />' % self.url | |
467 |
|
469 | |||
468 | def _repr_png_(self): |
|
470 | def _repr_png_(self): | |
469 | if self.embed and self.format == u'png': |
|
471 | if self.embed and self.format == u'png': | |
470 | return self.data |
|
472 | return self.data | |
471 |
|
473 | |||
472 | def _repr_jpeg_(self): |
|
474 | def _repr_jpeg_(self): | |
473 | if self.embed and (self.format == u'jpeg' or self.format == u'jpg'): |
|
475 | if self.embed and (self.format == u'jpeg' or self.format == u'jpg'): | |
474 | return self.data |
|
476 | return self.data | |
475 |
|
477 | |||
476 | def _find_ext(self, s): |
|
478 | def _find_ext(self, s): | |
477 | return unicode(s.split('.')[-1].lower()) |
|
479 | return unicode(s.split('.')[-1].lower()) | |
478 |
|
480 | |||
479 |
|
481 | |||
480 | def clear_output(stdout=True, stderr=True, other=True): |
|
482 | def clear_output(stdout=True, stderr=True, other=True): | |
481 | """Clear the output of the current cell receiving output. |
|
483 | """Clear the output of the current cell receiving output. | |
482 |
|
484 | |||
483 | Optionally, each of stdout/stderr or other non-stream data (e.g. anything |
|
485 | Optionally, each of stdout/stderr or other non-stream data (e.g. anything | |
484 | produced by display()) can be excluded from the clear event. |
|
486 | produced by display()) can be excluded from the clear event. | |
485 |
|
487 | |||
486 | By default, everything is cleared. |
|
488 | By default, everything is cleared. | |
487 |
|
489 | |||
488 | Parameters |
|
490 | Parameters | |
489 | ---------- |
|
491 | ---------- | |
490 | stdout : bool [default: True] |
|
492 | stdout : bool [default: True] | |
491 | Whether to clear stdout. |
|
493 | Whether to clear stdout. | |
492 | stderr : bool [default: True] |
|
494 | stderr : bool [default: True] | |
493 | Whether to clear stderr. |
|
495 | Whether to clear stderr. | |
494 | other : bool [default: True] |
|
496 | other : bool [default: True] | |
495 | Whether to clear everything else that is not stdout/stderr |
|
497 | Whether to clear everything else that is not stdout/stderr | |
496 | (e.g. figures,images,HTML, any result of display()). |
|
498 | (e.g. figures,images,HTML, any result of display()). | |
497 | """ |
|
499 | """ | |
498 | from IPython.core.interactiveshell import InteractiveShell |
|
500 | from IPython.core.interactiveshell import InteractiveShell | |
499 | InteractiveShell.instance().display_pub.clear_output( |
|
501 | if InteractiveShell.initialized(): | |
500 | stdout=stdout, stderr=stderr, other=other, |
|
502 | InteractiveShell.instance().display_pub.clear_output( | |
501 | ) |
|
503 | stdout=stdout, stderr=stderr, other=other, | |
|
504 | ) | |||
|
505 | else: | |||
|
506 | from IPython.utils import io | |||
|
507 | if stdout: | |||
|
508 | print('\033[2K\r', file=io.stdout, end='') | |||
|
509 | io.stdout.flush() | |||
|
510 | if stderr: | |||
|
511 | print('\033[2K\r', file=io.stderr, end='') | |||
|
512 | io.stderr.flush() | |||
|
513 |
@@ -1,302 +1,309 b'' | |||||
1 | """An interface for publishing rich data to frontends. |
|
1 | """An interface for publishing rich data to frontends. | |
2 |
|
2 | |||
3 | There are two components of the display system: |
|
3 | There are two components of the display system: | |
4 |
|
4 | |||
5 | * Display formatters, which take a Python object and compute the |
|
5 | * Display formatters, which take a Python object and compute the | |
6 | representation of the object in various formats (text, HTML, SVg, etc.). |
|
6 | representation of the object in various formats (text, HTML, SVg, etc.). | |
7 | * The display publisher that is used to send the representation data to the |
|
7 | * The display publisher that is used to send the representation data to the | |
8 | various frontends. |
|
8 | various frontends. | |
9 |
|
9 | |||
10 | This module defines the logic display publishing. The display publisher uses |
|
10 | This module defines the logic display publishing. The display publisher uses | |
11 | the ``display_data`` message type that is defined in the IPython messaging |
|
11 | the ``display_data`` message type that is defined in the IPython messaging | |
12 | spec. |
|
12 | spec. | |
13 |
|
13 | |||
14 | Authors: |
|
14 | Authors: | |
15 |
|
15 | |||
16 | * Brian Granger |
|
16 | * Brian Granger | |
17 | """ |
|
17 | """ | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Copyright (C) 2008-2011 The IPython Development Team |
|
20 | # Copyright (C) 2008-2011 The IPython Development Team | |
21 | # |
|
21 | # | |
22 | # Distributed under the terms of the BSD License. The full license is in |
|
22 | # Distributed under the terms of the BSD License. The full license is in | |
23 | # the file COPYING, distributed as part of this software. |
|
23 | # the file COPYING, distributed as part of this software. | |
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Imports |
|
27 | # Imports | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 |
|
29 | |||
30 | from __future__ import print_function |
|
30 | from __future__ import print_function | |
31 |
|
31 | |||
32 | from IPython.config.configurable import Configurable |
|
32 | from IPython.config.configurable import Configurable | |
|
33 | from IPython.utils import io | |||
33 |
|
34 | |||
34 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
35 | # Main payload class |
|
36 | # Main payload class | |
36 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
37 |
|
38 | |||
38 | class DisplayPublisher(Configurable): |
|
39 | class DisplayPublisher(Configurable): | |
39 | """A traited class that publishes display data to frontends. |
|
40 | """A traited class that publishes display data to frontends. | |
40 |
|
41 | |||
41 | Instances of this class are created by the main IPython object and should |
|
42 | Instances of this class are created by the main IPython object and should | |
42 | be accessed there. |
|
43 | be accessed there. | |
43 | """ |
|
44 | """ | |
44 |
|
45 | |||
45 | def _validate_data(self, source, data, metadata=None): |
|
46 | def _validate_data(self, source, data, metadata=None): | |
46 | """Validate the display data. |
|
47 | """Validate the display data. | |
47 |
|
48 | |||
48 | Parameters |
|
49 | Parameters | |
49 | ---------- |
|
50 | ---------- | |
50 | source : str |
|
51 | source : str | |
51 | The fully dotted name of the callable that created the data, like |
|
52 | The fully dotted name of the callable that created the data, like | |
52 | :func:`foo.bar.my_formatter`. |
|
53 | :func:`foo.bar.my_formatter`. | |
53 | data : dict |
|
54 | data : dict | |
54 | The formata data dictionary. |
|
55 | The formata data dictionary. | |
55 | metadata : dict |
|
56 | metadata : dict | |
56 | Any metadata for the data. |
|
57 | Any metadata for the data. | |
57 | """ |
|
58 | """ | |
58 |
|
59 | |||
59 | if not isinstance(source, basestring): |
|
60 | if not isinstance(source, basestring): | |
60 | raise TypeError('source must be a str, got: %r' % source) |
|
61 | raise TypeError('source must be a str, got: %r' % source) | |
61 | if not isinstance(data, dict): |
|
62 | if not isinstance(data, dict): | |
62 | raise TypeError('data must be a dict, got: %r' % data) |
|
63 | raise TypeError('data must be a dict, got: %r' % data) | |
63 | if metadata is not None: |
|
64 | if metadata is not None: | |
64 | if not isinstance(metadata, dict): |
|
65 | if not isinstance(metadata, dict): | |
65 | raise TypeError('metadata must be a dict, got: %r' % data) |
|
66 | raise TypeError('metadata must be a dict, got: %r' % data) | |
66 |
|
67 | |||
67 | def publish(self, source, data, metadata=None): |
|
68 | def publish(self, source, data, metadata=None): | |
68 | """Publish data and metadata to all frontends. |
|
69 | """Publish data and metadata to all frontends. | |
69 |
|
70 | |||
70 | See the ``display_data`` message in the messaging documentation for |
|
71 | See the ``display_data`` message in the messaging documentation for | |
71 | more details about this message type. |
|
72 | more details about this message type. | |
72 |
|
73 | |||
73 | The following MIME types are currently implemented: |
|
74 | The following MIME types are currently implemented: | |
74 |
|
75 | |||
75 | * text/plain |
|
76 | * text/plain | |
76 | * text/html |
|
77 | * text/html | |
77 | * text/latex |
|
78 | * text/latex | |
78 | * application/json |
|
79 | * application/json | |
79 | * application/javascript |
|
80 | * application/javascript | |
80 | * image/png |
|
81 | * image/png | |
81 | * image/jpeg |
|
82 | * image/jpeg | |
82 | * image/svg+xml |
|
83 | * image/svg+xml | |
83 |
|
84 | |||
84 | Parameters |
|
85 | Parameters | |
85 | ---------- |
|
86 | ---------- | |
86 | source : str |
|
87 | source : str | |
87 | A string that give the function or method that created the data, |
|
88 | A string that give the function or method that created the data, | |
88 | such as 'IPython.core.page'. |
|
89 | such as 'IPython.core.page'. | |
89 | data : dict |
|
90 | data : dict | |
90 | A dictionary having keys that are valid MIME types (like |
|
91 | A dictionary having keys that are valid MIME types (like | |
91 | 'text/plain' or 'image/svg+xml') and values that are the data for |
|
92 | 'text/plain' or 'image/svg+xml') and values that are the data for | |
92 | that MIME type. The data itself must be a JSON'able data |
|
93 | that MIME type. The data itself must be a JSON'able data | |
93 | structure. Minimally all data should have the 'text/plain' data, |
|
94 | structure. Minimally all data should have the 'text/plain' data, | |
94 | which can be displayed by all frontends. If more than the plain |
|
95 | which can be displayed by all frontends. If more than the plain | |
95 | text is given, it is up to the frontend to decide which |
|
96 | text is given, it is up to the frontend to decide which | |
96 | representation to use. |
|
97 | representation to use. | |
97 | metadata : dict |
|
98 | metadata : dict | |
98 | A dictionary for metadata related to the data. This can contain |
|
99 | A dictionary for metadata related to the data. This can contain | |
99 | arbitrary key, value pairs that frontends can use to interpret |
|
100 | arbitrary key, value pairs that frontends can use to interpret | |
100 | the data. |
|
101 | the data. | |
101 | """ |
|
102 | """ | |
102 | from IPython.utils import io |
|
103 | ||
103 | # The default is to simply write the plain text data using io.stdout. |
|
104 | # The default is to simply write the plain text data using io.stdout. | |
104 | if data.has_key('text/plain'): |
|
105 | if data.has_key('text/plain'): | |
105 | print(data['text/plain'], file=io.stdout) |
|
106 | print(data['text/plain'], file=io.stdout) | |
106 |
|
107 | |||
107 |
|
|
108 | def clear_output(self, stdout=True, stderr=True, other=True): | |
108 |
|
|
109 | """Clear the output of the cell receiving output.""" | |
109 | pass |
|
110 | if stdout: | |
|
111 | print('\033[2K\r', file=io.stdout, end='') | |||
|
112 | io.stdout.flush() | |||
|
113 | if stderr: | |||
|
114 | print('\033[2K\r', file=io.stderr, end='') | |||
|
115 | io.stderr.flush() | |||
|
116 | ||||
110 |
|
117 | |||
111 |
|
118 | |||
112 | def publish_display_data(source, data, metadata=None): |
|
119 | def publish_display_data(source, data, metadata=None): | |
113 | """Publish data and metadata to all frontends. |
|
120 | """Publish data and metadata to all frontends. | |
114 |
|
121 | |||
115 | See the ``display_data`` message in the messaging documentation for |
|
122 | See the ``display_data`` message in the messaging documentation for | |
116 | more details about this message type. |
|
123 | more details about this message type. | |
117 |
|
124 | |||
118 | The following MIME types are currently implemented: |
|
125 | The following MIME types are currently implemented: | |
119 |
|
126 | |||
120 | * text/plain |
|
127 | * text/plain | |
121 | * text/html |
|
128 | * text/html | |
122 | * text/latex |
|
129 | * text/latex | |
123 | * application/json |
|
130 | * application/json | |
124 | * application/javascript |
|
131 | * application/javascript | |
125 | * image/png |
|
132 | * image/png | |
126 | * image/jpeg |
|
133 | * image/jpeg | |
127 | * image/svg+xml |
|
134 | * image/svg+xml | |
128 |
|
135 | |||
129 | Parameters |
|
136 | Parameters | |
130 | ---------- |
|
137 | ---------- | |
131 | source : str |
|
138 | source : str | |
132 | A string that give the function or method that created the data, |
|
139 | A string that give the function or method that created the data, | |
133 | such as 'IPython.core.page'. |
|
140 | such as 'IPython.core.page'. | |
134 | data : dict |
|
141 | data : dict | |
135 | A dictionary having keys that are valid MIME types (like |
|
142 | A dictionary having keys that are valid MIME types (like | |
136 | 'text/plain' or 'image/svg+xml') and values that are the data for |
|
143 | 'text/plain' or 'image/svg+xml') and values that are the data for | |
137 | that MIME type. The data itself must be a JSON'able data |
|
144 | that MIME type. The data itself must be a JSON'able data | |
138 | structure. Minimally all data should have the 'text/plain' data, |
|
145 | structure. Minimally all data should have the 'text/plain' data, | |
139 | which can be displayed by all frontends. If more than the plain |
|
146 | which can be displayed by all frontends. If more than the plain | |
140 | text is given, it is up to the frontend to decide which |
|
147 | text is given, it is up to the frontend to decide which | |
141 | representation to use. |
|
148 | representation to use. | |
142 | metadata : dict |
|
149 | metadata : dict | |
143 | A dictionary for metadata related to the data. This can contain |
|
150 | A dictionary for metadata related to the data. This can contain | |
144 | arbitrary key, value pairs that frontends can use to interpret |
|
151 | arbitrary key, value pairs that frontends can use to interpret | |
145 | the data. |
|
152 | the data. | |
146 | """ |
|
153 | """ | |
147 | from IPython.core.interactiveshell import InteractiveShell |
|
154 | from IPython.core.interactiveshell import InteractiveShell | |
148 | InteractiveShell.instance().display_pub.publish( |
|
155 | InteractiveShell.instance().display_pub.publish( | |
149 | source, |
|
156 | source, | |
150 | data, |
|
157 | data, | |
151 | metadata |
|
158 | metadata | |
152 | ) |
|
159 | ) | |
153 |
|
160 | |||
154 |
|
161 | |||
155 | def publish_pretty(data, metadata=None): |
|
162 | def publish_pretty(data, metadata=None): | |
156 | """Publish raw text data to all frontends. |
|
163 | """Publish raw text data to all frontends. | |
157 |
|
164 | |||
158 | Parameters |
|
165 | Parameters | |
159 | ---------- |
|
166 | ---------- | |
160 | data : unicode |
|
167 | data : unicode | |
161 | The raw text data to publish. |
|
168 | The raw text data to publish. | |
162 | metadata : dict |
|
169 | metadata : dict | |
163 | A dictionary for metadata related to the data. This can contain |
|
170 | A dictionary for metadata related to the data. This can contain | |
164 | arbitrary key, value pairs that frontends can use to interpret |
|
171 | arbitrary key, value pairs that frontends can use to interpret | |
165 | the data. |
|
172 | the data. | |
166 | """ |
|
173 | """ | |
167 | publish_display_data( |
|
174 | publish_display_data( | |
168 | u'IPython.core.displaypub.publish_pretty', |
|
175 | u'IPython.core.displaypub.publish_pretty', | |
169 | {'text/plain':data}, |
|
176 | {'text/plain':data}, | |
170 | metadata=metadata |
|
177 | metadata=metadata | |
171 | ) |
|
178 | ) | |
172 |
|
179 | |||
173 |
|
180 | |||
174 | def publish_html(data, metadata=None): |
|
181 | def publish_html(data, metadata=None): | |
175 | """Publish raw HTML data to all frontends. |
|
182 | """Publish raw HTML data to all frontends. | |
176 |
|
183 | |||
177 | Parameters |
|
184 | Parameters | |
178 | ---------- |
|
185 | ---------- | |
179 | data : unicode |
|
186 | data : unicode | |
180 | The raw HTML data to publish. |
|
187 | The raw HTML data to publish. | |
181 | metadata : dict |
|
188 | metadata : dict | |
182 | A dictionary for metadata related to the data. This can contain |
|
189 | A dictionary for metadata related to the data. This can contain | |
183 | arbitrary key, value pairs that frontends can use to interpret |
|
190 | arbitrary key, value pairs that frontends can use to interpret | |
184 | the data. |
|
191 | the data. | |
185 | """ |
|
192 | """ | |
186 | publish_display_data( |
|
193 | publish_display_data( | |
187 | u'IPython.core.displaypub.publish_html', |
|
194 | u'IPython.core.displaypub.publish_html', | |
188 | {'text/html':data}, |
|
195 | {'text/html':data}, | |
189 | metadata=metadata |
|
196 | metadata=metadata | |
190 | ) |
|
197 | ) | |
191 |
|
198 | |||
192 |
|
199 | |||
193 | def publish_latex(data, metadata=None): |
|
200 | def publish_latex(data, metadata=None): | |
194 | """Publish raw LaTeX data to all frontends. |
|
201 | """Publish raw LaTeX data to all frontends. | |
195 |
|
202 | |||
196 | Parameters |
|
203 | Parameters | |
197 | ---------- |
|
204 | ---------- | |
198 | data : unicode |
|
205 | data : unicode | |
199 | The raw LaTeX data to publish. |
|
206 | The raw LaTeX data to publish. | |
200 | metadata : dict |
|
207 | metadata : dict | |
201 | A dictionary for metadata related to the data. This can contain |
|
208 | A dictionary for metadata related to the data. This can contain | |
202 | arbitrary key, value pairs that frontends can use to interpret |
|
209 | arbitrary key, value pairs that frontends can use to interpret | |
203 | the data. |
|
210 | the data. | |
204 | """ |
|
211 | """ | |
205 | publish_display_data( |
|
212 | publish_display_data( | |
206 | u'IPython.core.displaypub.publish_latex', |
|
213 | u'IPython.core.displaypub.publish_latex', | |
207 | {'text/latex':data}, |
|
214 | {'text/latex':data}, | |
208 | metadata=metadata |
|
215 | metadata=metadata | |
209 | ) |
|
216 | ) | |
210 |
|
217 | |||
211 | def publish_png(data, metadata=None): |
|
218 | def publish_png(data, metadata=None): | |
212 | """Publish raw binary PNG data to all frontends. |
|
219 | """Publish raw binary PNG data to all frontends. | |
213 |
|
220 | |||
214 | Parameters |
|
221 | Parameters | |
215 | ---------- |
|
222 | ---------- | |
216 | data : str/bytes |
|
223 | data : str/bytes | |
217 | The raw binary PNG data to publish. |
|
224 | The raw binary PNG data to publish. | |
218 | metadata : dict |
|
225 | metadata : dict | |
219 | A dictionary for metadata related to the data. This can contain |
|
226 | A dictionary for metadata related to the data. This can contain | |
220 | arbitrary key, value pairs that frontends can use to interpret |
|
227 | arbitrary key, value pairs that frontends can use to interpret | |
221 | the data. |
|
228 | the data. | |
222 | """ |
|
229 | """ | |
223 | publish_display_data( |
|
230 | publish_display_data( | |
224 | u'IPython.core.displaypub.publish_png', |
|
231 | u'IPython.core.displaypub.publish_png', | |
225 | {'image/png':data}, |
|
232 | {'image/png':data}, | |
226 | metadata=metadata |
|
233 | metadata=metadata | |
227 | ) |
|
234 | ) | |
228 |
|
235 | |||
229 |
|
236 | |||
230 | def publish_jpeg(data, metadata=None): |
|
237 | def publish_jpeg(data, metadata=None): | |
231 | """Publish raw binary JPEG data to all frontends. |
|
238 | """Publish raw binary JPEG data to all frontends. | |
232 |
|
239 | |||
233 | Parameters |
|
240 | Parameters | |
234 | ---------- |
|
241 | ---------- | |
235 | data : str/bytes |
|
242 | data : str/bytes | |
236 | The raw binary JPEG data to publish. |
|
243 | The raw binary JPEG data to publish. | |
237 | metadata : dict |
|
244 | metadata : dict | |
238 | A dictionary for metadata related to the data. This can contain |
|
245 | A dictionary for metadata related to the data. This can contain | |
239 | arbitrary key, value pairs that frontends can use to interpret |
|
246 | arbitrary key, value pairs that frontends can use to interpret | |
240 | the data. |
|
247 | the data. | |
241 | """ |
|
248 | """ | |
242 | publish_display_data( |
|
249 | publish_display_data( | |
243 | u'IPython.core.displaypub.publish_jpeg', |
|
250 | u'IPython.core.displaypub.publish_jpeg', | |
244 | {'image/jpeg':data}, |
|
251 | {'image/jpeg':data}, | |
245 | metadata=metadata |
|
252 | metadata=metadata | |
246 | ) |
|
253 | ) | |
247 |
|
254 | |||
248 |
|
255 | |||
249 | def publish_svg(data, metadata=None): |
|
256 | def publish_svg(data, metadata=None): | |
250 | """Publish raw SVG data to all frontends. |
|
257 | """Publish raw SVG data to all frontends. | |
251 |
|
258 | |||
252 | Parameters |
|
259 | Parameters | |
253 | ---------- |
|
260 | ---------- | |
254 | data : unicode |
|
261 | data : unicode | |
255 | The raw SVG data to publish. |
|
262 | The raw SVG data to publish. | |
256 | metadata : dict |
|
263 | metadata : dict | |
257 | A dictionary for metadata related to the data. This can contain |
|
264 | A dictionary for metadata related to the data. This can contain | |
258 | arbitrary key, value pairs that frontends can use to interpret |
|
265 | arbitrary key, value pairs that frontends can use to interpret | |
259 | the data. |
|
266 | the data. | |
260 | """ |
|
267 | """ | |
261 | publish_display_data( |
|
268 | publish_display_data( | |
262 | u'IPython.core.displaypub.publish_svg', |
|
269 | u'IPython.core.displaypub.publish_svg', | |
263 | {'image/svg+xml':data}, |
|
270 | {'image/svg+xml':data}, | |
264 | metadata=metadata |
|
271 | metadata=metadata | |
265 | ) |
|
272 | ) | |
266 |
|
273 | |||
267 | def publish_json(data, metadata=None): |
|
274 | def publish_json(data, metadata=None): | |
268 | """Publish raw JSON data to all frontends. |
|
275 | """Publish raw JSON data to all frontends. | |
269 |
|
276 | |||
270 | Parameters |
|
277 | Parameters | |
271 | ---------- |
|
278 | ---------- | |
272 | data : unicode |
|
279 | data : unicode | |
273 | The raw JSON data to publish. |
|
280 | The raw JSON data to publish. | |
274 | metadata : dict |
|
281 | metadata : dict | |
275 | A dictionary for metadata related to the data. This can contain |
|
282 | A dictionary for metadata related to the data. This can contain | |
276 | arbitrary key, value pairs that frontends can use to interpret |
|
283 | arbitrary key, value pairs that frontends can use to interpret | |
277 | the data. |
|
284 | the data. | |
278 | """ |
|
285 | """ | |
279 | publish_display_data( |
|
286 | publish_display_data( | |
280 | u'IPython.core.displaypub.publish_json', |
|
287 | u'IPython.core.displaypub.publish_json', | |
281 | {'application/json':data}, |
|
288 | {'application/json':data}, | |
282 | metadata=metadata |
|
289 | metadata=metadata | |
283 | ) |
|
290 | ) | |
284 |
|
291 | |||
285 | def publish_javascript(data, metadata=None): |
|
292 | def publish_javascript(data, metadata=None): | |
286 | """Publish raw Javascript data to all frontends. |
|
293 | """Publish raw Javascript data to all frontends. | |
287 |
|
294 | |||
288 | Parameters |
|
295 | Parameters | |
289 | ---------- |
|
296 | ---------- | |
290 | data : unicode |
|
297 | data : unicode | |
291 | The raw Javascript data to publish. |
|
298 | The raw Javascript data to publish. | |
292 | metadata : dict |
|
299 | metadata : dict | |
293 | A dictionary for metadata related to the data. This can contain |
|
300 | A dictionary for metadata related to the data. This can contain | |
294 | arbitrary key, value pairs that frontends can use to interpret |
|
301 | arbitrary key, value pairs that frontends can use to interpret | |
295 | the data. |
|
302 | the data. | |
296 | """ |
|
303 | """ | |
297 | publish_display_data( |
|
304 | publish_display_data( | |
298 | u'IPython.core.displaypub.publish_javascript', |
|
305 | u'IPython.core.displaypub.publish_javascript', | |
299 | {'application/javascript':data}, |
|
306 | {'application/javascript':data}, | |
300 | metadata=metadata |
|
307 | metadata=metadata | |
301 | ) |
|
308 | ) | |
302 |
|
309 |
@@ -1,516 +1,523 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, pylabtools |
|
28 | from IPython.core import page, pylabtools | |
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 ( |
|
34 | from IPython.lib.kernel import ( | |
35 | get_connection_file, get_connection_info, connect_qtconsole |
|
35 | get_connection_file, get_connection_info, connect_qtconsole | |
36 | ) |
|
36 | ) | |
37 | from IPython.utils import io |
|
37 | from IPython.utils import io | |
38 | from IPython.utils.jsonutil import json_clean |
|
38 | from IPython.utils.jsonutil import json_clean | |
39 | 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 |
|
40 | from IPython.utils.process import arg_split | |
41 | from IPython.utils.traitlets import Instance, Type, Dict, CBool |
|
41 | from IPython.utils.traitlets import Instance, Type, Dict, CBool | |
42 | from IPython.utils.warn import warn, error |
|
42 | from IPython.utils.warn import warn, error | |
43 | from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_binary |
|
43 | from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_binary | |
44 | from IPython.zmq.session import extract_header |
|
44 | from IPython.zmq.session import extract_header | |
45 | from session import Session |
|
45 | from session import Session | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | #----------------------------------------------------------------------------- |
|
48 | #----------------------------------------------------------------------------- | |
49 | # Functions and classes |
|
49 | # Functions and classes | |
50 | #----------------------------------------------------------------------------- |
|
50 | #----------------------------------------------------------------------------- | |
51 |
|
51 | |||
52 | class ZMQDisplayPublisher(DisplayPublisher): |
|
52 | class ZMQDisplayPublisher(DisplayPublisher): | |
53 | """A display publisher that publishes data using a ZeroMQ PUB socket.""" |
|
53 | """A display publisher that publishes data using a ZeroMQ PUB socket.""" | |
54 |
|
54 | |||
55 | session = Instance(Session) |
|
55 | session = Instance(Session) | |
56 | pub_socket = Instance('zmq.Socket') |
|
56 | pub_socket = Instance('zmq.Socket') | |
57 | parent_header = Dict({}) |
|
57 | parent_header = Dict({}) | |
58 |
|
58 | |||
59 | def set_parent(self, parent): |
|
59 | def set_parent(self, parent): | |
60 | """Set the parent for outbound messages.""" |
|
60 | """Set the parent for outbound messages.""" | |
61 | self.parent_header = extract_header(parent) |
|
61 | self.parent_header = extract_header(parent) | |
62 |
|
62 | |||
63 | def _flush_streams(self): |
|
63 | def _flush_streams(self): | |
64 | """flush IO Streams prior to display""" |
|
64 | """flush IO Streams prior to display""" | |
65 | sys.stdout.flush() |
|
65 | sys.stdout.flush() | |
66 | sys.stderr.flush() |
|
66 | sys.stderr.flush() | |
67 |
|
67 | |||
68 | def publish(self, source, data, metadata=None): |
|
68 | def publish(self, source, data, metadata=None): | |
69 | self._flush_streams() |
|
69 | self._flush_streams() | |
70 | if metadata is None: |
|
70 | if metadata is None: | |
71 | metadata = {} |
|
71 | metadata = {} | |
72 | self._validate_data(source, data, metadata) |
|
72 | self._validate_data(source, data, metadata) | |
73 | content = {} |
|
73 | content = {} | |
74 | content['source'] = source |
|
74 | content['source'] = source | |
75 | _encode_binary(data) |
|
75 | _encode_binary(data) | |
76 | content['data'] = data |
|
76 | content['data'] = data | |
77 | content['metadata'] = metadata |
|
77 | content['metadata'] = metadata | |
78 | self.session.send( |
|
78 | self.session.send( | |
79 | self.pub_socket, u'display_data', json_clean(content), |
|
79 | self.pub_socket, u'display_data', json_clean(content), | |
80 | parent=self.parent_header |
|
80 | parent=self.parent_header | |
81 | ) |
|
81 | ) | |
82 |
|
82 | |||
83 | def clear_output(self, stdout=True, stderr=True, other=True): |
|
83 | def clear_output(self, stdout=True, stderr=True, other=True): | |
84 | self._flush_streams() |
|
|||
85 | content = dict(stdout=stdout, stderr=stderr, other=other) |
|
84 | content = dict(stdout=stdout, stderr=stderr, other=other) | |
|
85 | ||||
|
86 | if stdout: | |||
|
87 | print('\r', file=sys.stdout, end='') | |||
|
88 | if stderr: | |||
|
89 | print('\r', file=sys.stderr, end='') | |||
|
90 | ||||
|
91 | self._flush_streams() | |||
|
92 | ||||
86 | self.session.send( |
|
93 | self.session.send( | |
87 | self.pub_socket, u'clear_output', content, |
|
94 | self.pub_socket, u'clear_output', content, | |
88 | parent=self.parent_header |
|
95 | parent=self.parent_header | |
89 | ) |
|
96 | ) | |
90 |
|
97 | |||
91 | class ZMQInteractiveShell(InteractiveShell): |
|
98 | class ZMQInteractiveShell(InteractiveShell): | |
92 | """A subclass of InteractiveShell for ZMQ.""" |
|
99 | """A subclass of InteractiveShell for ZMQ.""" | |
93 |
|
100 | |||
94 | displayhook_class = Type(ZMQShellDisplayHook) |
|
101 | displayhook_class = Type(ZMQShellDisplayHook) | |
95 | display_pub_class = Type(ZMQDisplayPublisher) |
|
102 | display_pub_class = Type(ZMQDisplayPublisher) | |
96 |
|
103 | |||
97 | # Override the traitlet in the parent class, because there's no point using |
|
104 | # Override the traitlet in the parent class, because there's no point using | |
98 | # readline for the kernel. Can be removed when the readline code is moved |
|
105 | # readline for the kernel. Can be removed when the readline code is moved | |
99 | # to the terminal frontend. |
|
106 | # to the terminal frontend. | |
100 | colors_force = CBool(True) |
|
107 | colors_force = CBool(True) | |
101 | readline_use = CBool(False) |
|
108 | readline_use = CBool(False) | |
102 | # autoindent has no meaning in a zmqshell, and attempting to enable it |
|
109 | # autoindent has no meaning in a zmqshell, and attempting to enable it | |
103 | # will print a warning in the absence of readline. |
|
110 | # will print a warning in the absence of readline. | |
104 | autoindent = CBool(False) |
|
111 | autoindent = CBool(False) | |
105 |
|
112 | |||
106 | exiter = Instance(ZMQExitAutocall) |
|
113 | exiter = Instance(ZMQExitAutocall) | |
107 | def _exiter_default(self): |
|
114 | def _exiter_default(self): | |
108 | return ZMQExitAutocall(self) |
|
115 | return ZMQExitAutocall(self) | |
109 |
|
116 | |||
110 | keepkernel_on_exit = None |
|
117 | keepkernel_on_exit = None | |
111 |
|
118 | |||
112 | # Over ZeroMQ, GUI control isn't done with PyOS_InputHook as there is no |
|
119 | # Over ZeroMQ, GUI control isn't done with PyOS_InputHook as there is no | |
113 | # interactive input being read; we provide event loop support in ipkernel |
|
120 | # interactive input being read; we provide event loop support in ipkernel | |
114 | from .eventloops import enable_gui |
|
121 | from .eventloops import enable_gui | |
115 | enable_gui = staticmethod(enable_gui) |
|
122 | enable_gui = staticmethod(enable_gui) | |
116 |
|
123 | |||
117 | def init_environment(self): |
|
124 | def init_environment(self): | |
118 | """Configure the user's environment. |
|
125 | """Configure the user's environment. | |
119 |
|
126 | |||
120 | """ |
|
127 | """ | |
121 | env = os.environ |
|
128 | env = os.environ | |
122 | # These two ensure 'ls' produces nice coloring on BSD-derived systems |
|
129 | # These two ensure 'ls' produces nice coloring on BSD-derived systems | |
123 | env['TERM'] = 'xterm-color' |
|
130 | env['TERM'] = 'xterm-color' | |
124 | env['CLICOLOR'] = '1' |
|
131 | env['CLICOLOR'] = '1' | |
125 | # Since normal pagers don't work at all (over pexpect we don't have |
|
132 | # Since normal pagers don't work at all (over pexpect we don't have | |
126 | # single-key control of the subprocess), try to disable paging in |
|
133 | # single-key control of the subprocess), try to disable paging in | |
127 | # subprocesses as much as possible. |
|
134 | # subprocesses as much as possible. | |
128 | env['PAGER'] = 'cat' |
|
135 | env['PAGER'] = 'cat' | |
129 | env['GIT_PAGER'] = 'cat' |
|
136 | env['GIT_PAGER'] = 'cat' | |
130 |
|
137 | |||
131 | # And install the payload version of page. |
|
138 | # And install the payload version of page. | |
132 | install_payload_page() |
|
139 | install_payload_page() | |
133 |
|
140 | |||
134 | def auto_rewrite_input(self, cmd): |
|
141 | def auto_rewrite_input(self, cmd): | |
135 | """Called to show the auto-rewritten input for autocall and friends. |
|
142 | """Called to show the auto-rewritten input for autocall and friends. | |
136 |
|
143 | |||
137 | FIXME: this payload is currently not correctly processed by the |
|
144 | FIXME: this payload is currently not correctly processed by the | |
138 | frontend. |
|
145 | frontend. | |
139 | """ |
|
146 | """ | |
140 | new = self.prompt_manager.render('rewrite') + cmd |
|
147 | new = self.prompt_manager.render('rewrite') + cmd | |
141 | payload = dict( |
|
148 | payload = dict( | |
142 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.auto_rewrite_input', |
|
149 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.auto_rewrite_input', | |
143 | transformed_input=new, |
|
150 | transformed_input=new, | |
144 | ) |
|
151 | ) | |
145 | self.payload_manager.write_payload(payload) |
|
152 | self.payload_manager.write_payload(payload) | |
146 |
|
153 | |||
147 | def ask_exit(self): |
|
154 | def ask_exit(self): | |
148 | """Engage the exit actions.""" |
|
155 | """Engage the exit actions.""" | |
149 | payload = dict( |
|
156 | payload = dict( | |
150 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit', |
|
157 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit', | |
151 | exit=True, |
|
158 | exit=True, | |
152 | keepkernel=self.keepkernel_on_exit, |
|
159 | keepkernel=self.keepkernel_on_exit, | |
153 | ) |
|
160 | ) | |
154 | self.payload_manager.write_payload(payload) |
|
161 | self.payload_manager.write_payload(payload) | |
155 |
|
162 | |||
156 | def _showtraceback(self, etype, evalue, stb): |
|
163 | def _showtraceback(self, etype, evalue, stb): | |
157 |
|
164 | |||
158 | exc_content = { |
|
165 | exc_content = { | |
159 | u'traceback' : stb, |
|
166 | u'traceback' : stb, | |
160 | u'ename' : unicode(etype.__name__), |
|
167 | u'ename' : unicode(etype.__name__), | |
161 | u'evalue' : unicode(evalue) |
|
168 | u'evalue' : unicode(evalue) | |
162 | } |
|
169 | } | |
163 |
|
170 | |||
164 | dh = self.displayhook |
|
171 | dh = self.displayhook | |
165 | # Send exception info over pub socket for other clients than the caller |
|
172 | # Send exception info over pub socket for other clients than the caller | |
166 | # to pick up |
|
173 | # to pick up | |
167 | exc_msg = dh.session.send(dh.pub_socket, u'pyerr', json_clean(exc_content), dh.parent_header) |
|
174 | exc_msg = dh.session.send(dh.pub_socket, u'pyerr', json_clean(exc_content), dh.parent_header) | |
168 |
|
175 | |||
169 | # FIXME - Hack: store exception info in shell object. Right now, the |
|
176 | # FIXME - Hack: store exception info in shell object. Right now, the | |
170 | # caller is reading this info after the fact, we need to fix this logic |
|
177 | # caller is reading this info after the fact, we need to fix this logic | |
171 | # to remove this hack. Even uglier, we need to store the error status |
|
178 | # to remove this hack. Even uglier, we need to store the error status | |
172 | # here, because in the main loop, the logic that sets it is being |
|
179 | # here, because in the main loop, the logic that sets it is being | |
173 | # skipped because runlines swallows the exceptions. |
|
180 | # skipped because runlines swallows the exceptions. | |
174 | exc_content[u'status'] = u'error' |
|
181 | exc_content[u'status'] = u'error' | |
175 | self._reply_content = exc_content |
|
182 | self._reply_content = exc_content | |
176 | # /FIXME |
|
183 | # /FIXME | |
177 |
|
184 | |||
178 | return exc_content |
|
185 | return exc_content | |
179 |
|
186 | |||
180 | #------------------------------------------------------------------------ |
|
187 | #------------------------------------------------------------------------ | |
181 | # Magic overrides |
|
188 | # Magic overrides | |
182 | #------------------------------------------------------------------------ |
|
189 | #------------------------------------------------------------------------ | |
183 | # Once the base class stops inheriting from magic, this code needs to be |
|
190 | # Once the base class stops inheriting from magic, this code needs to be | |
184 | # moved into a separate machinery as well. For now, at least isolate here |
|
191 | # moved into a separate machinery as well. For now, at least isolate here | |
185 | # the magics which this class needs to implement differently from the base |
|
192 | # the magics which this class needs to implement differently from the base | |
186 | # class, or that are unique to it. |
|
193 | # class, or that are unique to it. | |
187 |
|
194 | |||
188 | def magic_doctest_mode(self,parameter_s=''): |
|
195 | def magic_doctest_mode(self,parameter_s=''): | |
189 | """Toggle doctest mode on and off. |
|
196 | """Toggle doctest mode on and off. | |
190 |
|
197 | |||
191 | This mode is intended to make IPython behave as much as possible like a |
|
198 | This mode is intended to make IPython behave as much as possible like a | |
192 | plain Python shell, from the perspective of how its prompts, exceptions |
|
199 | plain Python shell, from the perspective of how its prompts, exceptions | |
193 | and output look. This makes it easy to copy and paste parts of a |
|
200 | and output look. This makes it easy to copy and paste parts of a | |
194 | session into doctests. It does so by: |
|
201 | session into doctests. It does so by: | |
195 |
|
202 | |||
196 | - Changing the prompts to the classic ``>>>`` ones. |
|
203 | - Changing the prompts to the classic ``>>>`` ones. | |
197 | - Changing the exception reporting mode to 'Plain'. |
|
204 | - Changing the exception reporting mode to 'Plain'. | |
198 | - Disabling pretty-printing of output. |
|
205 | - Disabling pretty-printing of output. | |
199 |
|
206 | |||
200 | Note that IPython also supports the pasting of code snippets that have |
|
207 | Note that IPython also supports the pasting of code snippets that have | |
201 | leading '>>>' and '...' prompts in them. This means that you can paste |
|
208 | leading '>>>' and '...' prompts in them. This means that you can paste | |
202 | doctests from files or docstrings (even if they have leading |
|
209 | doctests from files or docstrings (even if they have leading | |
203 | whitespace), and the code will execute correctly. You can then use |
|
210 | whitespace), and the code will execute correctly. You can then use | |
204 | '%history -t' to see the translated history; this will give you the |
|
211 | '%history -t' to see the translated history; this will give you the | |
205 | input after removal of all the leading prompts and whitespace, which |
|
212 | input after removal of all the leading prompts and whitespace, which | |
206 | can be pasted back into an editor. |
|
213 | can be pasted back into an editor. | |
207 |
|
214 | |||
208 | With these features, you can switch into this mode easily whenever you |
|
215 | With these features, you can switch into this mode easily whenever you | |
209 | need to do testing and changes to doctests, without having to leave |
|
216 | need to do testing and changes to doctests, without having to leave | |
210 | your existing IPython session. |
|
217 | your existing IPython session. | |
211 | """ |
|
218 | """ | |
212 |
|
219 | |||
213 | from IPython.utils.ipstruct import Struct |
|
220 | from IPython.utils.ipstruct import Struct | |
214 |
|
221 | |||
215 | # Shorthands |
|
222 | # Shorthands | |
216 | shell = self.shell |
|
223 | shell = self.shell | |
217 | disp_formatter = self.shell.display_formatter |
|
224 | disp_formatter = self.shell.display_formatter | |
218 | ptformatter = disp_formatter.formatters['text/plain'] |
|
225 | ptformatter = disp_formatter.formatters['text/plain'] | |
219 | # dstore is a data store kept in the instance metadata bag to track any |
|
226 | # dstore is a data store kept in the instance metadata bag to track any | |
220 | # changes we make, so we can undo them later. |
|
227 | # changes we make, so we can undo them later. | |
221 | dstore = shell.meta.setdefault('doctest_mode', Struct()) |
|
228 | dstore = shell.meta.setdefault('doctest_mode', Struct()) | |
222 | save_dstore = dstore.setdefault |
|
229 | save_dstore = dstore.setdefault | |
223 |
|
230 | |||
224 | # save a few values we'll need to recover later |
|
231 | # save a few values we'll need to recover later | |
225 | mode = save_dstore('mode', False) |
|
232 | mode = save_dstore('mode', False) | |
226 | save_dstore('rc_pprint', ptformatter.pprint) |
|
233 | save_dstore('rc_pprint', ptformatter.pprint) | |
227 | save_dstore('rc_plain_text_only',disp_formatter.plain_text_only) |
|
234 | save_dstore('rc_plain_text_only',disp_formatter.plain_text_only) | |
228 | save_dstore('xmode', shell.InteractiveTB.mode) |
|
235 | save_dstore('xmode', shell.InteractiveTB.mode) | |
229 |
|
236 | |||
230 | if mode == False: |
|
237 | if mode == False: | |
231 | # turn on |
|
238 | # turn on | |
232 | ptformatter.pprint = False |
|
239 | ptformatter.pprint = False | |
233 | disp_formatter.plain_text_only = True |
|
240 | disp_formatter.plain_text_only = True | |
234 | shell.magic_xmode('Plain') |
|
241 | shell.magic_xmode('Plain') | |
235 | else: |
|
242 | else: | |
236 | # turn off |
|
243 | # turn off | |
237 | ptformatter.pprint = dstore.rc_pprint |
|
244 | ptformatter.pprint = dstore.rc_pprint | |
238 | disp_formatter.plain_text_only = dstore.rc_plain_text_only |
|
245 | disp_formatter.plain_text_only = dstore.rc_plain_text_only | |
239 | shell.magic_xmode(dstore.xmode) |
|
246 | shell.magic_xmode(dstore.xmode) | |
240 |
|
247 | |||
241 | # Store new mode and inform on console |
|
248 | # Store new mode and inform on console | |
242 | dstore.mode = bool(1-int(mode)) |
|
249 | dstore.mode = bool(1-int(mode)) | |
243 | mode_label = ['OFF','ON'][dstore.mode] |
|
250 | mode_label = ['OFF','ON'][dstore.mode] | |
244 | print('Doctest mode is:', mode_label) |
|
251 | print('Doctest mode is:', mode_label) | |
245 |
|
252 | |||
246 | # Send the payload back so that clients can modify their prompt display |
|
253 | # Send the payload back so that clients can modify their prompt display | |
247 | payload = dict( |
|
254 | payload = dict( | |
248 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.magic_doctest_mode', |
|
255 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.magic_doctest_mode', | |
249 | mode=dstore.mode) |
|
256 | mode=dstore.mode) | |
250 | self.payload_manager.write_payload(payload) |
|
257 | self.payload_manager.write_payload(payload) | |
251 |
|
258 | |||
252 | def magic_edit(self,parameter_s='',last_call=['','']): |
|
259 | def magic_edit(self,parameter_s='',last_call=['','']): | |
253 | """Bring up an editor and execute the resulting code. |
|
260 | """Bring up an editor and execute the resulting code. | |
254 |
|
261 | |||
255 | Usage: |
|
262 | Usage: | |
256 | %edit [options] [args] |
|
263 | %edit [options] [args] | |
257 |
|
264 | |||
258 | %edit runs an external text editor. You will need to set the command for |
|
265 | %edit runs an external text editor. You will need to set the command for | |
259 | this editor via the ``TerminalInteractiveShell.editor`` option in your |
|
266 | this editor via the ``TerminalInteractiveShell.editor`` option in your | |
260 | configuration file before it will work. |
|
267 | configuration file before it will work. | |
261 |
|
268 | |||
262 | This command allows you to conveniently edit multi-line code right in |
|
269 | This command allows you to conveniently edit multi-line code right in | |
263 | your IPython session. |
|
270 | your IPython session. | |
264 |
|
271 | |||
265 | If called without arguments, %edit opens up an empty editor with a |
|
272 | If called without arguments, %edit opens up an empty editor with a | |
266 | temporary file and will execute the contents of this file when you |
|
273 | temporary file and will execute the contents of this file when you | |
267 | close it (don't forget to save it!). |
|
274 | close it (don't forget to save it!). | |
268 |
|
275 | |||
269 |
|
276 | |||
270 | Options: |
|
277 | Options: | |
271 |
|
278 | |||
272 | -n <number>: open the editor at a specified line number. By default, |
|
279 | -n <number>: open the editor at a specified line number. By default, | |
273 | the IPython editor hook uses the unix syntax 'editor +N filename', but |
|
280 | the IPython editor hook uses the unix syntax 'editor +N filename', but | |
274 | you can configure this by providing your own modified hook if your |
|
281 | you can configure this by providing your own modified hook if your | |
275 | favorite editor supports line-number specifications with a different |
|
282 | favorite editor supports line-number specifications with a different | |
276 | syntax. |
|
283 | syntax. | |
277 |
|
284 | |||
278 | -p: this will call the editor with the same data as the previous time |
|
285 | -p: this will call the editor with the same data as the previous time | |
279 | it was used, regardless of how long ago (in your current session) it |
|
286 | it was used, regardless of how long ago (in your current session) it | |
280 | was. |
|
287 | was. | |
281 |
|
288 | |||
282 | -r: use 'raw' input. This option only applies to input taken from the |
|
289 | -r: use 'raw' input. This option only applies to input taken from the | |
283 | user's history. By default, the 'processed' history is used, so that |
|
290 | user's history. By default, the 'processed' history is used, so that | |
284 | magics are loaded in their transformed version to valid Python. If |
|
291 | magics are loaded in their transformed version to valid Python. If | |
285 | this option is given, the raw input as typed as the command line is |
|
292 | this option is given, the raw input as typed as the command line is | |
286 | used instead. When you exit the editor, it will be executed by |
|
293 | used instead. When you exit the editor, it will be executed by | |
287 | IPython's own processor. |
|
294 | IPython's own processor. | |
288 |
|
295 | |||
289 | -x: do not execute the edited code immediately upon exit. This is |
|
296 | -x: do not execute the edited code immediately upon exit. This is | |
290 | mainly useful if you are editing programs which need to be called with |
|
297 | mainly useful if you are editing programs which need to be called with | |
291 | command line arguments, which you can then do using %run. |
|
298 | command line arguments, which you can then do using %run. | |
292 |
|
299 | |||
293 |
|
300 | |||
294 | Arguments: |
|
301 | Arguments: | |
295 |
|
302 | |||
296 | If arguments are given, the following possibilites exist: |
|
303 | If arguments are given, the following possibilites exist: | |
297 |
|
304 | |||
298 | - The arguments are numbers or pairs of colon-separated numbers (like |
|
305 | - The arguments are numbers or pairs of colon-separated numbers (like | |
299 | 1 4:8 9). These are interpreted as lines of previous input to be |
|
306 | 1 4:8 9). These are interpreted as lines of previous input to be | |
300 | loaded into the editor. The syntax is the same of the %macro command. |
|
307 | loaded into the editor. The syntax is the same of the %macro command. | |
301 |
|
308 | |||
302 | - If the argument doesn't start with a number, it is evaluated as a |
|
309 | - If the argument doesn't start with a number, it is evaluated as a | |
303 | variable and its contents loaded into the editor. You can thus edit |
|
310 | variable and its contents loaded into the editor. You can thus edit | |
304 | any string which contains python code (including the result of |
|
311 | any string which contains python code (including the result of | |
305 | previous edits). |
|
312 | previous edits). | |
306 |
|
313 | |||
307 | - If the argument is the name of an object (other than a string), |
|
314 | - If the argument is the name of an object (other than a string), | |
308 | IPython will try to locate the file where it was defined and open the |
|
315 | IPython will try to locate the file where it was defined and open the | |
309 | editor at the point where it is defined. You can use `%edit function` |
|
316 | editor at the point where it is defined. You can use `%edit function` | |
310 | to load an editor exactly at the point where 'function' is defined, |
|
317 | to load an editor exactly at the point where 'function' is defined, | |
311 | edit it and have the file be executed automatically. |
|
318 | edit it and have the file be executed automatically. | |
312 |
|
319 | |||
313 | If the object is a macro (see %macro for details), this opens up your |
|
320 | If the object is a macro (see %macro for details), this opens up your | |
314 | specified editor with a temporary file containing the macro's data. |
|
321 | specified editor with a temporary file containing the macro's data. | |
315 | Upon exit, the macro is reloaded with the contents of the file. |
|
322 | Upon exit, the macro is reloaded with the contents of the file. | |
316 |
|
323 | |||
317 | Note: opening at an exact line is only supported under Unix, and some |
|
324 | Note: opening at an exact line is only supported under Unix, and some | |
318 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
325 | editors (like kedit and gedit up to Gnome 2.8) do not understand the | |
319 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
326 | '+NUMBER' parameter necessary for this feature. Good editors like | |
320 | (X)Emacs, vi, jed, pico and joe all do. |
|
327 | (X)Emacs, vi, jed, pico and joe all do. | |
321 |
|
328 | |||
322 | - If the argument is not found as a variable, IPython will look for a |
|
329 | - If the argument is not found as a variable, IPython will look for a | |
323 | file with that name (adding .py if necessary) and load it into the |
|
330 | file with that name (adding .py if necessary) and load it into the | |
324 | editor. It will execute its contents with execfile() when you exit, |
|
331 | editor. It will execute its contents with execfile() when you exit, | |
325 | loading any code in the file into your interactive namespace. |
|
332 | loading any code in the file into your interactive namespace. | |
326 |
|
333 | |||
327 | After executing your code, %edit will return as output the code you |
|
334 | After executing your code, %edit will return as output the code you | |
328 | typed in the editor (except when it was an existing file). This way |
|
335 | typed in the editor (except when it was an existing file). This way | |
329 | you can reload the code in further invocations of %edit as a variable, |
|
336 | you can reload the code in further invocations of %edit as a variable, | |
330 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of |
|
337 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of | |
331 | the output. |
|
338 | the output. | |
332 |
|
339 | |||
333 | Note that %edit is also available through the alias %ed. |
|
340 | Note that %edit is also available through the alias %ed. | |
334 |
|
341 | |||
335 | This is an example of creating a simple function inside the editor and |
|
342 | This is an example of creating a simple function inside the editor and | |
336 | then modifying it. First, start up the editor: |
|
343 | then modifying it. First, start up the editor: | |
337 |
|
344 | |||
338 | In [1]: ed |
|
345 | In [1]: ed | |
339 | Editing... done. Executing edited code... |
|
346 | Editing... done. Executing edited code... | |
340 | Out[1]: 'def foo():n print "foo() was defined in an editing session"n' |
|
347 | Out[1]: 'def foo():n print "foo() was defined in an editing session"n' | |
341 |
|
348 | |||
342 | We can then call the function foo(): |
|
349 | We can then call the function foo(): | |
343 |
|
350 | |||
344 | In [2]: foo() |
|
351 | In [2]: foo() | |
345 | foo() was defined in an editing session |
|
352 | foo() was defined in an editing session | |
346 |
|
353 | |||
347 | Now we edit foo. IPython automatically loads the editor with the |
|
354 | Now we edit foo. IPython automatically loads the editor with the | |
348 | (temporary) file where foo() was previously defined: |
|
355 | (temporary) file where foo() was previously defined: | |
349 |
|
356 | |||
350 | In [3]: ed foo |
|
357 | In [3]: ed foo | |
351 | Editing... done. Executing edited code... |
|
358 | Editing... done. Executing edited code... | |
352 |
|
359 | |||
353 | And if we call foo() again we get the modified version: |
|
360 | And if we call foo() again we get the modified version: | |
354 |
|
361 | |||
355 | In [4]: foo() |
|
362 | In [4]: foo() | |
356 | foo() has now been changed! |
|
363 | foo() has now been changed! | |
357 |
|
364 | |||
358 | Here is an example of how to edit a code snippet successive |
|
365 | Here is an example of how to edit a code snippet successive | |
359 | times. First we call the editor: |
|
366 | times. First we call the editor: | |
360 |
|
367 | |||
361 | In [5]: ed |
|
368 | In [5]: ed | |
362 | Editing... done. Executing edited code... |
|
369 | Editing... done. Executing edited code... | |
363 | hello |
|
370 | hello | |
364 | Out[5]: "print 'hello'n" |
|
371 | Out[5]: "print 'hello'n" | |
365 |
|
372 | |||
366 | Now we call it again with the previous output (stored in _): |
|
373 | Now we call it again with the previous output (stored in _): | |
367 |
|
374 | |||
368 | In [6]: ed _ |
|
375 | In [6]: ed _ | |
369 | Editing... done. Executing edited code... |
|
376 | Editing... done. Executing edited code... | |
370 | hello world |
|
377 | hello world | |
371 | Out[6]: "print 'hello world'n" |
|
378 | Out[6]: "print 'hello world'n" | |
372 |
|
379 | |||
373 | Now we call it with the output #8 (stored in _8, also as Out[8]): |
|
380 | Now we call it with the output #8 (stored in _8, also as Out[8]): | |
374 |
|
381 | |||
375 | In [7]: ed _8 |
|
382 | In [7]: ed _8 | |
376 | Editing... done. Executing edited code... |
|
383 | Editing... done. Executing edited code... | |
377 | hello again |
|
384 | hello again | |
378 | Out[7]: "print 'hello again'n" |
|
385 | Out[7]: "print 'hello again'n" | |
379 | """ |
|
386 | """ | |
380 |
|
387 | |||
381 | opts,args = self.parse_options(parameter_s,'prn:') |
|
388 | opts,args = self.parse_options(parameter_s,'prn:') | |
382 |
|
389 | |||
383 | try: |
|
390 | try: | |
384 | filename, lineno, _ = self._find_edit_target(args, opts, last_call) |
|
391 | filename, lineno, _ = self._find_edit_target(args, opts, last_call) | |
385 | except MacroToEdit as e: |
|
392 | except MacroToEdit as e: | |
386 | # TODO: Implement macro editing over 2 processes. |
|
393 | # TODO: Implement macro editing over 2 processes. | |
387 | print("Macro editing not yet implemented in 2-process model.") |
|
394 | print("Macro editing not yet implemented in 2-process model.") | |
388 | return |
|
395 | return | |
389 |
|
396 | |||
390 | # Make sure we send to the client an absolute path, in case the working |
|
397 | # Make sure we send to the client an absolute path, in case the working | |
391 | # directory of client and kernel don't match |
|
398 | # directory of client and kernel don't match | |
392 | filename = os.path.abspath(filename) |
|
399 | filename = os.path.abspath(filename) | |
393 |
|
400 | |||
394 | payload = { |
|
401 | payload = { | |
395 | 'source' : 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic', |
|
402 | 'source' : 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic', | |
396 | 'filename' : filename, |
|
403 | 'filename' : filename, | |
397 | 'line_number' : lineno |
|
404 | 'line_number' : lineno | |
398 | } |
|
405 | } | |
399 | self.payload_manager.write_payload(payload) |
|
406 | self.payload_manager.write_payload(payload) | |
400 |
|
407 | |||
401 | # A few magics that are adapted to the specifics of using pexpect and a |
|
408 | # A few magics that are adapted to the specifics of using pexpect and a | |
402 | # remote terminal |
|
409 | # remote terminal | |
403 |
|
410 | |||
404 | def magic_clear(self, arg_s): |
|
411 | def magic_clear(self, arg_s): | |
405 | """Clear the terminal.""" |
|
412 | """Clear the terminal.""" | |
406 | if os.name == 'posix': |
|
413 | if os.name == 'posix': | |
407 | self.shell.system("clear") |
|
414 | self.shell.system("clear") | |
408 | else: |
|
415 | else: | |
409 | self.shell.system("cls") |
|
416 | self.shell.system("cls") | |
410 |
|
417 | |||
411 | if os.name == 'nt': |
|
418 | if os.name == 'nt': | |
412 | # This is the usual name in windows |
|
419 | # This is the usual name in windows | |
413 | magic_cls = magic_clear |
|
420 | magic_cls = magic_clear | |
414 |
|
421 | |||
415 | # Terminal pagers won't work over pexpect, but we do have our own pager |
|
422 | # Terminal pagers won't work over pexpect, but we do have our own pager | |
416 |
|
423 | |||
417 | def magic_less(self, arg_s): |
|
424 | def magic_less(self, arg_s): | |
418 | """Show a file through the pager. |
|
425 | """Show a file through the pager. | |
419 |
|
426 | |||
420 | Files ending in .py are syntax-highlighted.""" |
|
427 | Files ending in .py are syntax-highlighted.""" | |
421 | cont = open(arg_s).read() |
|
428 | cont = open(arg_s).read() | |
422 | if arg_s.endswith('.py'): |
|
429 | if arg_s.endswith('.py'): | |
423 | cont = self.shell.pycolorize(cont) |
|
430 | cont = self.shell.pycolorize(cont) | |
424 | page.page(cont) |
|
431 | page.page(cont) | |
425 |
|
432 | |||
426 | magic_more = magic_less |
|
433 | magic_more = magic_less | |
427 |
|
434 | |||
428 | # Man calls a pager, so we also need to redefine it |
|
435 | # Man calls a pager, so we also need to redefine it | |
429 | if os.name == 'posix': |
|
436 | if os.name == 'posix': | |
430 | def magic_man(self, arg_s): |
|
437 | def magic_man(self, arg_s): | |
431 | """Find the man page for the given command and display in pager.""" |
|
438 | """Find the man page for the given command and display in pager.""" | |
432 | page.page(self.shell.getoutput('man %s | col -b' % arg_s, |
|
439 | page.page(self.shell.getoutput('man %s | col -b' % arg_s, | |
433 | split=False)) |
|
440 | split=False)) | |
434 |
|
441 | |||
435 | # FIXME: this is specific to the GUI, so we should let the gui app load |
|
442 | # FIXME: this is specific to the GUI, so we should let the gui app load | |
436 | # magics at startup that are only for the gui. Once the gui app has proper |
|
443 | # magics at startup that are only for the gui. Once the gui app has proper | |
437 | # profile and configuration management, we can have it initialize a kernel |
|
444 | # profile and configuration management, we can have it initialize a kernel | |
438 | # with a special config file that provides these. |
|
445 | # with a special config file that provides these. | |
439 | def magic_guiref(self, arg_s): |
|
446 | def magic_guiref(self, arg_s): | |
440 | """Show a basic reference about the GUI console.""" |
|
447 | """Show a basic reference about the GUI console.""" | |
441 | from IPython.core.usage import gui_reference |
|
448 | from IPython.core.usage import gui_reference | |
442 | page.page(gui_reference, auto_html=True) |
|
449 | page.page(gui_reference, auto_html=True) | |
443 |
|
450 | |||
444 | def magic_connect_info(self, arg_s): |
|
451 | def magic_connect_info(self, arg_s): | |
445 | """Print information for connecting other clients to this kernel |
|
452 | """Print information for connecting other clients to this kernel | |
446 |
|
453 | |||
447 | It will print the contents of this session's connection file, as well as |
|
454 | It will print the contents of this session's connection file, as well as | |
448 | shortcuts for local clients. |
|
455 | shortcuts for local clients. | |
449 |
|
456 | |||
450 | In the simplest case, when called from the most recently launched kernel, |
|
457 | In the simplest case, when called from the most recently launched kernel, | |
451 | secondary clients can be connected, simply with: |
|
458 | secondary clients can be connected, simply with: | |
452 |
|
459 | |||
453 | $> ipython <app> --existing |
|
460 | $> ipython <app> --existing | |
454 |
|
461 | |||
455 | """ |
|
462 | """ | |
456 |
|
463 | |||
457 | from IPython.core.application import BaseIPythonApplication as BaseIPApp |
|
464 | from IPython.core.application import BaseIPythonApplication as BaseIPApp | |
458 |
|
465 | |||
459 | if BaseIPApp.initialized(): |
|
466 | if BaseIPApp.initialized(): | |
460 | app = BaseIPApp.instance() |
|
467 | app = BaseIPApp.instance() | |
461 | security_dir = app.profile_dir.security_dir |
|
468 | security_dir = app.profile_dir.security_dir | |
462 | profile = app.profile |
|
469 | profile = app.profile | |
463 | else: |
|
470 | else: | |
464 | profile = 'default' |
|
471 | profile = 'default' | |
465 | security_dir = '' |
|
472 | security_dir = '' | |
466 |
|
473 | |||
467 | try: |
|
474 | try: | |
468 | connection_file = get_connection_file() |
|
475 | connection_file = get_connection_file() | |
469 | info = get_connection_info(unpack=False) |
|
476 | info = get_connection_info(unpack=False) | |
470 | except Exception as e: |
|
477 | except Exception as e: | |
471 | error("Could not get connection info: %r" % e) |
|
478 | error("Could not get connection info: %r" % e) | |
472 | return |
|
479 | return | |
473 |
|
480 | |||
474 | # add profile flag for non-default profile |
|
481 | # add profile flag for non-default profile | |
475 | profile_flag = "--profile %s" % profile if profile != 'default' else "" |
|
482 | profile_flag = "--profile %s" % profile if profile != 'default' else "" | |
476 |
|
483 | |||
477 | # if it's in the security dir, truncate to basename |
|
484 | # if it's in the security dir, truncate to basename | |
478 | if security_dir == os.path.dirname(connection_file): |
|
485 | if security_dir == os.path.dirname(connection_file): | |
479 | connection_file = os.path.basename(connection_file) |
|
486 | connection_file = os.path.basename(connection_file) | |
480 |
|
487 | |||
481 |
|
488 | |||
482 | print (info + '\n') |
|
489 | print (info + '\n') | |
483 | print ("Paste the above JSON into a file, and connect with:\n" |
|
490 | print ("Paste the above JSON into a file, and connect with:\n" | |
484 | " $> ipython <app> --existing <file>\n" |
|
491 | " $> ipython <app> --existing <file>\n" | |
485 | "or, if you are local, you can connect with just:\n" |
|
492 | "or, if you are local, you can connect with just:\n" | |
486 | " $> ipython <app> --existing {0} {1}\n" |
|
493 | " $> ipython <app> --existing {0} {1}\n" | |
487 | "or even just:\n" |
|
494 | "or even just:\n" | |
488 | " $> ipython <app> --existing {1}\n" |
|
495 | " $> ipython <app> --existing {1}\n" | |
489 | "if this is the most recent IPython session you have started.".format( |
|
496 | "if this is the most recent IPython session you have started.".format( | |
490 | connection_file, profile_flag |
|
497 | connection_file, profile_flag | |
491 | ) |
|
498 | ) | |
492 | ) |
|
499 | ) | |
493 |
|
500 | |||
494 | def magic_qtconsole(self, arg_s): |
|
501 | def magic_qtconsole(self, arg_s): | |
495 | """Open a qtconsole connected to this kernel. |
|
502 | """Open a qtconsole connected to this kernel. | |
496 |
|
503 | |||
497 | Useful for connecting a qtconsole to running notebooks, for better |
|
504 | Useful for connecting a qtconsole to running notebooks, for better | |
498 | debugging. |
|
505 | debugging. | |
499 | """ |
|
506 | """ | |
500 | try: |
|
507 | try: | |
501 | p = connect_qtconsole(argv=arg_split(arg_s, os.name=='posix')) |
|
508 | p = connect_qtconsole(argv=arg_split(arg_s, os.name=='posix')) | |
502 | except Exception as e: |
|
509 | except Exception as e: | |
503 | error("Could not start qtconsole: %r" % e) |
|
510 | error("Could not start qtconsole: %r" % e) | |
504 | return |
|
511 | return | |
505 |
|
512 | |||
506 | def set_next_input(self, text): |
|
513 | def set_next_input(self, text): | |
507 | """Send the specified text to the frontend to be presented at the next |
|
514 | """Send the specified text to the frontend to be presented at the next | |
508 | input cell.""" |
|
515 | input cell.""" | |
509 | payload = dict( |
|
516 | payload = dict( | |
510 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input', |
|
517 | source='IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input', | |
511 | text=text |
|
518 | text=text | |
512 | ) |
|
519 | ) | |
513 | self.payload_manager.write_payload(payload) |
|
520 | self.payload_manager.write_payload(payload) | |
514 |
|
521 | |||
515 |
|
522 | |||
516 | InteractiveShellABC.register(ZMQInteractiveShell) |
|
523 | InteractiveShellABC.register(ZMQInteractiveShell) |
General Comments 0
You need to be logged in to leave comments.
Login now