You are viewing an older version (9.10.0). Go to latest (9.13.0)
{ } Raw JSON

bundles / IPython 9.10.0 / IPython / core / formatters

module

IPython.core.formatters

source: /IPython/core/formatters.py :0

Members

Summary

Display formatters.

Extended Summary

This module defines the base instances in order to implement custom formatters/mimetypes got objects:

As we want to see internal IPython working we are going to use the following function to diaply objects instead of the normal print or display method:

>>> ip = get_ipython()
>>> ip.display_formatter.format(...)
({'text/plain': 'Ellipsis'}, {})

This return a tuple with the mimebumdle for the current object, and the associated metadata.

We can now define our own formatter and register it:

>>> from IPython.core.formatters import BaseFormatter, FormatterABC
>>> class LLMFormatter(BaseFormatter):
...
...     format_type = 'x-vendor/llm'
...     print_method = '_repr_llm_'
...     _return_type = (dict, str)
>>> llm_formatter = LLMFormatter(parent=ip.display_formatter)
>>> ip.display_formatter.formatters[LLMFormatter.format_type] = llm_formatter

Now any class that define _repr_llm_ will return a x-vendor/llm as part of it's display data:

>>> class A:
...
...     def _repr_llm_(self, *kwargs):
...         return 'This a A'
...
>>> ip.display_formatter.format(A())
({'text/plain': '<IPython.core.formatters.A at ...>', 'x-vendor/llm': 'This a A'}, {})

As usual, you can register methods for third party types (see third_party_formatting)

>>> def llm_int(obj):
...      return 'This is the integer %s, in between %s and %s'%(obj, obj-1, obj+1)
>>> llm_formatter.for_type(int, llm_int)
>>> ip.display_formatter.format(42)
({'text/plain': '42', 'x-vendor/llm': 'This is the integer 42, in between 41 and 43'}, {})

Inheritance diagram:

Additional content

Display formatters.

This module defines the base instances in order to implement custom formatters/mimetypes got objects:

As we want to see internal IPython working we are going to use the following function to diaply objects instead of the normal print or display method:

>>> ip = get_ipython()
>>> ip.display_formatter.format(...)
({'text/plain': 'Ellipsis'}, {})

This return a tuple with the mimebumdle for the current object, and the associated metadata.

We can now define our own formatter and register it:

>>> from IPython.core.formatters import BaseFormatter, FormatterABC
>>> class LLMFormatter(BaseFormatter):
...
...     format_type = 'x-vendor/llm'
...     print_method = '_repr_llm_'
...     _return_type = (dict, str)
>>> llm_formatter = LLMFormatter(parent=ip.display_formatter)
>>> ip.display_formatter.formatters[LLMFormatter.format_type] = llm_formatter

Now any class that define _repr_llm_ will return a x-vendor/llm as part of it's display data:

>>> class A:
...
...     def _repr_llm_(self, *kwargs):
...         return 'This a A'
...
>>> ip.display_formatter.format(A())
({'text/plain': '<IPython.core.formatters.A at ...>', 'x-vendor/llm': 'This a A'}, {})

As usual, you can register methods for third party types (see third_party_formatting)

>>> def llm_int(obj):
...      return 'This is the integer %s, in between %s and %s'%(obj, obj-1, obj+1)
>>> llm_formatter.for_type(int, llm_int)
>>> ip.display_formatter.format(42)
({'text/plain': '42', 'x-vendor/llm': 'This is the integer 42, in between 41 and 43'}, {})

Inheritance diagram:

Aliases

  • IPython.core.formatters