Show More
@@ -21,7 +21,7 b' Authors:' | |||||
21 | # Main functions |
|
21 | # Main functions | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 |
|
23 | |||
24 | def display(obj, include=None, exclude=None): |
|
24 | def display(*objs, **kwargs): | |
25 | """Display a Python object in all frontends. |
|
25 | """Display a Python object in all frontends. | |
26 |
|
26 | |||
27 | By default all representations will be computed and sent to the frontends. |
|
27 | By default all representations will be computed and sent to the frontends. | |
@@ -29,8 +29,8 b' def display(obj, include=None, exclude=None):' | |||||
29 |
|
29 | |||
30 | Parameters |
|
30 | Parameters | |
31 | ---------- |
|
31 | ---------- | |
32 | obj : object |
|
32 | objs : tuple of objects | |
33 | The Python object to display. |
|
33 | The Python objects to display. | |
34 | include : list or tuple, optional |
|
34 | include : list or tuple, optional | |
35 | A list of format type strings (MIME types) to include in the |
|
35 | A list of format type strings (MIME types) to include in the | |
36 | format data dict. If this is set *only* the format types included |
|
36 | format data dict. If this is set *only* the format types included | |
@@ -40,79 +40,83 b' def display(obj, include=None, exclude=None):' | |||||
40 | data dict. If this is set all format types will be computed, |
|
40 | data dict. If this is set all format types will be computed, | |
41 | except for those included in this argument. |
|
41 | except for those included in this argument. | |
42 | """ |
|
42 | """ | |
|
43 | include = kwargs.get('include') | |||
|
44 | exclude = kwargs.get('exclude') | |||
|
45 | ||||
43 | from IPython.core.interactiveshell import InteractiveShell |
|
46 | from IPython.core.interactiveshell import InteractiveShell | |
44 | inst = InteractiveShell.instance() |
|
47 | inst = InteractiveShell.instance() | |
45 | format = inst.display_formatter.format |
|
48 | format = inst.display_formatter.format | |
46 | publish = inst.display_pub.publish |
|
49 | publish = inst.display_pub.publish | |
47 |
|
50 | |||
48 | format_dict = format(obj, include=include, exclude=exclude) |
|
51 | for obj in objs: | |
49 | publish('IPython.core.display.display', format_dict) |
|
52 | format_dict = format(obj, include=include, exclude=exclude) | |
|
53 | publish('IPython.core.display.display', format_dict) | |||
50 |
|
54 | |||
51 |
|
55 | |||
52 | def display_pretty(obj): |
|
56 | def display_pretty(*objs): | |
53 | """Display the pretty (default) representation of an object. |
|
57 | """Display the pretty (default) representation of an object. | |
54 |
|
58 | |||
55 | Parameters |
|
59 | Parameters | |
56 | ---------- |
|
60 | ---------- | |
57 | obj : object |
|
61 | objs : tuple of objects | |
58 | The Python object to display. |
|
62 | The Python objects to display. | |
59 | """ |
|
63 | """ | |
60 | display(obj, include=['text/plain']) |
|
64 | display(*objs, include=['text/plain']) | |
61 |
|
65 | |||
62 |
|
66 | |||
63 | def display_html(obj): |
|
67 | def display_html(*objs): | |
64 | """Display the HTML representation of an object. |
|
68 | """Display the HTML representation of an object. | |
65 |
|
69 | |||
66 | Parameters |
|
70 | Parameters | |
67 | ---------- |
|
71 | ---------- | |
68 | obj : object |
|
72 | objs : tuple of objects | |
69 | The Python object to display. |
|
73 | The Python objects to display. | |
70 | """ |
|
74 | """ | |
71 | display(obj, include=['text/plain','text/html']) |
|
75 | display(*objs, include=['text/plain','text/html']) | |
72 |
|
76 | |||
73 |
|
77 | |||
74 | def display_svg(obj): |
|
78 | def display_svg(*objs): | |
75 | """Display the SVG representation of an object. |
|
79 | """Display the SVG representation of an object. | |
76 |
|
80 | |||
77 | Parameters |
|
81 | Parameters | |
78 | ---------- |
|
82 | ---------- | |
79 | obj : object |
|
83 | objs : tuple of objects | |
80 | The Python object to display. |
|
84 | The Python objects to display. | |
81 | """ |
|
85 | """ | |
82 | display(obj, include=['text/plain','image/svg+xml']) |
|
86 | display(*objs, include=['text/plain','image/svg+xml']) | |
83 |
|
87 | |||
84 |
|
88 | |||
85 | def display_png(obj): |
|
89 | def display_png(*objs): | |
86 | """Display the PNG representation of an object. |
|
90 | """Display the PNG representation of an object. | |
87 |
|
91 | |||
88 | Parameters |
|
92 | Parameters | |
89 | ---------- |
|
93 | ---------- | |
90 | obj : object |
|
94 | objs : tuple of objects | |
91 | The Python object to display. |
|
95 | The Python objects to display. | |
92 | """ |
|
96 | """ | |
93 | display(obj, include=['text/plain','image/png']) |
|
97 | display(*objs, include=['text/plain','image/png']) | |
94 |
|
98 | |||
95 |
|
99 | |||
96 | def display_latex(obj): |
|
100 | def display_latex(*objs): | |
97 | """Display the LaTeX representation of an object. |
|
101 | """Display the LaTeX representation of an object. | |
98 |
|
102 | |||
99 | Parameters |
|
103 | Parameters | |
100 | ---------- |
|
104 | ---------- | |
101 | obj : object |
|
105 | objs : tuple of objects | |
102 | The Python object to display. |
|
106 | The Python objects to display. | |
103 | """ |
|
107 | """ | |
104 | display(obj, include=['text/plain','text/latex']) |
|
108 | display(*objs, include=['text/plain','text/latex']) | |
105 |
|
109 | |||
106 |
|
110 | |||
107 | def display_json(obj): |
|
111 | def display_json(*objs): | |
108 | """Display the JSON representation of an object. |
|
112 | """Display the JSON representation of an object. | |
109 |
|
113 | |||
110 | Parameters |
|
114 | Parameters | |
111 | ---------- |
|
115 | ---------- | |
112 | obj : object |
|
116 | objs : tuple of objects | |
113 | The Python object to display. |
|
117 | The Python objects to display. | |
114 | """ |
|
118 | """ | |
115 | display(obj, include=['text/plain','application/json']) |
|
119 | display(*objs, include=['text/plain','application/json']) | |
116 |
|
120 | |||
117 |
|
121 | |||
118 |
|
122 |
@@ -212,8 +212,7 b' class DisplayHook(Configurable):' | |||||
212 | all return values of this should always include the "text/plain" |
|
212 | all return values of this should always include the "text/plain" | |
213 | MIME type representation of the object. |
|
213 | MIME type representation of the object. | |
214 | """ |
|
214 | """ | |
215 |
|
|
215 | return self.shell.display_formatter.format(result) | |
216 | return format_dict |
|
|||
217 |
|
216 | |||
218 | def write_format_data(self, format_dict): |
|
217 | def write_format_data(self, format_dict): | |
219 | """Write the format data dict to the frontend. |
|
218 | """Write the format data dict to the frontend. |
@@ -179,10 +179,11 b' class BaseFormatter(Configurable):' | |||||
179 | to find print method: :attr:`singleton_printers`, :attr:`type_printers` |
|
179 | to find print method: :attr:`singleton_printers`, :attr:`type_printers` | |
180 | and :attr:`deferred_printers`. |
|
180 | and :attr:`deferred_printers`. | |
181 |
|
181 | |||
182 |
Users should use these dictionarie to register functions that will be |
|
182 | Users should use these dictionaries to register functions that will be | |
183 |
to compute the format data for their objects (if those objects don't |
|
183 | used to compute the format data for their objects (if those objects don't | |
184 |
the special print methods). The easiest way of using these |
|
184 | have the special print methods). The easiest way of using these | |
185 |
is through the :meth:`for_type` and :meth:`for_type_by_name` |
|
185 | dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name` | |
|
186 | methods. | |||
186 |
|
187 | |||
187 | If no function/callable is found to compute the format data, ``None`` is |
|
188 | If no function/callable is found to compute the format data, ``None`` is | |
188 | returned and this format type is not used. |
|
189 | returned and this format type is not used. | |
@@ -243,7 +244,7 b' class BaseFormatter(Configurable):' | |||||
243 | def for_type(self, typ, func): |
|
244 | def for_type(self, typ, func): | |
244 | """Add a format function for a given type. |
|
245 | """Add a format function for a given type. | |
245 |
|
246 | |||
246 |
Parameter |
|
247 | Parameters | |
247 | ----------- |
|
248 | ----------- | |
248 | typ : class |
|
249 | typ : class | |
249 | The class of the object that will be formatted using `func`. |
|
250 | The class of the object that will be formatted using `func`. |
@@ -441,6 +441,31 b' We have provided as magics ``%less`` to page files (aliased to ``%more``),' | |||||
441 | most common commands you'd want to call in your subshell and that would cause |
|
441 | most common commands you'd want to call in your subshell and that would cause | |
442 | problems if invoked via ``!cmd``, but you need to be aware of this limitation. |
|
442 | problems if invoked via ``!cmd``, but you need to be aware of this limitation. | |
443 |
|
443 | |||
|
444 | Display | |||
|
445 | ======= | |||
|
446 | ||||
|
447 | The IPython console can now display objects in a variety of formats, including | |||
|
448 | HTML, PNG and SVG. This is accomplished using the display functions in | |||
|
449 | ``IPython.core.display``:: | |||
|
450 | ||||
|
451 | In [4]: from IPython.core.display import display, display_html | |||
|
452 | ||||
|
453 | In [5]: from IPython.core.display import display_png, display_svg | |||
|
454 | ||||
|
455 | Python objects can simply be passed to these functions and the appropriate | |||
|
456 | representations will be displayed in the console as long as the objects know | |||
|
457 | how to compute those representations. The easiest way of teaching objects how | |||
|
458 | to format themselves in various representations is to define special methods | |||
|
459 | such as: ``__html``, ``__svg__`` and ``__png__``. IPython's display formatters | |||
|
460 | can also be given custom formatter functions for various types:: | |||
|
461 | ||||
|
462 | In [6]: ip = get_ipython() | |||
|
463 | ||||
|
464 | In [7]: html_formatter = ip.display_formatter.formatters['text/html'] | |||
|
465 | ||||
|
466 | In [8]: html_formatter.for_type(Foo, foo_to_html) | |||
|
467 | ||||
|
468 | For further details, see ``IPython.core.formatters``. | |||
444 |
|
469 | |||
445 | Inline matplotlib graphics |
|
470 | Inline matplotlib graphics | |
446 | ========================== |
|
471 | ========================== | |
@@ -448,10 +473,14 b' Inline matplotlib graphics' | |||||
448 | The IPython console is capable of displaying matplotlib figures inline, in SVG |
|
473 | The IPython console is capable of displaying matplotlib figures inline, in SVG | |
449 | format. If started with the ``--pylab inline`` flag, then all figures are |
|
474 | format. If started with the ``--pylab inline`` flag, then all figures are | |
450 | rendered inline automatically. If started with ``--pylab`` or ``--pylab <your |
|
475 | rendered inline automatically. If started with ``--pylab`` or ``--pylab <your | |
451 |
backend>``, then a GUI backend will be used, but |
|
476 | backend>``, then a GUI backend will be used, but IPython's ``display()`` and | |
452 | added to the global and ``plt`` namespaces. You can paste any figure that is |
|
477 | ``getfigs()`` functions can be used to view plots inline:: | |
453 | currently open in a window with this function; type ``pastefig?`` for |
|
478 | ||
454 | additional details.""" |
|
479 | In [9]: display(*getfigs()) # display all figures inline | |
|
480 | ||||
|
481 | In[10]: display(*getfigs(1,2)) # display figures 1 and 2 inline | |||
|
482 | """ | |||
|
483 | ||||
455 |
|
484 | |||
456 | quick_guide = """\ |
|
485 | quick_guide = """\ | |
457 | ? -> Introduction and overview of IPython's features. |
|
486 | ? -> Introduction and overview of IPython's features. |
@@ -36,6 +36,33 b" backends = {'tk': 'TkAgg'," | |||||
36 | # Matplotlib utilities |
|
36 | # Matplotlib utilities | |
37 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
38 |
|
38 | |||
|
39 | ||||
|
40 | def getfigs(*fig_nums): | |||
|
41 | """Get a list of matplotlib figures by figure numbers. | |||
|
42 | ||||
|
43 | If no arguments are given, all available figures are returned. If the | |||
|
44 | argument list contains references to invalid figures, a warning is printed | |||
|
45 | but the function continues pasting further figures. | |||
|
46 | ||||
|
47 | Parameters | |||
|
48 | ---------- | |||
|
49 | figs : tuple | |||
|
50 | A tuple of ints giving the figure numbers of the figures to return. | |||
|
51 | """ | |||
|
52 | from matplotlib._pylab_helpers import Gcf | |||
|
53 | if not fig_nums: | |||
|
54 | fig_managers = Gcf.get_all_fig_managers() | |||
|
55 | return [fm.canvas.figure for fm in fig_managers] | |||
|
56 | else: | |||
|
57 | figs = [] | |||
|
58 | for num in fig_nums: | |||
|
59 | f = Gcf.figs.get(num) | |||
|
60 | if f is None: | |||
|
61 | print('Warning: figure %s not available.' % num) | |||
|
62 | figs.append(f.canvas.figure) | |||
|
63 | return figs | |||
|
64 | ||||
|
65 | ||||
39 | def figsize(sizex, sizey): |
|
66 | def figsize(sizex, sizey): | |
40 | """Set the default figure size to be [sizex, sizey]. |
|
67 | """Set the default figure size to be [sizex, sizey]. | |
41 |
|
68 | |||
@@ -209,6 +236,8 b' def import_pylab(user_ns, backend, import_all=True, shell=None):' | |||||
209 | shell.user_ns_hidden['display'] = display |
|
236 | shell.user_ns_hidden['display'] = display | |
210 | user_ns['display_svg'] = display_svg |
|
237 | user_ns['display_svg'] = display_svg | |
211 | shell.user_ns_hidden['display_svg'] = display_svg |
|
238 | shell.user_ns_hidden['display_svg'] = display_svg | |
|
239 | user_ns['getfigs'] = getfigs | |||
|
240 | shell.user_ns_hidden['getfigs'] = getfigs | |||
212 |
|
241 | |||
213 | if import_all: |
|
242 | if import_all: | |
214 | s = ("from matplotlib.pylab import *\n" |
|
243 | s = ("from matplotlib.pylab import *\n" |
General Comments 0
You need to be logged in to leave comments.
Login now