display — IPython 9.2.0 documentation (original) (raw)

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: display

Public API for display tools in IPython.

23 Classes

class IPython.display.Audio(data=None, filename=None, url=None, embed=None, rate=None, autoplay=False, normalize=True, *, element_id=None)

Bases: DisplayObject

Create an audio object.

When this object is returned by an input cell or passed to the display function, it will result in Audio controls being displayed in the frontend (only works in the notebook).

Parameters:

Examples

import pytest np = pytest.importorskip("numpy")

Generate a sound

import numpy as np framerate = 44100 t = np.linspace(0,5,framerate5) data = np.sin(2np.pi220t) + np.sin(2np.pi224*t) Audio(data, rate=framerate) <IPython.lib.display.Audio object>

Can also do stereo or more channels

dataleft = np.sin(2np.pi220t) dataright = np.sin(2np.pi224t) Audio([dataleft, dataright], rate=framerate) <IPython.lib.display.Audio object>

From URL:

Audio("http://www.nch.com.au/acm/8k16bitpcm.wav") Audio(url="http://www.w3schools.com/html/horse.ogg")

From a File:

Audio('IPython/lib/tests/test.wav') Audio(filename='IPython/lib/tests/test.wav')

From Bytes:

Audio(b'RAW_WAV_DATA..') Audio(data=b'RAW_WAV_DATA..')

See also

ipywidgets.Audio

Audio widget with more more flexibility and options.

__init__(data=None, filename=None, url=None, embed=None, rate=None, autoplay=False, normalize=True, *, element_id=None)

Create a display object given raw data.

When this object is returned by an expression or passed to the display function, it will result in the data being displayed in the frontend. The MIME type of the data should match the subclasses used, so the Png subclass should be used for ‘image/png’ data. If the data is a URL, the data will first be downloaded and then displayed.

Parameters:

reload()

Reload the raw data from file or URL.

class IPython.display.Code(data=None, url=None, filename=None, language=None)

Bases: TextDisplayObject

Display syntax-highlighted source code.

This uses Pygments to highlight the code for HTML and Latex output.

Parameters:

__init__(data=None, url=None, filename=None, language=None)

Create a display object given raw data.

When this object is returned by an expression or passed to the display function, it will result in the data being displayed in the frontend. The MIME type of the data should match the subclasses used, so the Png subclass should be used for ‘image/png’ data. If the data is a URL, the data will first be downloaded and then displayed.

Parameters:

class IPython.display.DisplayHandle(display_id=None)

Bases: object

A handle on an updatable display

Call .update(obj) to display a new object.

Call .display(obj) to add a new instance of this display, and update existing instances.

__init__(display_id=None)

display(obj, **kwargs)

Make a new display with my id, updating existing instances.

Parameters:

update(obj, **kwargs)

Update existing displays with my id

Parameters:

class IPython.display.DisplayObject(data=None, url=None, filename=None, metadata=None)

Bases: object

An object that wraps data to be displayed.

__init__(data=None, url=None, filename=None, metadata=None)

Create a display object given raw data.

When this object is returned by an expression or passed to the display function, it will result in the data being displayed in the frontend. The MIME type of the data should match the subclasses used, so the Png subclass should be used for ‘image/png’ data. If the data is a URL, the data will first be downloaded and then displayed.

Parameters:

reload()

Reload the raw data from file or URL.

class IPython.display.FileLink(path, url_prefix='', result_html_prefix='', result_html_suffix='
'
)

Bases: object

Class for embedding a local file link in an IPython session, based on path

e.g. to embed a link that was generated in the IPython notebook as my/data.txt

you would do:

local_file = FileLink("my/data.txt") display(local_file)

or in the HTML notebook, just:

__init__(path, url_prefix='', result_html_prefix='', result_html_suffix='
'
)

Parameters:

class IPython.display.FileLinks(path, url_prefix='', included_suffixes=None, result_html_prefix='', result_html_suffix='
'
, notebook_display_formatter=None, terminal_display_formatter=None, recursive=True)

Bases: FileLink

Class for embedding local file links in an IPython session, based on path

e.g. to embed links to files that were generated in the IPython notebook under my/data, you would do:

local_files = FileLinks("my/data") display(local_files)

or in the HTML notebook, just:

__init__(path, url_prefix='', included_suffixes=None, result_html_prefix='', result_html_suffix='
'
, notebook_display_formatter=None, terminal_display_formatter=None, recursive=True)

See FileLink for the path, url_prefix,result_html_prefix and result_html_suffix parameters.

included_suffixeslist

Filename suffixes to include when formatting output [default: include all files]

notebook_display_formatterfunction

Used to format links for display in the notebook. See discussion of formatter functions below.

terminal_display_formatterfunction

Used to format links for display in the terminal. See discussion of formatter functions below.

Formatter functions must be of the form:

f(dirname, fnames, included_suffixes)

dirnamestr

The name of a directory

fnameslist

The files in that directory

included_suffixeslist

The file suffixes that should be included in the output (passing None meansto include all suffixes in the output in the built-in formatters)

recursiveboolean

Whether to recurse into subdirectories. Default is True.

The function should return a list of lines that will be printed in the notebook (if passing notebook_display_formatter) or the terminal (if passing terminal_display_formatter). This function is iterated over for each directory in self.path. Default formatters are in place, can be passed here to support alternative formatting.

class IPython.display.GeoJSON(*args, **kwargs)

Bases: JSON

GeoJSON expects JSON-able dict

not an already-serialized JSON string.

Scalar types (None, number, string) are not allowed, only dict containers.

__init__(*args, **kwargs)

Create a GeoJSON display object given raw data.

Parameters:

Examples

The following will display an interactive map of Mars with a point of interest on frontend that do support GeoJSON display.

from IPython.display import GeoJSON

GeoJSON(data={ ... "type": "Feature", ... "geometry": { ... "type": "Point", ... "coordinates": [-81.327, 296.038] ... } ... }, ... url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png", ... layer_options={ ... "basemap_id": "celestia_mars-shaded-16k_global", ... "attribution" : "Celestia/praesepe", ... "minZoom" : 0, ... "maxZoom" : 18, ... }) <IPython.core.display.GeoJSON object>

In the terminal IPython, you will only see the text representation of the GeoJSON object.

class IPython.display.HTML(data=None, url=None, filename=None, metadata=None)

Bases: TextDisplayObject

__init__(data=None, url=None, filename=None, metadata=None)

Create a display object given raw data.

When this object is returned by an expression or passed to the display function, it will result in the data being displayed in the frontend. The MIME type of the data should match the subclasses used, so the Png subclass should be used for ‘image/png’ data. If the data is a URL, the data will first be downloaded and then displayed.

Parameters:

class IPython.display.IFrame(src, width, height, extras: Iterable[str] | None = None, **kwargs)

Bases: object

Generic class to embed an iframe in an IPython notebook

__init__(src, width, height, extras: Iterable[str] | None = None, **kwargs)

class IPython.display.Image(data=None, url=None, filename=None, format=None, embed=None, width=None, height=None, retina=False, unconfined=False, metadata=None, alt=None)

Bases: DisplayObject

__init__(data=None, url=None, filename=None, format=None, embed=None, width=None, height=None, retina=False, unconfined=False, metadata=None, alt=None)

Create a PNG/JPEG/GIF/WEBP image object given raw data.

When this object is returned by an input cell or passed to the display function, it will result in the image being displayed in the frontend.

Parameters:

Examples

embedded image data, works in qtconsole and notebook when passed positionally, the first arg can be any of raw image data, a URL, or a filename from which to load image data. The result is always embedding image data for inline images.

Image('https://www.google.fr/images/srpr/logo3w.png') <IPython.core.display.Image object>

Image('/path/to/image.jpg') <IPython.core.display.Image object>

Image(b'RAW_PNG_DATA...') <IPython.core.display.Image object>

Specifying Image(url=…) does not embed the image data, it only generates <img> tag with a link to the source. This will not work in the qtconsole or offline.

Image(url='https://www.google.fr/images/srpr/logo3w.png') <IPython.core.display.Image object>

reload()

Reload the raw data from file or URL.

class IPython.display.JSON(data=None, url=None, filename=None, expanded=False, metadata=None, root='root', **kwargs)

Bases: DisplayObject

JSON expects a JSON-able dict or list

not an already-serialized JSON string.

Scalar types (None, number, string) are not allowed, only dict or list containers.

__init__(data=None, url=None, filename=None, expanded=False, metadata=None, root='root', **kwargs)

Create a JSON display object given raw data.

Parameters:

class IPython.display.Javascript(data=None, url=None, filename=None, lib=None, css=None)

Bases: TextDisplayObject

__init__(data=None, url=None, filename=None, lib=None, css=None)

Create a Javascript display object given raw data.

When this object is returned by an expression or passed to the display function, it will result in the data being displayed in the frontend. If the data is a URL, the data will first be downloaded and then displayed.

In the Notebook, the containing element will be available as element, and jQuery will be available. Content appended to element will be visible in the output area.

Parameters:

class IPython.display.Latex(data=None, url=None, filename=None, metadata=None)

Bases: TextDisplayObject

class IPython.display.Markdown(data=None, url=None, filename=None, metadata=None)

Bases: TextDisplayObject

class IPython.display.Math(data=None, url=None, filename=None, metadata=None)

Bases: TextDisplayObject

class IPython.display.Pretty(data=None, url=None, filename=None, metadata=None)

Bases: TextDisplayObject

class IPython.display.ProgressBar(total)

Bases: DisplayObject

Progressbar supports displaying a progressbar like element

__init__(total)

Creates a new progressbar

Parameters:

total (int) – maximum size of the progressbar

class IPython.display.SVG(data=None, url=None, filename=None, metadata=None)

Bases: DisplayObject

Embed an SVG into the display.

Note if you just want to view a svg image via a URL use :class:Image with a url=URL keyword argument.

class IPython.display.ScribdDocument(id, width=400, height=300, **kwargs)

Bases: IFrame

Class for embedding a Scribd document in an IPython session

Use the start_page params to specify a starting point in the document Use the view_mode params to specify display type one off scroll | slideshow | book

e.g to Display Wes’ foundational paper about PANDAS in book mode from page 3

ScribdDocument(71048089, width=800, height=400, start_page=3, view_mode=”book”)

__init__(id, width=400, height=300, **kwargs)

class IPython.display.TextDisplayObject(data=None, url=None, filename=None, metadata=None)

Bases: DisplayObject

Create a text display object given raw data.

Parameters:

class IPython.display.Video(data=None, url=None, filename=None, embed=False, mimetype=None, width=None, height=None, html_attributes='controls')

Bases: DisplayObject

__init__(data=None, url=None, filename=None, embed=False, mimetype=None, width=None, height=None, html_attributes='controls')

Create a video object given raw data or an URL.

When this object is returned by an input cell or passed to the display function, it will result in the video being displayed in the frontend.

Parameters:

Examples

Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4') Video('path/to/video.mp4') Video('path/to/video.mp4', embed=True) Video('path/to/video.mp4', embed=True, html_attributes="controls muted autoplay") Video(b'raw-videodata', embed=True)

reload()

Reload the raw data from file or URL.

class IPython.display.VimeoVideo(id, width=400, height=300, **kwargs)

Bases: IFrame

Class for embedding a Vimeo video in an IPython session, based on its video id.

__init__(id, width=400, height=300, **kwargs)

class IPython.display.YouTubeVideo(id, width=400, height=300, allow_autoplay=False, **kwargs)

Bases: IFrame

Class for embedding a YouTube Video in an IPython session, based on its video id.

e.g. to embed the video from https://www.youtube.com/watch?v=foo , you would do:

vid = YouTubeVideo("foo") display(vid)

To start from 30 seconds:

vid = YouTubeVideo("abc", start=30) display(vid)

To calculate seconds from time as hours, minutes, seconds usedatetime.timedelta:

start=int(timedelta(hours=1, minutes=46, seconds=40).total_seconds())

Other parameters can be provided as documented athttps://developers.google.com/youtube/player_parameters#Parameters

When converting the notebook using nbconvert, a jpeg representation of the video will be inserted in the document.

__init__(id, width=400, height=300, allow_autoplay=False, **kwargs)

14 Functions

IPython.display.clear_output(wait=False)

Clear the output of the current cell receiving output.

Parameters:

wait (bool [ default: false ]) – Wait to clear the output until new output is available to replace it.

IPython.display.display(*objs, include=None, exclude=None, metadata=None, transient=None, display_id=None, raw=False, clear=False, **kwargs)

Display a Python object in all frontends.

By default all representations will be computed and sent to the frontends. Frontends can decide which representation is used and how.

In terminal IPython this will be similar to using print(), for use in richer frontends see Jupyter notebook examples with rich display logic.

Parameters:

Returns:

handle – Returns a handle on updatable displays for use with update_display(), if display_id is given. Returns None if no display_id is given (default).

Return type:

DisplayHandle

Examples

class Json(object): ... def init(self, json): ... self.json = json ... def repr_pretty(self, pp, cycle): ... import json ... pp.text(json.dumps(self.json, indent=2)) ... def repr(self): ... return str(self.json) ...

d = Json({1:2, 3: {4:5}})

print(d) {1: 2, 3: {4: 5}}

display(d) { "1": 2, "3": { "4": 5 } }

def int_formatter(integer, pp, cycle): ... pp.text('I'*integer)

plain = get_ipython().display_formatter.formatters['text/plain'] plain.for_type(int, int_formatter) <function _repr_pprint at 0x...> display(7-5) II

del plain.type_printers[int] display(7-5) 2

See also

update_display()

Notes

In Python, objects can declare their textual representation using the__repr__ method. IPython expands on this idea and allows objects to declare other, rich representations including:

A single object can declare some or all of these representations; all are handled by IPython’s display system.

The main idea of the first approach is that you have to implement special display methods when you define your class, one for each representation you want to use. Here is a list of the names of the special methods and the values they must return:

The above functions may also return the object’s metadata alonside the data. If the metadata is available, the functions will return a tuple containing the data and metadata, in that order. If there is no metadata available, then the functions will return the data only.

When you are directly writing your own classes, you can adapt them for display in IPython by following the above approach. But in practice, you often need to work with existing classes that you can’t easily modify.

You can refer to the documentation on integrating with the display system in order to register custom formatters for already existing types (Rich display).

Added in version 5.4: display available without import

Added in version 6.1: display available without import

Since IPython 5.4 and 6.1 display() is automatically made available to the user without import. If you are using display in a document that might be used in a pure python context or with older version of IPython, use the following import at the top of your file:

from IPython.display import display

IPython.display.display_html(*objs, **kwargs)

Display the HTML representation of an object.

Note: If raw=False and the object does not have a HTML representation, no HTML will be shown.

Parameters:

IPython.display.display_javascript(*objs, **kwargs)

Display the Javascript representation of an object.

Parameters:

IPython.display.display_jpeg(*objs, **kwargs)

Display the JPEG representation of an object.

Parameters:

IPython.display.display_json(*objs, **kwargs)

Display the JSON representation of an object.

Note that not many frontends support displaying JSON.

Parameters:

IPython.display.display_latex(*objs, **kwargs)

Display the LaTeX representation of an object.

Parameters:

IPython.display.display_markdown(*objs, **kwargs)

Displays the Markdown representation of an object.

Parameters:

IPython.display.display_pdf(*objs, **kwargs)

Display the PDF representation of an object.

Parameters:

IPython.display.display_png(*objs, **kwargs)

Display the PNG representation of an object.

Parameters:

IPython.display.display_pretty(*objs, **kwargs)

Display the pretty (default) representation of an object.

Parameters:

IPython.display.display_svg(*objs, **kwargs)

Display the SVG representation of an object.

Parameters:

IPython.display.publish_display_data(data, metadata=None, *, transient=None, **kwargs)

Publish data and metadata to all frontends.

See the display_data message in the messaging documentation for more details about this message type.

Keys of data and metadata can be any mime-type.

Parameters:

IPython.display.update_display(obj, *, display_id, **kwargs)

Update an existing display by id

Parameters: