Important
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.
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 Formatters for third-party types)
>>> 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:
16 ClassesBases: Configurable
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.
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
Notes
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.
Return the format types (MIME types) of the active formatters.
Bases: UserWarning
Warning class for errors in formatters
Bases: object
Abstract 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/html
and would return the HTML representation of the object when called.
Bases: Configurable
A base formatter class that is configurable.
This formatter should usually be used as the base class of all formatters. It is a traited Configurable
class 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_printers
and deferred_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()
and for_type_by_name()
methods.
If no function/callable is found to compute the format data, None
is returned and this format type is not used.
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. func
will 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 the func
argument.
If func
is 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
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 dtype
func (callable) –
A callable for computing the format data. func
will 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 the func
argument.
If func
is 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
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. –
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 a formatter for the given type.
Bases: BaseFormatter
The default pretty-printer.
This uses IPython.lib.pretty
to compute the format data of the object. If the object cannot be pretty printed, repr()
is used. See the documentation of IPython.lib.pretty
for 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, '])')
Truncate large collections (lists, dicts, tuples, sets) to this size.
Set to 0 to disable truncation.
Bases: BaseFormatter
An HTML formatter.
To define the callables that compute the HTML representation of your objects, define a _repr_html_()
method or use the for_type()
or for_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.
Bases: BaseFormatter
A Markdown formatter.
To define the callables that compute the Markdown representation of your objects, define a _repr_markdown_()
method or use the for_type()
or for_type_by_name()
methods to register functions that handle this.
The return value of this formatter should be a valid Markdown.
Bases: BaseFormatter
An SVG formatter.
To define the callables that compute the SVG representation of your objects, define a _repr_svg_()
method or use the for_type()
or for_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.
Bases: BaseFormatter
A PNG formatter.
To define the callables that compute the PNG representation of your objects, define a _repr_png_()
method or use the for_type()
or for_type_by_name()
methods to register functions that handle this.
The return value of this formatter should be raw PNG data, not base64 encoded.
Bases: BaseFormatter
A JPEG formatter.
To define the callables that compute the JPEG representation of your objects, define a _repr_jpeg_()
method or use the for_type()
or for_type_by_name()
methods to register functions that handle this.
The return value of this formatter should be raw JPEG data, not base64 encoded.
Bases: BaseFormatter
A LaTeX formatter.
To define the callables that compute the LaTeX representation of your objects, define a _repr_latex_()
method or use the for_type()
or for_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.
Bases: BaseFormatter
A JSON string formatter.
To define the callables that compute the JSONable representation of your objects, define a _repr_json_()
method or use the for_type()
or for_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.
Bases: BaseFormatter
A Javascript formatter.
To define the callables that compute the Javascript representation of your objects, define a _repr_javascript_()
method or use the for_type()
or for_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.
Bases: BaseFormatter
A PDF formatter.
To define the callables that compute the PDF representation of your objects, define a _repr_pdf_()
method or use the for_type()
or for_type_by_name()
methods to register functions that handle this.
The return value of this formatter should be raw PDF data, not base64 encoded.
Bases: BaseFormatter
An 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 the for_type()
or for_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.
Bases: BaseFormatter
A Formatter for arbitrary mime-types.
Unlike other _repr_<mimetype>_
methods, _repr_mimebundle_
should return mime-bundle data, either the mime-keyed data
dictionary 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 the for_type()
or for_type_by_name()
methods to register functions that handle this.
Added in version 6.1.
show traceback on failed format call
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4