IPython Documentation

Table Of Contents

Previous topic

core.display_trap

Next topic

core.displaypub

This Page

core.displayhook

Module: core.displayhook

Inheritance diagram for IPython.core.displayhook:

Displayhook for IPython.

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

Authors:

  • Fernando Perez
  • Brian Granger
  • Robert Kern

DisplayHook

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

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, config=None)
check_for_underscore()

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

classmethod class_config_section()

Get the config class config section

classmethod class_get_help(inst=None)

Get the help string for this class in ReST format.

If inst is given, it’s current trait values will be used in place of class defaults.

classmethod class_get_trait_help(trait, inst=None)

Get the help string for a single trait.

If inst is given, it’s current trait values will be used in place of the class default.

classmethod class_print_help(inst=None)

Get the help string for a single trait and print it.

classmethod class_trait_names(**metadata)

Get a list of all the names of this classes traits.

This method is just like the trait_names() method, but is unbound.

classmethod class_traits(**metadata)

Get a list of all the traits of this class.

This method is just like the traits() method, but is unbound.

The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.

This follows the same algorithm as traits does and does not allow for any simple way of specifying merely that a metadata name exists, but has any value. This is because get_metadata returns None if a metadata key doesn’t exist.

compute_format_data(result)

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.

Parameters :

result : object

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

Returns :

format_data : dict

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.

config

A trait whose value must be an instance of a specified class.

The value can also be an instance of a subclass of the specified class.

created = None
finish_displayhook()

Finish up all displayhook activities.

flush()
log_output(format_dict)

Log the output.

on_trait_change(handler, name=None, remove=False)

Setup a handler to be called when a trait changes.

This is used to setup dynamic notifications of trait changes.

Static handlers can be created by creating methods on a HasTraits subclass with the naming convention ‘_[traitname]_changed’. Thus, to create static handler for the trait ‘a’, create the method _a_changed(self, name, old, new) (fewer arguments can be used, see below).

Parameters :

handler : callable

A callable that is called when a trait changes. Its signature can be handler(), handler(name), handler(name, new) or handler(name, old, new).

name : list, str, None

If None, the handler will apply to all traits. If a list of str, handler will apply to all names in the list. If a str, the handler will apply just to that name.

remove : bool

If False (the default), then install the handler. If True then unintall it.

prompt_count
quiet()

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

shell

A trait whose value must be an instance of a specified class.

The value can also be an instance of a subclass of the specified class.

start_displayhook()

Start the displayhook, initializing resources.

trait_metadata(traitname, key)

Get metadata values for trait by key.

trait_names(**metadata)

Get a list of all the names of this classes traits.

traits(**metadata)

Get a list of all the traits of this class.

The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.

This follows the same algorithm as traits does and does not allow for any simple way of specifying merely that a metadata name exists, but has any value. This is because get_metadata returns None if a metadata key doesn’t exist.

update_config(config)

Fire the traits events when the config is updated.

update_user_ns(result)

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

write_format_data(format_dict)

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.

Parameters :

format_dict : dict

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

write_output_prompt()

Write the output prompt.

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