From 43994e7b6fa1afd849fd36edc0cae45083f4857b 2017-11-13 16:00:11 From: Matthias Bussonnier Date: 2017-11-13 16:00:11 Subject: [PATCH] Merge pull request #10890 from takluyver/doc-more-display-fmt Extend docs on customising object display --- diff --git a/IPython/core/display.py b/IPython/core/display.py index fa3dea0..1b98dbb 100644 --- a/IPython/core/display.py +++ b/IPython/core/display.py @@ -252,8 +252,9 @@ def display(*objs, include=None, exclude=None, metadata=None, transient=None, di display in IPython by following the above approach. But in practice, you often need to work with existing classes that you can't easily modify. - You can refer to the documentation on IPython display formatters in order to - register custom formatters for already existing types. + You can refer to the documentation on integrating with the display system in + order to register custom formatters for already existing types + (:ref:`integrating_rich_display`). .. versionadded:: 5.4 display available without import .. versionadded:: 6.1 display available without import diff --git a/docs/source/config/integrating.rst b/docs/source/config/integrating.rst index d1ae9d3..368761e 100644 --- a/docs/source/config/integrating.rst +++ b/docs/source/config/integrating.rst @@ -19,6 +19,8 @@ returns a list of objects which are possible keys in a subscript expression .. versionadded:: 5.0 Custom key completions +.. _integrating_rich_display: + Rich display ============ @@ -28,8 +30,8 @@ these are surrounded by single, not double underscores. Both the notebook and the Qt console can display ``svg``, ``png`` and ``jpeg`` representations. The notebook can also display ``html``, ``javascript``, -and ``latex``. If the methods don't exist, or return ``None``, it falls -back to a standard ``repr()``. +``markdown`` and ``latex``. If the methods don't exist, or return ``None``, it +falls back to a standard ``repr()``. For example:: @@ -40,6 +42,38 @@ For example:: def _repr_html_(self): return "

" + self.text + "

" +There are also two more powerful display methods: + +.. class:: MyObject + + .. method:: _repr_mimebundle_(include=None, exclude=None) + + Should return a dictionary of multiple formats, keyed by mimetype, or a tuple + of two dictionaries: *data, metadata*. If this returns something, other + ``_repr_*_`` methods are ignored. The method should take keyword arguments + ``include`` and ``exclude``, though it is not required to respect them. + + .. method:: _ipython_display_() + + Displays the object as a side effect; the return value is ignored. If this + is defined, all other display methods are ignored. + +Formatters for third-party types +-------------------------------- + +The user can also register formatters for types without modifying the class:: + + from bar import Foo + + def foo_html(obj): + return 'Foo object %s' % obj.name + + html_formatter = get_ipython().display_formatter.formatters['text/html'] + html_formatter.for_type(Foo, foo_html) + + # Or register a type without importing it - this does the same as above: + html_formatter.for_type_by_name('bar.Foo', foo_html) + Custom exception tracebacks ===========================