##// END OF EJS Templates
Some documentations about custom formatters
M Bussonnier -
Show More
@@ -1,6 +1,61
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Display formatters.
2 """Display formatters.
3
3
4 This module defines the base instances in order to implement custom
5 formatters/mimetypes
6 got objects:
7
8 As we want to see internal IPython working we are going to use the following
9 function to diaply objects instead of the normal print or display method:
10
11 >>> ip = get_ipython()
12 >>> ip.display_formatter.format(...)
13 ({'text/plain': 'Ellipsis'}, {})
14
15 This return a tuple with the mimebumdle for the current object, and the
16 associated metadata.
17
18
19 We can now define our own formatter and register it:
20
21
22 >>> from IPython.core.formatters import BaseFormatter, FormatterABC
23
24
25 >>> class LLMFormatter(BaseFormatter):
26 ...
27 ... format_type = 'x-vendor/llm'
28 ... print_method = '_repr_llm_'
29 ... _return_type = (dict, str)
30
31 >>> llm_formatter = LLMFormatter(parent=ip.display_formatter)
32
33 >>> ip.display_formatter.formatters[LLMFormatter.format_type] = llm_formatter
34
35 Now any class that define `_repr_llm_` will return a x-vendor/llm as part of
36 it's display data:
37
38 >>> class A:
39 ...
40 ... def _repr_llm_(self, *kwargs):
41 ... return 'This a A'
42 ...
43
44 >>> ip.display_formatter.format(A())
45 ({'text/plain': '<IPython.core.formatters.A at ...>', 'x-vendor/llm': 'This a A'}, {})
46
47 As usual, you can register methods for third party types (see
48 :ref:`third_party_formatting`)
49
50 >>> def llm_int(obj):
51 ... return 'This is the integer %s, in between %s and %s'%(obj, obj-1, obj+1)
52
53 >>> llm_formatter.for_type(int, llm_int)
54
55 >>> ip.display_formatter.format(42)
56 ({'text/plain': '42', 'x-vendor/llm': 'This is the integer 42, in between 41 and 43'}, {})
57
58
4 Inheritance diagram:
59 Inheritance diagram:
5
60
6 .. inheritance-diagram:: IPython.core.formatters
61 .. inheritance-diagram:: IPython.core.formatters
@@ -39,7 +94,8 class DisplayFormatter(Configurable):
39 You can use this to set a white-list for formats to display.
94 You can use this to set a white-list for formats to display.
40
95
41 Most users will not need to change this value.
96 Most users will not need to change this value.
42 """).tag(config=True)
97 """,
98 ).tag(config=True)
43
99
44 @default('active_types')
100 @default('active_types')
45 def _active_types_default(self):
101 def _active_types_default(self):
@@ -278,12 +334,15 def _get_type(obj):
278 return getattr(obj, '__class__', None) or type(obj)
334 return getattr(obj, '__class__', None) or type(obj)
279
335
280
336
281 _raise_key_error = Sentinel('_raise_key_error', __name__,
337 _raise_key_error = Sentinel(
338 "_raise_key_error",
339 __name__,
282 """
340 """
283 Special value to raise a KeyError
341 Special value to raise a KeyError
284
342
285 Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop`
343 Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop`
286 """)
344 """,
345 )
287
346
288
347
289 class BaseFormatter(Configurable):
348 class BaseFormatter(Configurable):
@@ -604,7 +663,7 class PlainTextFormatter(BaseFormatter):
604 help="""Truncate large collections (lists, dicts, tuples, sets) to this size.
663 help="""Truncate large collections (lists, dicts, tuples, sets) to this size.
605
664
606 Set to 0 to disable truncation.
665 Set to 0 to disable truncation.
607 """
666 """,
608 ).tag(config=True)
667 ).tag(config=True)
609
668
610 # Look for a _repr_pretty_ methods to use for pretty printing.
669 # Look for a _repr_pretty_ methods to use for pretty printing.
@@ -9,7 +9,7 Tab completion
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 :external+python:py:func:`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
@@ -25,7 +25,7 Rich display
25 ============
25 ============
26
26
27 Custom methods
27 Custom methods
28 ----------------------
28 --------------
29
29
30 IPython can display richer representations of objects.
30 IPython can display richer representations of objects.
31 To do this, you can define ``_ipython_display_()``, or any of a number of
31 To do this, you can define ``_ipython_display_()``, or any of a number of
@@ -154,6 +154,8 to inform the frontend how to size the image.
154
154
155
155
156
156
157 .. _third_party_formatting:
158
157 Formatters for third-party types
159 Formatters for third-party types
158 --------------------------------
160 --------------------------------
159
161
General Comments 0
You need to be logged in to leave comments. Login now