##// END OF EJS Templates
Add support to embed videos.
Daniel Wehner -
Show More
@@ -1,812 +1,897 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) 2013 The IPython Development Team
10 # Copyright (C) 2013 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
20 from __future__ import print_function
21
21
22 import os
22 import os
23 import struct
23 import struct
24 import mimetypes
24
25
25 from IPython.core.formatters import _safe_get_formatter_method
26 from IPython.core.formatters import _safe_get_formatter_method
26 from IPython.utils.py3compat import (string_types, cast_bytes_py2, cast_unicode,
27 from IPython.utils.py3compat import (string_types, cast_bytes_py2, cast_unicode,
27 unicode_type)
28 unicode_type)
28 from IPython.testing.skipdoctest import skip_doctest
29 from IPython.testing.skipdoctest import skip_doctest
29 from .displaypub import publish_display_data
30 from .displaypub import publish_display_data
30
31
31 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
32 # utility functions
33 # utility functions
33 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
34
35
35 def _safe_exists(path):
36 def _safe_exists(path):
36 """Check path, but don't let exceptions raise"""
37 """Check path, but don't let exceptions raise"""
37 try:
38 try:
38 return os.path.exists(path)
39 return os.path.exists(path)
39 except Exception:
40 except Exception:
40 return False
41 return False
41
42
42 def _merge(d1, d2):
43 def _merge(d1, d2):
43 """Like update, but merges sub-dicts instead of clobbering at the top level.
44 """Like update, but merges sub-dicts instead of clobbering at the top level.
44
45
45 Updates d1 in-place
46 Updates d1 in-place
46 """
47 """
47
48
48 if not isinstance(d2, dict) or not isinstance(d1, dict):
49 if not isinstance(d2, dict) or not isinstance(d1, dict):
49 return d2
50 return d2
50 for key, value in d2.items():
51 for key, value in d2.items():
51 d1[key] = _merge(d1.get(key), value)
52 d1[key] = _merge(d1.get(key), value)
52 return d1
53 return d1
53
54
54 def _display_mimetype(mimetype, objs, raw=False, metadata=None):
55 def _display_mimetype(mimetype, objs, raw=False, metadata=None):
55 """internal implementation of all display_foo methods
56 """internal implementation of all display_foo methods
56
57
57 Parameters
58 Parameters
58 ----------
59 ----------
59 mimetype : str
60 mimetype : str
60 The mimetype to be published (e.g. 'image/png')
61 The mimetype to be published (e.g. 'image/png')
61 objs : tuple of objects
62 objs : tuple of objects
62 The Python objects to display, or if raw=True raw text data to
63 The Python objects to display, or if raw=True raw text data to
63 display.
64 display.
64 raw : bool
65 raw : bool
65 Are the data objects raw data or Python objects that need to be
66 Are the data objects raw data or Python objects that need to be
66 formatted before display? [default: False]
67 formatted before display? [default: False]
67 metadata : dict (optional)
68 metadata : dict (optional)
68 Metadata to be associated with the specific mimetype output.
69 Metadata to be associated with the specific mimetype output.
69 """
70 """
70 if metadata:
71 if metadata:
71 metadata = {mimetype: metadata}
72 metadata = {mimetype: metadata}
72 if raw:
73 if raw:
73 # turn list of pngdata into list of { 'image/png': pngdata }
74 # turn list of pngdata into list of { 'image/png': pngdata }
74 objs = [ {mimetype: obj} for obj in objs ]
75 objs = [ {mimetype: obj} for obj in objs ]
75 display(*objs, raw=raw, metadata=metadata, include=[mimetype])
76 display(*objs, raw=raw, metadata=metadata, include=[mimetype])
76
77
77 #-----------------------------------------------------------------------------
78 #-----------------------------------------------------------------------------
78 # Main functions
79 # Main functions
79 #-----------------------------------------------------------------------------
80 #-----------------------------------------------------------------------------
80
81
81 def display(*objs, **kwargs):
82 def display(*objs, **kwargs):
82 """Display a Python object in all frontends.
83 """Display a Python object in all frontends.
83
84
84 By default all representations will be computed and sent to the frontends.
85 By default all representations will be computed and sent to the frontends.
85 Frontends can decide which representation is used and how.
86 Frontends can decide which representation is used and how.
86
87
87 Parameters
88 Parameters
88 ----------
89 ----------
89 objs : tuple of objects
90 objs : tuple of objects
90 The Python objects to display.
91 The Python objects to display.
91 raw : bool, optional
92 raw : bool, optional
92 Are the objects to be displayed already mimetype-keyed dicts of raw display data,
93 Are the objects to be displayed already mimetype-keyed dicts of raw display data,
93 or Python objects that need to be formatted before display? [default: False]
94 or Python objects that need to be formatted before display? [default: False]
94 include : list or tuple, optional
95 include : list or tuple, optional
95 A list of format type strings (MIME types) to include in the
96 A list of format type strings (MIME types) to include in the
96 format data dict. If this is set *only* the format types included
97 format data dict. If this is set *only* the format types included
97 in this list will be computed.
98 in this list will be computed.
98 exclude : list or tuple, optional
99 exclude : list or tuple, optional
99 A list of format type strings (MIME types) to exclude in the format
100 A list of format type strings (MIME types) to exclude in the format
100 data dict. If this is set all format types will be computed,
101 data dict. If this is set all format types will be computed,
101 except for those included in this argument.
102 except for those included in this argument.
102 metadata : dict, optional
103 metadata : dict, optional
103 A dictionary of metadata to associate with the output.
104 A dictionary of metadata to associate with the output.
104 mime-type keys in this dictionary will be associated with the individual
105 mime-type keys in this dictionary will be associated with the individual
105 representation formats, if they exist.
106 representation formats, if they exist.
106 """
107 """
107 raw = kwargs.get('raw', False)
108 raw = kwargs.get('raw', False)
108 include = kwargs.get('include')
109 include = kwargs.get('include')
109 exclude = kwargs.get('exclude')
110 exclude = kwargs.get('exclude')
110 metadata = kwargs.get('metadata')
111 metadata = kwargs.get('metadata')
111
112
112 from IPython.core.interactiveshell import InteractiveShell
113 from IPython.core.interactiveshell import InteractiveShell
113
114
114 if not raw:
115 if not raw:
115 format = InteractiveShell.instance().display_formatter.format
116 format = InteractiveShell.instance().display_formatter.format
116
117
117 for obj in objs:
118 for obj in objs:
118
119
119 # If _ipython_display_ is defined, use that to display this object.
120 # If _ipython_display_ is defined, use that to display this object.
120 display_method = _safe_get_formatter_method(obj, '_ipython_display_')
121 display_method = _safe_get_formatter_method(obj, '_ipython_display_')
121 if display_method is not None:
122 if display_method is not None:
122 try:
123 try:
123 display_method(**kwargs)
124 display_method(**kwargs)
124 except NotImplementedError:
125 except NotImplementedError:
125 pass
126 pass
126 else:
127 else:
127 continue
128 continue
128 if raw:
129 if raw:
129 publish_display_data(data=obj, metadata=metadata)
130 publish_display_data(data=obj, metadata=metadata)
130 else:
131 else:
131 format_dict, md_dict = format(obj, include=include, exclude=exclude)
132 format_dict, md_dict = format(obj, include=include, exclude=exclude)
132 if metadata:
133 if metadata:
133 # kwarg-specified metadata gets precedence
134 # kwarg-specified metadata gets precedence
134 _merge(md_dict, metadata)
135 _merge(md_dict, metadata)
135 publish_display_data(data=format_dict, metadata=md_dict)
136 publish_display_data(data=format_dict, metadata=md_dict)
136
137
137
138
138 def display_pretty(*objs, **kwargs):
139 def display_pretty(*objs, **kwargs):
139 """Display the pretty (default) representation of an object.
140 """Display the pretty (default) representation of an object.
140
141
141 Parameters
142 Parameters
142 ----------
143 ----------
143 objs : tuple of objects
144 objs : tuple of objects
144 The Python objects to display, or if raw=True raw text data to
145 The Python objects to display, or if raw=True raw text data to
145 display.
146 display.
146 raw : bool
147 raw : bool
147 Are the data objects raw data or Python objects that need to be
148 Are the data objects raw data or Python objects that need to be
148 formatted before display? [default: False]
149 formatted before display? [default: False]
149 metadata : dict (optional)
150 metadata : dict (optional)
150 Metadata to be associated with the specific mimetype output.
151 Metadata to be associated with the specific mimetype output.
151 """
152 """
152 _display_mimetype('text/plain', objs, **kwargs)
153 _display_mimetype('text/plain', objs, **kwargs)
153
154
154
155
155 def display_html(*objs, **kwargs):
156 def display_html(*objs, **kwargs):
156 """Display the HTML representation of an object.
157 """Display the HTML representation of an object.
157
158
158 Parameters
159 Parameters
159 ----------
160 ----------
160 objs : tuple of objects
161 objs : tuple of objects
161 The Python objects to display, or if raw=True raw HTML data to
162 The Python objects to display, or if raw=True raw HTML data to
162 display.
163 display.
163 raw : bool
164 raw : bool
164 Are the data objects raw data or Python objects that need to be
165 Are the data objects raw data or Python objects that need to be
165 formatted before display? [default: False]
166 formatted before display? [default: False]
166 metadata : dict (optional)
167 metadata : dict (optional)
167 Metadata to be associated with the specific mimetype output.
168 Metadata to be associated with the specific mimetype output.
168 """
169 """
169 _display_mimetype('text/html', objs, **kwargs)
170 _display_mimetype('text/html', objs, **kwargs)
170
171
171
172
172 def display_markdown(*objs, **kwargs):
173 def display_markdown(*objs, **kwargs):
173 """Displays the Markdown representation of an object.
174 """Displays the Markdown representation of an object.
174
175
175 Parameters
176 Parameters
176 ----------
177 ----------
177 objs : tuple of objects
178 objs : tuple of objects
178 The Python objects to display, or if raw=True raw markdown data to
179 The Python objects to display, or if raw=True raw markdown data to
179 display.
180 display.
180 raw : bool
181 raw : bool
181 Are the data objects raw data or Python objects that need to be
182 Are the data objects raw data or Python objects that need to be
182 formatted before display? [default: False]
183 formatted before display? [default: False]
183 metadata : dict (optional)
184 metadata : dict (optional)
184 Metadata to be associated with the specific mimetype output.
185 Metadata to be associated with the specific mimetype output.
185 """
186 """
186
187
187 _display_mimetype('text/markdown', objs, **kwargs)
188 _display_mimetype('text/markdown', objs, **kwargs)
188
189
189
190
190 def display_svg(*objs, **kwargs):
191 def display_svg(*objs, **kwargs):
191 """Display the SVG representation of an object.
192 """Display the SVG representation of an object.
192
193
193 Parameters
194 Parameters
194 ----------
195 ----------
195 objs : tuple of objects
196 objs : tuple of objects
196 The Python objects to display, or if raw=True raw svg data to
197 The Python objects to display, or if raw=True raw svg data to
197 display.
198 display.
198 raw : bool
199 raw : bool
199 Are the data objects raw data or Python objects that need to be
200 Are the data objects raw data or Python objects that need to be
200 formatted before display? [default: False]
201 formatted before display? [default: False]
201 metadata : dict (optional)
202 metadata : dict (optional)
202 Metadata to be associated with the specific mimetype output.
203 Metadata to be associated with the specific mimetype output.
203 """
204 """
204 _display_mimetype('image/svg+xml', objs, **kwargs)
205 _display_mimetype('image/svg+xml', objs, **kwargs)
205
206
206
207
207 def display_png(*objs, **kwargs):
208 def display_png(*objs, **kwargs):
208 """Display the PNG representation of an object.
209 """Display the PNG representation of an object.
209
210
210 Parameters
211 Parameters
211 ----------
212 ----------
212 objs : tuple of objects
213 objs : tuple of objects
213 The Python objects to display, or if raw=True raw png data to
214 The Python objects to display, or if raw=True raw png data to
214 display.
215 display.
215 raw : bool
216 raw : bool
216 Are the data objects raw data or Python objects that need to be
217 Are the data objects raw data or Python objects that need to be
217 formatted before display? [default: False]
218 formatted before display? [default: False]
218 metadata : dict (optional)
219 metadata : dict (optional)
219 Metadata to be associated with the specific mimetype output.
220 Metadata to be associated with the specific mimetype output.
220 """
221 """
221 _display_mimetype('image/png', objs, **kwargs)
222 _display_mimetype('image/png', objs, **kwargs)
222
223
223
224
224 def display_jpeg(*objs, **kwargs):
225 def display_jpeg(*objs, **kwargs):
225 """Display the JPEG representation of an object.
226 """Display the JPEG representation of an object.
226
227
227 Parameters
228 Parameters
228 ----------
229 ----------
229 objs : tuple of objects
230 objs : tuple of objects
230 The Python objects to display, or if raw=True raw JPEG data to
231 The Python objects to display, or if raw=True raw JPEG data to
231 display.
232 display.
232 raw : bool
233 raw : bool
233 Are the data objects raw data or Python objects that need to be
234 Are the data objects raw data or Python objects that need to be
234 formatted before display? [default: False]
235 formatted before display? [default: False]
235 metadata : dict (optional)
236 metadata : dict (optional)
236 Metadata to be associated with the specific mimetype output.
237 Metadata to be associated with the specific mimetype output.
237 """
238 """
238 _display_mimetype('image/jpeg', objs, **kwargs)
239 _display_mimetype('image/jpeg', objs, **kwargs)
239
240
240
241
241 def display_latex(*objs, **kwargs):
242 def display_latex(*objs, **kwargs):
242 """Display the LaTeX representation of an object.
243 """Display the LaTeX representation of an object.
243
244
244 Parameters
245 Parameters
245 ----------
246 ----------
246 objs : tuple of objects
247 objs : tuple of objects
247 The Python objects to display, or if raw=True raw latex data to
248 The Python objects to display, or if raw=True raw latex data to
248 display.
249 display.
249 raw : bool
250 raw : bool
250 Are the data objects raw data or Python objects that need to be
251 Are the data objects raw data or Python objects that need to be
251 formatted before display? [default: False]
252 formatted before display? [default: False]
252 metadata : dict (optional)
253 metadata : dict (optional)
253 Metadata to be associated with the specific mimetype output.
254 Metadata to be associated with the specific mimetype output.
254 """
255 """
255 _display_mimetype('text/latex', objs, **kwargs)
256 _display_mimetype('text/latex', objs, **kwargs)
256
257
257
258
258 def display_json(*objs, **kwargs):
259 def display_json(*objs, **kwargs):
259 """Display the JSON representation of an object.
260 """Display the JSON representation of an object.
260
261
261 Note that not many frontends support displaying JSON.
262 Note that not many frontends support displaying JSON.
262
263
263 Parameters
264 Parameters
264 ----------
265 ----------
265 objs : tuple of objects
266 objs : tuple of objects
266 The Python objects to display, or if raw=True raw json data to
267 The Python objects to display, or if raw=True raw json data to
267 display.
268 display.
268 raw : bool
269 raw : bool
269 Are the data objects raw data or Python objects that need to be
270 Are the data objects raw data or Python objects that need to be
270 formatted before display? [default: False]
271 formatted before display? [default: False]
271 metadata : dict (optional)
272 metadata : dict (optional)
272 Metadata to be associated with the specific mimetype output.
273 Metadata to be associated with the specific mimetype output.
273 """
274 """
274 _display_mimetype('application/json', objs, **kwargs)
275 _display_mimetype('application/json', objs, **kwargs)
275
276
276
277
277 def display_javascript(*objs, **kwargs):
278 def display_javascript(*objs, **kwargs):
278 """Display the Javascript representation of an object.
279 """Display the Javascript representation of an object.
279
280
280 Parameters
281 Parameters
281 ----------
282 ----------
282 objs : tuple of objects
283 objs : tuple of objects
283 The Python objects to display, or if raw=True raw javascript data to
284 The Python objects to display, or if raw=True raw javascript data to
284 display.
285 display.
285 raw : bool
286 raw : bool
286 Are the data objects raw data or Python objects that need to be
287 Are the data objects raw data or Python objects that need to be
287 formatted before display? [default: False]
288 formatted before display? [default: False]
288 metadata : dict (optional)
289 metadata : dict (optional)
289 Metadata to be associated with the specific mimetype output.
290 Metadata to be associated with the specific mimetype output.
290 """
291 """
291 _display_mimetype('application/javascript', objs, **kwargs)
292 _display_mimetype('application/javascript', objs, **kwargs)
292
293
293
294
294 def display_pdf(*objs, **kwargs):
295 def display_pdf(*objs, **kwargs):
295 """Display the PDF representation of an object.
296 """Display the PDF representation of an object.
296
297
297 Parameters
298 Parameters
298 ----------
299 ----------
299 objs : tuple of objects
300 objs : tuple of objects
300 The Python objects to display, or if raw=True raw javascript data to
301 The Python objects to display, or if raw=True raw javascript data to
301 display.
302 display.
302 raw : bool
303 raw : bool
303 Are the data objects raw data or Python objects that need to be
304 Are the data objects raw data or Python objects that need to be
304 formatted before display? [default: False]
305 formatted before display? [default: False]
305 metadata : dict (optional)
306 metadata : dict (optional)
306 Metadata to be associated with the specific mimetype output.
307 Metadata to be associated with the specific mimetype output.
307 """
308 """
308 _display_mimetype('application/pdf', objs, **kwargs)
309 _display_mimetype('application/pdf', objs, **kwargs)
309
310
310
311
311 #-----------------------------------------------------------------------------
312 #-----------------------------------------------------------------------------
312 # Smart classes
313 # Smart classes
313 #-----------------------------------------------------------------------------
314 #-----------------------------------------------------------------------------
314
315
315
316
316 class DisplayObject(object):
317 class DisplayObject(object):
317 """An object that wraps data to be displayed."""
318 """An object that wraps data to be displayed."""
318
319
319 _read_flags = 'r'
320 _read_flags = 'r'
320 _show_mem_addr = False
321 _show_mem_addr = False
321
322
322 def __init__(self, data=None, url=None, filename=None):
323 def __init__(self, data=None, url=None, filename=None):
323 """Create a display object given raw data.
324 """Create a display object given raw data.
324
325
325 When this object is returned by an expression or passed to the
326 When this object is returned by an expression or passed to the
326 display function, it will result in the data being displayed
327 display function, it will result in the data being displayed
327 in the frontend. The MIME type of the data should match the
328 in the frontend. The MIME type of the data should match the
328 subclasses used, so the Png subclass should be used for 'image/png'
329 subclasses used, so the Png subclass should be used for 'image/png'
329 data. If the data is a URL, the data will first be downloaded
330 data. If the data is a URL, the data will first be downloaded
330 and then displayed. If
331 and then displayed. If
331
332
332 Parameters
333 Parameters
333 ----------
334 ----------
334 data : unicode, str or bytes
335 data : unicode, str or bytes
335 The raw data or a URL or file to load the data from
336 The raw data or a URL or file to load the data from
336 url : unicode
337 url : unicode
337 A URL to download the data from.
338 A URL to download the data from.
338 filename : unicode
339 filename : unicode
339 Path to a local file to load the data from.
340 Path to a local file to load the data from.
340 """
341 """
341 if data is not None and isinstance(data, string_types):
342 if data is not None and isinstance(data, string_types):
342 if data.startswith('http') and url is None:
343 if data.startswith('http') and url is None:
343 url = data
344 url = data
344 filename = None
345 filename = None
345 data = None
346 data = None
346 elif _safe_exists(data) and filename is None:
347 elif _safe_exists(data) and filename is None:
347 url = None
348 url = None
348 filename = data
349 filename = data
349 data = None
350 data = None
350
351
351 self.data = data
352 self.data = data
352 self.url = url
353 self.url = url
353 self.filename = None if filename is None else unicode_type(filename)
354 self.filename = None if filename is None else unicode_type(filename)
354
355
355 self.reload()
356 self.reload()
356 self._check_data()
357 self._check_data()
357
358
358 def __repr__(self):
359 def __repr__(self):
359 if not self._show_mem_addr:
360 if not self._show_mem_addr:
360 cls = self.__class__
361 cls = self.__class__
361 r = "<%s.%s object>" % (cls.__module__, cls.__name__)
362 r = "<%s.%s object>" % (cls.__module__, cls.__name__)
362 else:
363 else:
363 r = super(DisplayObject, self).__repr__()
364 r = super(DisplayObject, self).__repr__()
364 return r
365 return r
365
366
366 def _check_data(self):
367 def _check_data(self):
367 """Override in subclasses if there's something to check."""
368 """Override in subclasses if there's something to check."""
368 pass
369 pass
369
370
370 def reload(self):
371 def reload(self):
371 """Reload the raw data from file or URL."""
372 """Reload the raw data from file or URL."""
372 if self.filename is not None:
373 if self.filename is not None:
373 with open(self.filename, self._read_flags) as f:
374 with open(self.filename, self._read_flags) as f:
374 self.data = f.read()
375 self.data = f.read()
375 elif self.url is not None:
376 elif self.url is not None:
376 try:
377 try:
377 try:
378 try:
378 from urllib.request import urlopen # Py3
379 from urllib.request import urlopen # Py3
379 except ImportError:
380 except ImportError:
380 from urllib2 import urlopen
381 from urllib2 import urlopen
381 response = urlopen(self.url)
382 response = urlopen(self.url)
382 self.data = response.read()
383 self.data = response.read()
383 # extract encoding from header, if there is one:
384 # extract encoding from header, if there is one:
384 encoding = None
385 encoding = None
385 for sub in response.headers['content-type'].split(';'):
386 for sub in response.headers['content-type'].split(';'):
386 sub = sub.strip()
387 sub = sub.strip()
387 if sub.startswith('charset'):
388 if sub.startswith('charset'):
388 encoding = sub.split('=')[-1].strip()
389 encoding = sub.split('=')[-1].strip()
389 break
390 break
390 # decode data, if an encoding was specified
391 # decode data, if an encoding was specified
391 if encoding:
392 if encoding:
392 self.data = self.data.decode(encoding, 'replace')
393 self.data = self.data.decode(encoding, 'replace')
393 except:
394 except:
394 self.data = None
395 self.data = None
395
396
396 class TextDisplayObject(DisplayObject):
397 class TextDisplayObject(DisplayObject):
397 """Validate that display data is text"""
398 """Validate that display data is text"""
398 def _check_data(self):
399 def _check_data(self):
399 if self.data is not None and not isinstance(self.data, string_types):
400 if self.data is not None and not isinstance(self.data, string_types):
400 raise TypeError("%s expects text, not %r" % (self.__class__.__name__, self.data))
401 raise TypeError("%s expects text, not %r" % (self.__class__.__name__, self.data))
401
402
402 class Pretty(TextDisplayObject):
403 class Pretty(TextDisplayObject):
403
404
404 def _repr_pretty_(self):
405 def _repr_pretty_(self):
405 return self.data
406 return self.data
406
407
407
408
408 class HTML(TextDisplayObject):
409 class HTML(TextDisplayObject):
409
410
410 def _repr_html_(self):
411 def _repr_html_(self):
411 return self.data
412 return self.data
412
413
413 def __html__(self):
414 def __html__(self):
414 """
415 """
415 This method exists to inform other HTML-using modules (e.g. Markupsafe,
416 This method exists to inform other HTML-using modules (e.g. Markupsafe,
416 htmltag, etc) that this object is HTML and does not need things like
417 htmltag, etc) that this object is HTML and does not need things like
417 special characters (<>&) escaped.
418 special characters (<>&) escaped.
418 """
419 """
419 return self._repr_html_()
420 return self._repr_html_()
420
421
421
422
422 class Markdown(TextDisplayObject):
423 class Markdown(TextDisplayObject):
423
424
424 def _repr_markdown_(self):
425 def _repr_markdown_(self):
425 return self.data
426 return self.data
426
427
427
428
428 class Math(TextDisplayObject):
429 class Math(TextDisplayObject):
429
430
430 def _repr_latex_(self):
431 def _repr_latex_(self):
431 s = self.data.strip('$')
432 s = self.data.strip('$')
432 return "$$%s$$" % s
433 return "$$%s$$" % s
433
434
434
435
435 class Latex(TextDisplayObject):
436 class Latex(TextDisplayObject):
436
437
437 def _repr_latex_(self):
438 def _repr_latex_(self):
438 return self.data
439 return self.data
439
440
440
441
441 class SVG(DisplayObject):
442 class SVG(DisplayObject):
442
443
443 # wrap data in a property, which extracts the <svg> tag, discarding
444 # wrap data in a property, which extracts the <svg> tag, discarding
444 # document headers
445 # document headers
445 _data = None
446 _data = None
446
447
447 @property
448 @property
448 def data(self):
449 def data(self):
449 return self._data
450 return self._data
450
451
451 @data.setter
452 @data.setter
452 def data(self, svg):
453 def data(self, svg):
453 if svg is None:
454 if svg is None:
454 self._data = None
455 self._data = None
455 return
456 return
456 # parse into dom object
457 # parse into dom object
457 from xml.dom import minidom
458 from xml.dom import minidom
458 svg = cast_bytes_py2(svg)
459 svg = cast_bytes_py2(svg)
459 x = minidom.parseString(svg)
460 x = minidom.parseString(svg)
460 # get svg tag (should be 1)
461 # get svg tag (should be 1)
461 found_svg = x.getElementsByTagName('svg')
462 found_svg = x.getElementsByTagName('svg')
462 if found_svg:
463 if found_svg:
463 svg = found_svg[0].toxml()
464 svg = found_svg[0].toxml()
464 else:
465 else:
465 # fallback on the input, trust the user
466 # fallback on the input, trust the user
466 # but this is probably an error.
467 # but this is probably an error.
467 pass
468 pass
468 svg = cast_unicode(svg)
469 svg = cast_unicode(svg)
469 self._data = svg
470 self._data = svg
470
471
471 def _repr_svg_(self):
472 def _repr_svg_(self):
472 return self.data
473 return self.data
473
474
474
475
475 class JSON(TextDisplayObject):
476 class JSON(TextDisplayObject):
476
477
477 def _repr_json_(self):
478 def _repr_json_(self):
478 return self.data
479 return self.data
479
480
480 css_t = """$("head").append($("<link/>").attr({
481 css_t = """$("head").append($("<link/>").attr({
481 rel: "stylesheet",
482 rel: "stylesheet",
482 type: "text/css",
483 type: "text/css",
483 href: "%s"
484 href: "%s"
484 }));
485 }));
485 """
486 """
486
487
487 lib_t1 = """$.getScript("%s", function () {
488 lib_t1 = """$.getScript("%s", function () {
488 """
489 """
489 lib_t2 = """});
490 lib_t2 = """});
490 """
491 """
491
492
492 class Javascript(TextDisplayObject):
493 class Javascript(TextDisplayObject):
493
494
494 def __init__(self, data=None, url=None, filename=None, lib=None, css=None):
495 def __init__(self, data=None, url=None, filename=None, lib=None, css=None):
495 """Create a Javascript display object given raw data.
496 """Create a Javascript display object given raw data.
496
497
497 When this object is returned by an expression or passed to the
498 When this object is returned by an expression or passed to the
498 display function, it will result in the data being displayed
499 display function, it will result in the data being displayed
499 in the frontend. If the data is a URL, the data will first be
500 in the frontend. If the data is a URL, the data will first be
500 downloaded and then displayed.
501 downloaded and then displayed.
501
502
502 In the Notebook, the containing element will be available as `element`,
503 In the Notebook, the containing element will be available as `element`,
503 and jQuery will be available. The output area starts hidden, so if
504 and jQuery will be available. The output area starts hidden, so if
504 the js appends content to `element` that should be visible, then
505 the js appends content to `element` that should be visible, then
505 it must call `container.show()` to unhide the area.
506 it must call `container.show()` to unhide the area.
506
507
507 Parameters
508 Parameters
508 ----------
509 ----------
509 data : unicode, str or bytes
510 data : unicode, str or bytes
510 The Javascript source code or a URL to download it from.
511 The Javascript source code or a URL to download it from.
511 url : unicode
512 url : unicode
512 A URL to download the data from.
513 A URL to download the data from.
513 filename : unicode
514 filename : unicode
514 Path to a local file to load the data from.
515 Path to a local file to load the data from.
515 lib : list or str
516 lib : list or str
516 A sequence of Javascript library URLs to load asynchronously before
517 A sequence of Javascript library URLs to load asynchronously before
517 running the source code. The full URLs of the libraries should
518 running the source code. The full URLs of the libraries should
518 be given. A single Javascript library URL can also be given as a
519 be given. A single Javascript library URL can also be given as a
519 string.
520 string.
520 css: : list or str
521 css: : list or str
521 A sequence of css files to load before running the source code.
522 A sequence of css files to load before running the source code.
522 The full URLs of the css files should be given. A single css URL
523 The full URLs of the css files should be given. A single css URL
523 can also be given as a string.
524 can also be given as a string.
524 """
525 """
525 if isinstance(lib, string_types):
526 if isinstance(lib, string_types):
526 lib = [lib]
527 lib = [lib]
527 elif lib is None:
528 elif lib is None:
528 lib = []
529 lib = []
529 if isinstance(css, string_types):
530 if isinstance(css, string_types):
530 css = [css]
531 css = [css]
531 elif css is None:
532 elif css is None:
532 css = []
533 css = []
533 if not isinstance(lib, (list,tuple)):
534 if not isinstance(lib, (list,tuple)):
534 raise TypeError('expected sequence, got: %r' % lib)
535 raise TypeError('expected sequence, got: %r' % lib)
535 if not isinstance(css, (list,tuple)):
536 if not isinstance(css, (list,tuple)):
536 raise TypeError('expected sequence, got: %r' % css)
537 raise TypeError('expected sequence, got: %r' % css)
537 self.lib = lib
538 self.lib = lib
538 self.css = css
539 self.css = css
539 super(Javascript, self).__init__(data=data, url=url, filename=filename)
540 super(Javascript, self).__init__(data=data, url=url, filename=filename)
540
541
541 def _repr_javascript_(self):
542 def _repr_javascript_(self):
542 r = ''
543 r = ''
543 for c in self.css:
544 for c in self.css:
544 r += css_t % c
545 r += css_t % c
545 for l in self.lib:
546 for l in self.lib:
546 r += lib_t1 % l
547 r += lib_t1 % l
547 r += self.data
548 r += self.data
548 r += lib_t2*len(self.lib)
549 r += lib_t2*len(self.lib)
549 return r
550 return r
550
551
551 # constants for identifying png/jpeg data
552 # constants for identifying png/jpeg data
552 _PNG = b'\x89PNG\r\n\x1a\n'
553 _PNG = b'\x89PNG\r\n\x1a\n'
553 _JPEG = b'\xff\xd8'
554 _JPEG = b'\xff\xd8'
554
555
555 def _pngxy(data):
556 def _pngxy(data):
556 """read the (width, height) from a PNG header"""
557 """read the (width, height) from a PNG header"""
557 ihdr = data.index(b'IHDR')
558 ihdr = data.index(b'IHDR')
558 # next 8 bytes are width/height
559 # next 8 bytes are width/height
559 w4h4 = data[ihdr+4:ihdr+12]
560 w4h4 = data[ihdr+4:ihdr+12]
560 return struct.unpack('>ii', w4h4)
561 return struct.unpack('>ii', w4h4)
561
562
562 def _jpegxy(data):
563 def _jpegxy(data):
563 """read the (width, height) from a JPEG header"""
564 """read the (width, height) from a JPEG header"""
564 # adapted from http://www.64lines.com/jpeg-width-height
565 # adapted from http://www.64lines.com/jpeg-width-height
565
566
566 idx = 4
567 idx = 4
567 while True:
568 while True:
568 block_size = struct.unpack('>H', data[idx:idx+2])[0]
569 block_size = struct.unpack('>H', data[idx:idx+2])[0]
569 idx = idx + block_size
570 idx = idx + block_size
570 if data[idx:idx+2] == b'\xFF\xC0':
571 if data[idx:idx+2] == b'\xFF\xC0':
571 # found Start of Frame
572 # found Start of Frame
572 iSOF = idx
573 iSOF = idx
573 break
574 break
574 else:
575 else:
575 # read another block
576 # read another block
576 idx += 2
577 idx += 2
577
578
578 h, w = struct.unpack('>HH', data[iSOF+5:iSOF+9])
579 h, w = struct.unpack('>HH', data[iSOF+5:iSOF+9])
579 return w, h
580 return w, h
580
581
581 class Image(DisplayObject):
582 class Image(DisplayObject):
582
583
583 _read_flags = 'rb'
584 _read_flags = 'rb'
584 _FMT_JPEG = u'jpeg'
585 _FMT_JPEG = u'jpeg'
585 _FMT_PNG = u'png'
586 _FMT_PNG = u'png'
586 _ACCEPTABLE_EMBEDDINGS = [_FMT_JPEG, _FMT_PNG]
587 _ACCEPTABLE_EMBEDDINGS = [_FMT_JPEG, _FMT_PNG]
587
588
588 def __init__(self, data=None, url=None, filename=None, format=u'png', embed=None, width=None, height=None, retina=False):
589 def __init__(self, data=None, url=None, filename=None, format=u'png', embed=None, width=None, height=None, retina=False):
589 """Create a PNG/JPEG image object given raw data.
590 """Create a PNG/JPEG image object given raw data.
590
591
591 When this object is returned by an input cell or passed to the
592 When this object is returned by an input cell or passed to the
592 display function, it will result in the image being displayed
593 display function, it will result in the image being displayed
593 in the frontend.
594 in the frontend.
594
595
595 Parameters
596 Parameters
596 ----------
597 ----------
597 data : unicode, str or bytes
598 data : unicode, str or bytes
598 The raw image data or a URL or filename to load the data from.
599 The raw image data or a URL or filename to load the data from.
599 This always results in embedded image data.
600 This always results in embedded image data.
600 url : unicode
601 url : unicode
601 A URL to download the data from. If you specify `url=`,
602 A URL to download the data from. If you specify `url=`,
602 the image data will not be embedded unless you also specify `embed=True`.
603 the image data will not be embedded unless you also specify `embed=True`.
603 filename : unicode
604 filename : unicode
604 Path to a local file to load the data from.
605 Path to a local file to load the data from.
605 Images from a file are always embedded.
606 Images from a file are always embedded.
606 format : unicode
607 format : unicode
607 The format of the image data (png/jpeg/jpg). If a filename or URL is given
608 The format of the image data (png/jpeg/jpg). If a filename or URL is given
608 for format will be inferred from the filename extension.
609 for format will be inferred from the filename extension.
609 embed : bool
610 embed : bool
610 Should the image data be embedded using a data URI (True) or be
611 Should the image data be embedded using a data URI (True) or be
611 loaded using an <img> tag. Set this to True if you want the image
612 loaded using an <img> tag. Set this to True if you want the image
612 to be viewable later with no internet connection in the notebook.
613 to be viewable later with no internet connection in the notebook.
613
614
614 Default is `True`, unless the keyword argument `url` is set, then
615 Default is `True`, unless the keyword argument `url` is set, then
615 default value is `False`.
616 default value is `False`.
616
617
617 Note that QtConsole is not able to display images if `embed` is set to `False`
618 Note that QtConsole is not able to display images if `embed` is set to `False`
618 width : int
619 width : int
619 Width to which to constrain the image in html
620 Width to which to constrain the image in html
620 height : int
621 height : int
621 Height to which to constrain the image in html
622 Height to which to constrain the image in html
622 retina : bool
623 retina : bool
623 Automatically set the width and height to half of the measured
624 Automatically set the width and height to half of the measured
624 width and height.
625 width and height.
625 This only works for embedded images because it reads the width/height
626 This only works for embedded images because it reads the width/height
626 from image data.
627 from image data.
627 For non-embedded images, you can just set the desired display width
628 For non-embedded images, you can just set the desired display width
628 and height directly.
629 and height directly.
629
630
630 Examples
631 Examples
631 --------
632 --------
632 # embedded image data, works in qtconsole and notebook
633 # embedded image data, works in qtconsole and notebook
633 # when passed positionally, the first arg can be any of raw image data,
634 # when passed positionally, the first arg can be any of raw image data,
634 # a URL, or a filename from which to load image data.
635 # a URL, or a filename from which to load image data.
635 # The result is always embedding image data for inline images.
636 # The result is always embedding image data for inline images.
636 Image('http://www.google.fr/images/srpr/logo3w.png')
637 Image('http://www.google.fr/images/srpr/logo3w.png')
637 Image('/path/to/image.jpg')
638 Image('/path/to/image.jpg')
638 Image(b'RAW_PNG_DATA...')
639 Image(b'RAW_PNG_DATA...')
639
640
640 # Specifying Image(url=...) does not embed the image data,
641 # Specifying Image(url=...) does not embed the image data,
641 # it only generates `<img>` tag with a link to the source.
642 # it only generates `<img>` tag with a link to the source.
642 # This will not work in the qtconsole or offline.
643 # This will not work in the qtconsole or offline.
643 Image(url='http://www.google.fr/images/srpr/logo3w.png')
644 Image(url='http://www.google.fr/images/srpr/logo3w.png')
644
645
645 """
646 """
646 if filename is not None:
647 if filename is not None:
647 ext = self._find_ext(filename)
648 ext = self._find_ext(filename)
648 elif url is not None:
649 elif url is not None:
649 ext = self._find_ext(url)
650 ext = self._find_ext(url)
650 elif data is None:
651 elif data is None:
651 raise ValueError("No image data found. Expecting filename, url, or data.")
652 raise ValueError("No image data found. Expecting filename, url, or data.")
652 elif isinstance(data, string_types) and (
653 elif isinstance(data, string_types) and (
653 data.startswith('http') or _safe_exists(data)
654 data.startswith('http') or _safe_exists(data)
654 ):
655 ):
655 ext = self._find_ext(data)
656 ext = self._find_ext(data)
656 else:
657 else:
657 ext = None
658 ext = None
658
659
659 if ext is not None:
660 if ext is not None:
660 format = ext.lower()
661 format = ext.lower()
661 if ext == u'jpg' or ext == u'jpeg':
662 if ext == u'jpg' or ext == u'jpeg':
662 format = self._FMT_JPEG
663 format = self._FMT_JPEG
663 if ext == u'png':
664 if ext == u'png':
664 format = self._FMT_PNG
665 format = self._FMT_PNG
665 elif isinstance(data, bytes) and format == 'png':
666 elif isinstance(data, bytes) and format == 'png':
666 # infer image type from image data header,
667 # infer image type from image data header,
667 # only if format might not have been specified.
668 # only if format might not have been specified.
668 if data[:2] == _JPEG:
669 if data[:2] == _JPEG:
669 format = 'jpeg'
670 format = 'jpeg'
670
671
671 self.format = unicode_type(format).lower()
672 self.format = unicode_type(format).lower()
672 self.embed = embed if embed is not None else (url is None)
673 self.embed = embed if embed is not None else (url is None)
673
674
674 if self.embed and self.format not in self._ACCEPTABLE_EMBEDDINGS:
675 if self.embed and self.format not in self._ACCEPTABLE_EMBEDDINGS:
675 raise ValueError("Cannot embed the '%s' image format" % (self.format))
676 raise ValueError("Cannot embed the '%s' image format" % (self.format))
676 self.width = width
677 self.width = width
677 self.height = height
678 self.height = height
678 self.retina = retina
679 self.retina = retina
679 super(Image, self).__init__(data=data, url=url, filename=filename)
680 super(Image, self).__init__(data=data, url=url, filename=filename)
680
681
681 if retina:
682 if retina:
682 self._retina_shape()
683 self._retina_shape()
683
684
684 def _retina_shape(self):
685 def _retina_shape(self):
685 """load pixel-doubled width and height from image data"""
686 """load pixel-doubled width and height from image data"""
686 if not self.embed:
687 if not self.embed:
687 return
688 return
688 if self.format == 'png':
689 if self.format == 'png':
689 w, h = _pngxy(self.data)
690 w, h = _pngxy(self.data)
690 elif self.format == 'jpeg':
691 elif self.format == 'jpeg':
691 w, h = _jpegxy(self.data)
692 w, h = _jpegxy(self.data)
692 else:
693 else:
693 # retina only supports png
694 # retina only supports png
694 return
695 return
695 self.width = w // 2
696 self.width = w // 2
696 self.height = h // 2
697 self.height = h // 2
697
698
698 def reload(self):
699 def reload(self):
699 """Reload the raw data from file or URL."""
700 """Reload the raw data from file or URL."""
700 if self.embed:
701 if self.embed:
701 super(Image,self).reload()
702 super(Image,self).reload()
702 if self.retina:
703 if self.retina:
703 self._retina_shape()
704 self._retina_shape()
704
705
705 def _repr_html_(self):
706 def _repr_html_(self):
706 if not self.embed:
707 if not self.embed:
707 width = height = ''
708 width = height = ''
708 if self.width:
709 if self.width:
709 width = ' width="%d"' % self.width
710 width = ' width="%d"' % self.width
710 if self.height:
711 if self.height:
711 height = ' height="%d"' % self.height
712 height = ' height="%d"' % self.height
712 return u'<img src="%s"%s%s/>' % (self.url, width, height)
713 return u'<img src="%s"%s%s/>' % (self.url, width, height)
713
714
714 def _data_and_metadata(self):
715 def _data_and_metadata(self):
715 """shortcut for returning metadata with shape information, if defined"""
716 """shortcut for returning metadata with shape information, if defined"""
716 md = {}
717 md = {}
717 if self.width:
718 if self.width:
718 md['width'] = self.width
719 md['width'] = self.width
719 if self.height:
720 if self.height:
720 md['height'] = self.height
721 md['height'] = self.height
721 if md:
722 if md:
722 return self.data, md
723 return self.data, md
723 else:
724 else:
724 return self.data
725 return self.data
725
726
726 def _repr_png_(self):
727 def _repr_png_(self):
727 if self.embed and self.format == u'png':
728 if self.embed and self.format == u'png':
728 return self._data_and_metadata()
729 return self._data_and_metadata()
729
730
730 def _repr_jpeg_(self):
731 def _repr_jpeg_(self):
731 if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
732 if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
732 return self._data_and_metadata()
733 return self._data_and_metadata()
733
734
734 def _find_ext(self, s):
735 def _find_ext(self, s):
735 return unicode_type(s.split('.')[-1].lower())
736 return unicode_type(s.split('.')[-1].lower())
736
737
738 class Video(DisplayObject):
739
740 def __init__(self, data=None, url=None, filename=None, embed=None, mimetype=None):
741 """Create a video object given raw data or an URL.
742
743 When this object is returned by an input cell or passed to the
744 display function, it will result in the video being displayed
745 in the frontend.
746
747 Parameters
748 ----------
749 data : unicode, str or bytes
750 The raw image data or a URL or filename to load the data from.
751 This always results in embedded image data.
752 url : unicode
753 A URL to download the data from. If you specify `url=`,
754 the image data will not be embedded unless you also specify `embed=True`.
755 filename : unicode
756 Path to a local file to load the data from.
757 Videos from a file are always embedded.
758 embed : bool
759 Should the image data be embedded using a data URI (True) or be
760 loaded using an <img> tag. Set this to True if you want the image
761 to be viewable later with no internet connection in the notebook.
762
763 Default is `True`, unless the keyword argument `url` is set, then
764 default value is `False`.
765
766 Note that QtConsole is not able to display images if `embed` is set to `False`
767 mimetype: unicode
768 Specify the mimetype in case you load in a encoded video.
769 Examples
770 --------
771 Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4')
772 Video('path/to/video.mp4')
773 Video('path/to/video.mp4', embed=False)
774 """
775 if url is None and (data.startswith('http') or data.startswith('https')):
776 url = data
777 data = None
778 embed = False
779 elif os.path.exists(data):
780 filename = data
781 data = None
782
783 self.mimetype = mimetype
784 self.embed = embed if embed is not None else (filename is not None)
785 super(Video, self).__init__(data=data, url=url, filename=filename)
786
787 def _repr_html_(self):
788 # External URLs and potentially local files are not embedded into the
789 # notebook output.
790 if not self.embed:
791 url = self.url if self.url is not None else self.filename
792 output = """<video src="{0}" controls>
793 Your browser does not support the <code>video</code> element.
794 </video>""".format(url)
795 return output
796 # Embedded videos uses base64 encoded videos.
797 if self.filename is not None:
798 mimetypes.init()
799 mimetype, encoding = mimetypes.guess_type(self.filename)
800
801 video = open(self.filename, 'rb').read()
802 video_encoded = video.encode('base64')
803 else:
804 video_encoded = self.data
805 mimetype = self.mimetype
806 output = """<video controls>
807 <source src="data:{0};base64,{1}" type="{0}">
808 Your browser does not support the video tag.
809 </video>""".format(mimetype, video_encoded)
810 return output
811
812 def reload(self):
813 # TODO
814 pass
815
816 def _repr_png_(self):
817 # TODO
818 pass
819 def _repr_jpeg_(self):
820 # TODO
821 pass
737
822
738 def clear_output(wait=False):
823 def clear_output(wait=False):
739 """Clear the output of the current cell receiving output.
824 """Clear the output of the current cell receiving output.
740
825
741 Parameters
826 Parameters
742 ----------
827 ----------
743 wait : bool [default: false]
828 wait : bool [default: false]
744 Wait to clear the output until new output is available to replace it."""
829 Wait to clear the output until new output is available to replace it."""
745 from IPython.core.interactiveshell import InteractiveShell
830 from IPython.core.interactiveshell import InteractiveShell
746 if InteractiveShell.initialized():
831 if InteractiveShell.initialized():
747 InteractiveShell.instance().display_pub.clear_output(wait)
832 InteractiveShell.instance().display_pub.clear_output(wait)
748 else:
833 else:
749 from IPython.utils import io
834 from IPython.utils import io
750 print('\033[2K\r', file=io.stdout, end='')
835 print('\033[2K\r', file=io.stdout, end='')
751 io.stdout.flush()
836 io.stdout.flush()
752 print('\033[2K\r', file=io.stderr, end='')
837 print('\033[2K\r', file=io.stderr, end='')
753 io.stderr.flush()
838 io.stderr.flush()
754
839
755
840
756 @skip_doctest
841 @skip_doctest
757 def set_matplotlib_formats(*formats, **kwargs):
842 def set_matplotlib_formats(*formats, **kwargs):
758 """Select figure formats for the inline backend. Optionally pass quality for JPEG.
843 """Select figure formats for the inline backend. Optionally pass quality for JPEG.
759
844
760 For example, this enables PNG and JPEG output with a JPEG quality of 90%::
845 For example, this enables PNG and JPEG output with a JPEG quality of 90%::
761
846
762 In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)
847 In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)
763
848
764 To set this in your config files use the following::
849 To set this in your config files use the following::
765
850
766 c.InlineBackend.figure_formats = {'png', 'jpeg'}
851 c.InlineBackend.figure_formats = {'png', 'jpeg'}
767 c.InlineBackend.print_figure_kwargs.update({'quality' : 90})
852 c.InlineBackend.print_figure_kwargs.update({'quality' : 90})
768
853
769 Parameters
854 Parameters
770 ----------
855 ----------
771 *formats : strs
856 *formats : strs
772 One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
857 One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
773 **kwargs :
858 **kwargs :
774 Keyword args will be relayed to ``figure.canvas.print_figure``.
859 Keyword args will be relayed to ``figure.canvas.print_figure``.
775 """
860 """
776 from IPython.core.interactiveshell import InteractiveShell
861 from IPython.core.interactiveshell import InteractiveShell
777 from IPython.core.pylabtools import select_figure_formats
862 from IPython.core.pylabtools import select_figure_formats
778 from IPython.kernel.zmq.pylab.config import InlineBackend
863 from IPython.kernel.zmq.pylab.config import InlineBackend
779 # build kwargs, starting with InlineBackend config
864 # build kwargs, starting with InlineBackend config
780 kw = {}
865 kw = {}
781 cfg = InlineBackend.instance()
866 cfg = InlineBackend.instance()
782 kw.update(cfg.print_figure_kwargs)
867 kw.update(cfg.print_figure_kwargs)
783 kw.update(**kwargs)
868 kw.update(**kwargs)
784 shell = InteractiveShell.instance()
869 shell = InteractiveShell.instance()
785 select_figure_formats(shell, formats, **kw)
870 select_figure_formats(shell, formats, **kw)
786
871
787 @skip_doctest
872 @skip_doctest
788 def set_matplotlib_close(close=True):
873 def set_matplotlib_close(close=True):
789 """Set whether the inline backend closes all figures automatically or not.
874 """Set whether the inline backend closes all figures automatically or not.
790
875
791 By default, the inline backend used in the IPython Notebook will close all
876 By default, the inline backend used in the IPython Notebook will close all
792 matplotlib figures automatically after each cell is run. This means that
877 matplotlib figures automatically after each cell is run. This means that
793 plots in different cells won't interfere. Sometimes, you may want to make
878 plots in different cells won't interfere. Sometimes, you may want to make
794 a plot in one cell and then refine it in later cells. This can be accomplished
879 a plot in one cell and then refine it in later cells. This can be accomplished
795 by::
880 by::
796
881
797 In [1]: set_matplotlib_close(False)
882 In [1]: set_matplotlib_close(False)
798
883
799 To set this in your config files use the following::
884 To set this in your config files use the following::
800
885
801 c.InlineBackend.close_figures = False
886 c.InlineBackend.close_figures = False
802
887
803 Parameters
888 Parameters
804 ----------
889 ----------
805 close : bool
890 close : bool
806 Should all matplotlib figures be automatically closed after each cell is
891 Should all matplotlib figures be automatically closed after each cell is
807 run?
892 run?
808 """
893 """
809 from IPython.kernel.zmq.pylab.config import InlineBackend
894 from IPython.kernel.zmq.pylab.config import InlineBackend
810 cfg = InlineBackend.instance()
895 cfg = InlineBackend.instance()
811 cfg.close_figures = close
896 cfg.close_figures = close
812
897
General Comments 0
You need to be logged in to leave comments. Login now