Warning

This documentation is for an old version of IPython. You can find docs for newer versions here.

Module: core.formatters

Display formatters.

Inheritance diagram:

Inheritance diagram of IPython.core.formatters

Authors:

  • Robert Kern
  • Brian Granger

13 Classes

class IPython.core.formatters.DisplayFormatter(**kwargs)

Bases: IPython.config.configurable.Configurable

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 currently implemented:

  • text/plain
  • text/html
  • text/latex
  • application/json
  • application/javascript
  • application/pdf
  • image/png
  • image/jpeg
  • image/svg+xml
Parameters:

obj : object

The Python object whose format data will be computed.

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.

Returns:

(format_dict, metadata_dict) : tuple of two dicts

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.

format_types

Return the format types (MIME types) of the active formatters.

class IPython.core.formatters.FormatterWarning

Bases: exceptions.UserWarning

Warning class for errors in formatters

class IPython.core.formatters.FormatterABC

Bases: abc._NewBase

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.

class IPython.core.formatters.BaseFormatter(**kwargs)

Bases: IPython.config.configurable.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.

  1. 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.
  2. 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.

for_type(typ, func=None)

Add a format function for a given type.

Parameters:

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.

Returns:

oldfunc : callable

The currently registered callable. If you are registering a new formatter, this will be the previous value (to enable restoring later).

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.

Parameters:

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.

Returns:

oldfunc : callable

The currently registered callable. If you are registering a new formatter, this will be the previous value (to enable restoring later).

lookup(obj)

Look up the formatter for a given instance.

Parameters:

obj : object instance

Returns:

f : callable

The registered formatting callable for the type.

Raises:

KeyError if the type has not been registered.

lookup_by_type(typ)

Look up the registered formatter for a type.

Parameters:

typ : type or ‘__module__.__name__’ string for a type

Returns:

f : callable

The registered formatting callable for the type.

Raises:

KeyError if the type has not been registered.

pop(typ, default=<object object>)

Pop a formatter for the given type.

Parameters:

typ : type or ‘__module__.__name__’ string for a type

default : object

value to be returned if no formatter is registered for typ.

Returns:

obj : object

The last registered object for the type.

Raises:

KeyError if the type is not registered and default is not specified.

class IPython.core.formatters.PlainTextFormatter(**kwargs)

Bases: IPython.core.formatters.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, '])')
class IPython.core.formatters.HTMLFormatter(**kwargs)

Bases: IPython.core.formatters.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.

class IPython.core.formatters.SVGFormatter(**kwargs)

Bases: IPython.core.formatters.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.

class IPython.core.formatters.PNGFormatter(**kwargs)

Bases: IPython.core.formatters.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.

class IPython.core.formatters.JPEGFormatter(**kwargs)

Bases: IPython.core.formatters.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.

class IPython.core.formatters.LatexFormatter(**kwargs)

Bases: IPython.core.formatters.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.

class IPython.core.formatters.JSONFormatter(**kwargs)

Bases: IPython.core.formatters.BaseFormatter

A JSON string formatter.

To define the callables that compute the JSON string 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 valid JSON string.

class IPython.core.formatters.JavascriptFormatter(**kwargs)

Bases: IPython.core.formatters.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.

class IPython.core.formatters.PDFFormatter(**kwargs)

Bases: IPython.core.formatters.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.

2 Functions

IPython.core.formatters.warn_format_error(method)

decorator for warning 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.

The following MIME types are currently implemented:

  • text/plain
  • text/html
  • text/latex
  • application/json
  • application/javascript
  • application/pdf
  • image/png
  • image/jpeg
  • image/svg+xml
Parameters:

obj : object

The Python object whose format data will be computed.

Returns:

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 exclue in the format data dict. If this is set all format types will be computed, except for those included in this argument.