| 1 |
.. _integrating: |
| 2 |
|
| 3 |
===================================== |
| 4 |
Integrating your objects with IPython |
| 5 |
===================================== |
| 6 |
|
| 7 |
Tab completion |
| 8 |
============== |
| 9 |
|
| 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 |
| 12 |
built-in :external+python:py:func:`dir` |
| 13 |
|
| 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 |
| 16 |
returns a list of objects which are possible keys in a subscript expression |
| 17 |
``obj[key]``. |
| 18 |
|
| 19 |
.. versionadded:: 5.0 |
| 20 |
Custom key completions |
| 21 |
|
| 22 |
.. _integrating_rich_display: |
| 23 |
|
| 24 |
Rich display |
| 25 |
============ |
| 26 |
|
| 27 |
Custom methods |
| 28 |
-------------- |
| 29 |
|
| 30 |
IPython can display richer representations of objects. |
| 31 |
To do this, you can define ``_ipython_display_()``, or any of a number of |
| 32 |
``_repr_*_()`` methods. |
| 33 |
Note that these are surrounded by single, not double underscores. |
| 34 |
|
| 35 |
|
| 36 |
.. list-table:: Supported ``_repr_*_`` methods |
| 37 |
:widths: 20 15 15 15 |
| 38 |
:header-rows: 1 |
| 39 |
|
| 40 |
* - Format |
| 41 |
- REPL |
| 42 |
- Notebook |
| 43 |
- Qt Console |
| 44 |
* - ``_repr_pretty_`` |
| 45 |
- yes |
| 46 |
- yes |
| 47 |
- yes |
| 48 |
* - ``_repr_svg_`` |
| 49 |
- no |
| 50 |
- yes |
| 51 |
- yes |
| 52 |
* - ``_repr_png_`` |
| 53 |
- no |
| 54 |
- yes |
| 55 |
- yes |
| 56 |
* - ``_repr_jpeg_`` |
| 57 |
- no |
| 58 |
- yes |
| 59 |
- yes |
| 60 |
* - ``_repr_html_`` |
| 61 |
- no |
| 62 |
- yes |
| 63 |
- no |
| 64 |
* - ``_repr_javascript_`` |
| 65 |
- no |
| 66 |
- yes |
| 67 |
- no |
| 68 |
* - ``_repr_markdown_`` |
| 69 |
- no |
| 70 |
- yes |
| 71 |
- no |
| 72 |
* - ``_repr_latex_`` |
| 73 |
- no |
| 74 |
- yes |
| 75 |
- no |
| 76 |
* - ``_repr_mimebundle_`` |
| 77 |
- no |
| 78 |
- ? |
| 79 |
- ? |
| 80 |
|
| 81 |
If the methods don't exist, the standard ``repr()`` is used. |
| 82 |
If a method exists and returns :py:data:`None`, it is treated the same as if it does not exist. |
| 83 |
In general, *all* available formatters will be called when an object is displayed, |
| 84 |
and it is up to the UI to select which to display. |
| 85 |
A given formatter should not generally change its output based on what other formats are available - |
| 86 |
that should be handled at a different level, such as the :class:`~.DisplayFormatter`, or configuration. |
| 87 |
|
| 88 |
``_repr_*_`` methods should *return* data of the expected format and have no side effects. |
| 89 |
For example, ``_repr_html_`` should return HTML as a `str` and ``_repr_png_`` should return PNG data as `bytes`. |
| 90 |
|
| 91 |
If you wish to take control of display via your own side effects, use ``_ipython_display_()``. |
| 92 |
|
| 93 |
For example:: |
| 94 |
|
| 95 |
class Shout(object): |
| 96 |
def __init__(self, text): |
| 97 |
self.text = text |
| 98 |
|
| 99 |
def _repr_html_(self): |
| 100 |
return "<h1>" + self.text + "</h1>" |
| 101 |
|
| 102 |
|
| 103 |
Special methods |
| 104 |
^^^^^^^^^^^^^^^ |
| 105 |
|
| 106 |
Pretty printing |
| 107 |
""""""""""""""" |
| 108 |
|
| 109 |
To customize how your object is pretty-printed, add a ``_repr_pretty_`` method |
| 110 |
to the class. |
| 111 |
The method should accept a pretty printer, and a boolean that indicates whether |
| 112 |
the printer detected a cycle. |
| 113 |
The method should act on the printer to produce your customized pretty output. |
| 114 |
Here is an example:: |
| 115 |
|
| 116 |
class MyObject(object): |
| 117 |
|
| 118 |
def _repr_pretty_(self, p, cycle): |
| 119 |
if cycle: |
| 120 |
p.text('MyObject(...)') |
| 121 |
else: |
| 122 |
p.text('MyObject[...]') |
| 123 |
|
| 124 |
For details on how to use the pretty printer, see :py:mod:`IPython.lib.pretty`. |
| 125 |
|
| 126 |
More powerful methods |
| 127 |
""""""""""""""""""""" |
| 128 |
|
| 129 |
.. class:: MyObject |
| 130 |
|
| 131 |
.. method:: _repr_mimebundle_(include=None, exclude=None) |
| 132 |
|
| 133 |
Should return a dictionary of multiple formats, keyed by mimetype, or a tuple |
| 134 |
of two dictionaries: *data, metadata* (see :ref:`Metadata`). |
| 135 |
If this returns something, other ``_repr_*_`` methods are ignored. |
| 136 |
The method should take keyword arguments ``include`` and ``exclude``, though |
| 137 |
it is not required to respect them. |
| 138 |
|
| 139 |
.. method:: _ipython_display_() |
| 140 |
|
| 141 |
Displays the object as a side effect; the return value is ignored. If this |
| 142 |
is defined, all other display methods are ignored. |
| 143 |
|
| 144 |
|
| 145 |
Metadata |
| 146 |
^^^^^^^^ |
| 147 |
|
| 148 |
We often want to provide frontends with guidance on how to display the data. To |
| 149 |
support this, ``_repr_*_()`` methods (except ``_repr_pretty_``?) can also return a ``(data, metadata)`` |
| 150 |
tuple where ``metadata`` is a dictionary containing arbitrary key-value pairs for |
| 151 |
the frontend to interpret. An example use case is ``_repr_jpeg_()``, which can |
| 152 |
be set to return a jpeg image and a ``{'height': 400, 'width': 600}`` dictionary |
| 153 |
to inform the frontend how to size the image. |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
.. _third_party_formatting: |
| 158 |
|
| 159 |
Formatters for third-party types |
| 160 |
-------------------------------- |
| 161 |
|
| 162 |
The user can also register formatters for types without modifying the class:: |
| 163 |
|
| 164 |
from bar.baz import Foo |
| 165 |
|
| 166 |
def foo_html(obj): |
| 167 |
return '<marquee>Foo object %s</marquee>' % obj.name |
| 168 |
|
| 169 |
html_formatter = get_ipython().display_formatter.formatters['text/html'] |
| 170 |
html_formatter.for_type(Foo, foo_html) |
| 171 |
|
| 172 |
# Or register a type without importing it - this does the same as above: |
| 173 |
html_formatter.for_type_by_name('bar.baz', 'Foo', foo_html) |
| 174 |
|
| 175 |
Custom exception tracebacks |
| 176 |
=========================== |
| 177 |
|
| 178 |
Rarely, you might want to display a custom traceback when reporting an |
| 179 |
exception. To do this, define the custom traceback using |
| 180 |
`_render_traceback_(self)` method which returns a list of strings, one string |
| 181 |
for each line of the traceback. For example, the `ipyparallel |
| 182 |
<https://ipyparallel.readthedocs.io/>`__ a parallel computing framework for |
| 183 |
IPython, does this to display errors from multiple engines. |
| 184 |
|
| 185 |
Please be conservative in using this feature; by replacing the default traceback |
| 186 |
you may hide important information from the user. |