bundles / IPython 9.5.0 / IPython / core / formatters
module
IPython.core.formatters
source: /IPython/core/formatters.py :0
Members
-
_get_type -
_mod_name_key -
_safe_repr -
BaseFormatter -
DisplayFormatter -
format_display_data -
FormatterABC -
FormatterWarning -
HTMLFormatter -
IPythonDisplayFormatter -
JavascriptFormatter -
JPEGFormatter -
JSONFormatter -
LatexFormatter -
MarkdownFormatter -
MimeBundleFormatter -
PDFFormatter -
PlainTextFormatter -
PNGFormatter -
SVGFormatter
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