##// END OF EJS Templates
Document that _repr_*_() can return metadata
Zi Chong Kao -
Show More
@@ -1,88 +1,95 b''
1 1 .. _integrating:
2 2
3 3 =====================================
4 4 Integrating your objects with IPython
5 5 =====================================
6 6
7 7 Tab completion
8 8 ==============
9 9
10 10 To change the attributes displayed by tab-completing your object, define a
11 11 ``__dir__(self)`` method for it. For more details, see the documentation of the
12 12 built-in `dir() function <http://docs.python.org/library/functions.html#dir>`_.
13 13
14 14 You can also customise key completions for your objects, e.g. pressing tab after
15 15 ``obj["a``. To do so, define a method ``_ipython_key_completions_()``, which
16 16 returns a list of objects which are possible keys in a subscript expression
17 17 ``obj[key]``.
18 18
19 19 .. versionadded:: 5.0
20 20 Custom key completions
21 21
22 22 .. _integrating_rich_display:
23 23
24 24 Rich display
25 25 ============
26 26
27 27 The notebook and the Qt console can display richer representations of objects.
28 To use this, you can define any of a number of ``_repr_*_()`` methods. Note that
28 To use this, you can define any number of ``_repr_*_()`` methods. Note that
29 29 these are surrounded by single, not double underscores.
30 30
31 31 Both the notebook and the Qt console can display ``svg``, ``png`` and ``jpeg``
32 32 representations. The notebook can also display ``html``, ``javascript``,
33 33 ``markdown`` and ``latex``. If the methods don't exist, or return ``None``, it
34 34 falls back to a standard ``repr()``.
35 35
36 36 For example::
37 37
38 38 class Shout(object):
39 39 def __init__(self, text):
40 40 self.text = text
41 41
42 42 def _repr_html_(self):
43 43 return "<h1>" + self.text + "</h1>"
44 44
45 We often want to provide frontends with guidance on how to display the data. To
46 support this, ``_repr_*_()`` methods can also return a data, metadata tuple where
47 metadata is a dictionary containing arbitrary key, value pairs for the frontend
48 to interpret. An example use case is ``_repr_jpeg()``, which can be set to
49 return a jpeg image and a ``{'height': 400, 'width': 600}`` dictionary to inform
50 the frontend how to size the image.
51
45 52 There are also two more powerful display methods:
46 53
47 54 .. class:: MyObject
48 55
49 56 .. method:: _repr_mimebundle_(include=None, exclude=None)
50 57
51 58 Should return a dictionary of multiple formats, keyed by mimetype, or a tuple
52 59 of two dictionaries: *data, metadata*. If this returns something, other
53 60 ``_repr_*_`` methods are ignored. The method should take keyword arguments
54 61 ``include`` and ``exclude``, though it is not required to respect them.
55 62
56 63 .. method:: _ipython_display_()
57 64
58 65 Displays the object as a side effect; the return value is ignored. If this
59 66 is defined, all other display methods are ignored.
60 67
61 68 Formatters for third-party types
62 69 --------------------------------
63 70
64 71 The user can also register formatters for types without modifying the class::
65 72
66 73 from bar import Foo
67 74
68 75 def foo_html(obj):
69 76 return '<marquee>Foo object %s</marquee>' % obj.name
70 77
71 78 html_formatter = get_ipython().display_formatter.formatters['text/html']
72 79 html_formatter.for_type(Foo, foo_html)
73 80
74 81 # Or register a type without importing it - this does the same as above:
75 82 html_formatter.for_type_by_name('bar.Foo', foo_html)
76 83
77 84 Custom exception tracebacks
78 85 ===========================
79 86
80 87 Rarely, you might want to display a custom traceback when reporting an
81 88 exception. To do this, define the custom traceback using
82 89 `_render_traceback_(self)` method which returns a list of strings, one string
83 90 for each line of the traceback. For example, the `ipyparallel
84 91 <http://ipyparallel.readthedocs.io/>`__ a parallel computing framework for
85 92 IPython, does this to display errors from multiple engines.
86 93
87 94 Please be conservative in using this feature; by replacing the default traceback
88 95 you may hide important information from the user.
General Comments 0
You need to be logged in to leave comments. Login now