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