Важно
This documentation covers IPython versions 6.0 and higher. Beginning with version 6.0, IPython stopped supporting compatibility with Python versions lower than 3.3 including all versions of Python 2.7.
If you are looking for an IPython version compatible with Python 2.7, please use the IPython 5.x LTS release and refer to its documentation (LTS is the long term support release).
Module: core.formatters¶
Display formatters.
Inheritance diagram:

16 Classes¶
- class IPython.core.formatters.DisplayFormatter(**kwargs: Any)¶
Базовые классы:
Configurable- active_types¶
List of currently active mime-types to display. You can use this to set a white-list for formats to display.
Most users will not need to change this value.
- format(obj, include=None, exclude=None)¶
Return a format data dict for an object.
By default all format types will be computed.
The following MIME types are usually implemented:
text/plain
text/html
text/markdown
text/latex
application/json
application/javascript
application/pdf
image/png
image/jpeg
image/svg+xml
- Параметры
obj (object) – The Python object whose format data will be computed.
include (list, tuple or set; optional) – A list of format type strings (MIME types) to include in the format data dict. If this is set only the format types included in this list will be computed.
exclude (list, tuple or set; optional) – A list of format type string (MIME types) to exclude in the format data dict. If this is set all format types will be computed, except for those included in this argument. Mimetypes present in exclude will take precedence over the ones in include
- Результат
(format_dict, metadata_dict) – format_dict is a dictionary of key/value pairs, one of each format that was generated for the object. The keys are the format types, which will usually be MIME type strings and the values and JSON’able data structure containing the raw data for the representation in that format.
metadata_dict is a dictionary of metadata about each mime-type output. Its keys will be a strict subset of the keys in format_dict.
- Тип результата
tuple of two dicts
Заметки
If an object implement
_repr_mimebundle_as well as various_repr_*_, the data returned by_repr_mimebundle_will take precedence and the corresponding_repr_*_for this mimetype will not be called.
- property format_types¶
Return the format types (MIME types) of the active formatters.
- class IPython.core.formatters.FormatterWarning¶
Базовые классы:
UserWarningWarning class for errors in formatters
- class IPython.core.formatters.FormatterABC¶
Базовые классы:
objectAbstract base class for Formatters.
A formatter is a callable class that is responsible for computing the raw format data for a particular format type (MIME type). For example, an HTML formatter would have a format type of
text/htmland would return the HTML representation of the object when called.
- class IPython.core.formatters.BaseFormatter(**kwargs: Any)¶
Базовые классы:
ConfigurableA base formatter class that is configurable.
This formatter should usually be used as the base class of all formatters. It is a traited
Configurableclass and includes an extensible API for users to determine how their objects are formatted. The following logic is used to find a function to format an given object.The object is introspected to see if it has a method with the name
print_method. If is does, that object is passed to that method for formatting.If no print method is found, three internal dictionaries are consulted to find print method:
singleton_printers,type_printersanddeferred_printers.
Users should use these dictionaries to register functions that will be used to compute the format data for their objects (if those objects don’t have the special print methods). The easiest way of using these dictionaries is through the
for_type()andfor_type_by_name()methods.If no function/callable is found to compute the format data,
Noneis returned and this format type is not used.- for_type(typ, func=None)¶
Add a format function for a given type.
- Параметры
typ (type or '__module__.__name__' string for a type) – The class of the object that will be formatted using
func.func (callable) –
A callable for computing the format data.
funcwill be called with the object to be formatted, and will return the raw data in this formatter’s format. Subclasses may use a different call signature for thefuncargument.If
funcis None or not specified, there will be no change, only returning the current value.
- Результат
oldfunc – The currently registered callable. If you are registering a new formatter, this will be the previous value (to enable restoring later).
- Тип результата
callable
- for_type_by_name(type_module, type_name, func=None)¶
Add a format function for a type specified by the full dotted module and name of the type, rather than the type of the object.
- Параметры
type_module (str) – The full dotted name of the module the type is defined in, like
numpy.type_name (str) – The name of the type (the class name), like
dtypefunc (callable) –
A callable for computing the format data.
funcwill be called with the object to be formatted, and will return the raw data in this formatter’s format. Subclasses may use a different call signature for thefuncargument.If
funcis None or unspecified, there will be no change, only returning the current value.
- Результат
oldfunc – The currently registered callable. If you are registering a new formatter, this will be the previous value (to enable restoring later).
- Тип результата
callable
- lookup(obj)¶
Look up the formatter for a given instance.
- Параметры
obj (object instance) –
- Результат
f – The registered formatting callable for the type.
- Тип результата
callable
- Исключение
KeyError if the type has not been registered. –
- lookup_by_type(typ)¶
Look up the registered formatter for a type.
- Параметры
typ (type or '__module__.__name__' string for a type) –
- Результат
f – The registered formatting callable for the type.
- Тип результата
callable
- Исключение
KeyError if the type has not been registered. –
- pop(typ, default=IPython.core.formatters._raise_key_error)¶
Pop a formatter for the given type.
- Параметры
- Результат
obj – The last registered object for the type.
- Тип результата
- Исключение
KeyError if the type is not registered and default is not specified. –
- class IPython.core.formatters.PlainTextFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterThe default pretty-printer.
This uses
IPython.lib.prettyto compute the format data of the object. If the object cannot be pretty printed,repr()is used. See the documentation ofIPython.lib.prettyfor details on how to write pretty printers. Here is a simple example:def dtype_pprinter(obj, p, cycle): if cycle: return p.text('dtype(...)') if hasattr(obj, 'fields'): if obj.fields is None: p.text(repr(obj)) else: p.begin_group(7, 'dtype([') for i, field in enumerate(obj.descr): if i > 0: p.text(',') p.breakable() p.pretty(field) p.end_group(7, '])')
- max_seq_length¶
Truncate large collections (lists, dicts, tuples, sets) to this size.
Set to 0 to disable truncation.
- class IPython.core.formatters.HTMLFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterAn HTML formatter.
To define the callables that compute the HTML representation of your objects, define a
_repr_html_()method or use thefor_type()orfor_type_by_name()methods to register functions that handle this.The return value of this formatter should be a valid HTML snippet that could be injected into an existing DOM. It should not include the
`<html>or`<body>tags.
- class IPython.core.formatters.MarkdownFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterA Markdown formatter.
To define the callables that compute the Markdown representation of your objects, define a
_repr_markdown_()method or use thefor_type()orfor_type_by_name()methods to register functions that handle this.The return value of this formatter should be a valid Markdown.
- class IPython.core.formatters.SVGFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterAn SVG formatter.
To define the callables that compute the SVG representation of your objects, define a
_repr_svg_()method or use thefor_type()orfor_type_by_name()methods to register functions that handle this.The return value of this formatter should be valid SVG enclosed in
`<svg>`tags, that could be injected into an existing DOM. It should not include the`<html>or`<body>tags.
- class IPython.core.formatters.PNGFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterA PNG formatter.
To define the callables that compute the PNG representation of your objects, define a
_repr_png_()method or use thefor_type()orfor_type_by_name()methods to register functions that handle this.The return value of this formatter should be raw PNG data, not base64 encoded.
- class IPython.core.formatters.JPEGFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterA JPEG formatter.
To define the callables that compute the JPEG representation of your objects, define a
_repr_jpeg_()method or use thefor_type()orfor_type_by_name()methods to register functions that handle this.The return value of this formatter should be raw JPEG data, not base64 encoded.
- class IPython.core.formatters.LatexFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterA LaTeX formatter.
To define the callables that compute the LaTeX representation of your objects, define a
_repr_latex_()method or use thefor_type()orfor_type_by_name()methods to register functions that handle this.The return value of this formatter should be a valid LaTeX equation, enclosed in either
`$`,`$$`or another LaTeX equation environment.
- class IPython.core.formatters.JSONFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterA JSON string formatter.
To define the callables that compute the JSONable representation of your objects, define a
_repr_json_()method or use thefor_type()orfor_type_by_name()methods to register functions that handle this.The return value of this formatter should be a JSONable list or dict. JSON scalars (None, number, string) are not allowed, only dict or list containers.
- class IPython.core.formatters.JavascriptFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterA Javascript formatter.
To define the callables that compute the Javascript representation of your objects, define a
_repr_javascript_()method or use thefor_type()orfor_type_by_name()methods to register functions that handle this.The return value of this formatter should be valid Javascript code and should not be enclosed in
`<script>`tags.
- class IPython.core.formatters.PDFFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterA PDF formatter.
To define the callables that compute the PDF representation of your objects, define a
_repr_pdf_()method or use thefor_type()orfor_type_by_name()methods to register functions that handle this.The return value of this formatter should be raw PDF data, not base64 encoded.
- class IPython.core.formatters.IPythonDisplayFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterAn escape-hatch Formatter for objects that know how to display themselves.
To define the callables that compute the representation of your objects, define a
_ipython_display_()method or use thefor_type()orfor_type_by_name()methods to register functions that handle this. Unlike mime-type displays, this method should not return anything, instead calling any appropriate display methods itself.This display formatter has highest priority. If it fires, no other display formatter will be called.
Prior to IPython 6.1,
_ipython_display_was the only way to display custom mime-types without registering a new Formatter.IPython 6.1 introduces
_repr_mimebundle_for displaying custom mime-types, so_ipython_display_should only be used for objects that require unusual display patterns, such as multiple display calls.
- class IPython.core.formatters.MimeBundleFormatter(**kwargs: Any)¶
Базовые классы:
BaseFormatterA Formatter for arbitrary mime-types.
Unlike other
_repr_<mimetype>_methods,_repr_mimebundle_should return mime-bundle data, either the mime-keyeddatadictionary or the tuple(data, metadata). Any mime-type is valid.To define the callables that compute the mime-bundle representation of your objects, define a
_repr_mimebundle_()method or use thefor_type()orfor_type_by_name()methods to register functions that handle this.Добавлено в версии 6.1.
2 Functions¶
- IPython.core.formatters.catch_format_error(method, self)¶
show traceback on failed format call
- IPython.core.formatters.format_display_data(obj, include=None, exclude=None)¶
Return a format data dict for an object.
By default all format types will be computed.
- Параметры
obj (object) – The Python object whose format data will be computed.
- Результат
format_dict (dict) – A dictionary of key/value pairs, one or each format that was generated for the object. The keys are the format types, which will usually be MIME type strings and the values and JSON’able data structure containing the raw data for the representation in that format.
include (list or tuple, optional) – A list of format type strings (MIME types) to include in the format data dict. If this is set only the format types included in this list will be computed.
exclude (list or tuple, optional) – A list of format type string (MIME types) to exclude in the format data dict. If this is set all format types will be computed, except for those included in this argument.