IPython Documentation

Table Of Contents

Previous topic

Module: core.display_trap

Next topic

Module: core.displaypub

This Page


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

Module: core.displayhook

Displayhook for IPython.

This defines a callable class that IPython uses for sys.displayhook.


  • Fernando Perez
  • Brian Granger
  • Robert Kern

1 Class

class IPython.core.displayhook.DisplayHook(shell=None, cache_size=1000, **kwargs)

Bases: IPython.config.configurable.Configurable

The custom IPython displayhook to replace sys.displayhook.

This class does many things, but the basic idea is that it is a callable that gets called anytime user code returns a value.

__init__(shell=None, cache_size=1000, **kwargs)

Check if the user has set the ‘_’ variable by hand.


Compute format data of the object to be displayed.

The format data is a generalization of the repr() of an object. In the default implementation the format data is a dict of key value pair where the keys are valid MIME types and the values are JSON’able data structure containing the raw data for that MIME type. It is up to frontends to determine pick a MIME to to use and display that data in an appropriate manner.

This method only computes the format data for the object and should NOT actually print or write that to a stream.


result : object

The Python object passed to the display hook, whose format will be computed.


(format_dict, md_dict) : dict

format_dict is a dict whose keys are valid MIME types and values are JSON’able raw data for that MIME type. It is recommended that all return values of this should always include the “text/plain” MIME type representation of the object. md_dict is a dict with the same MIME type keys of metadata associated with each output.


Finish up all displayhook activities.


Log the output.


Should we silence the display hook because of ‘;’?


Start the displayhook, initializing resources.


Update user_ns with various things like _, __, _1, etc.

write_format_data(format_dict, md_dict=None)

Write the format data dict to the frontend.

This default version of this method simply writes the plain text representation of the object to io.stdout. Subclasses should override this method to send the entire format_dict to the frontends.


format_dict : dict

The format dict for the object passed to sys.displayhook.

md_dict : dict (optional)

The metadata dict to be associated with the display data.


Write the output prompt.

The default implementation simply writes the prompt to io.stdout.