IPython: Productive Interactive Computing - An advanced interactive computing environment and command shell for Python.
86
IPython's rich output formatting and media display capabilities for presenting HTML, images, videos, interactive widgets, and custom representations. The display system enables rich media output in Jupyter notebooks and IPython terminals.
Primary functions for displaying objects and managing display output.
def display(*objs, include=None, exclude=None, metadata=None, transient=None, display_id=None, **kwargs):
"""
Display objects with rich formatting.
Parameters:
- *objs: objects to display
- include: list, optional - MIME types to include
- exclude: list, optional - MIME types to exclude
- metadata: dict, optional - Metadata for display
- transient: dict, optional - Transient display data
- display_id: str, optional - Unique ID for updating display
- **kwargs: additional display arguments
Returns:
DisplayHandle or None
"""
def clear_output(wait=False):
"""
Clear the output area.
Parameters:
- wait: bool - If True, wait until next output before clearing
"""
def update_display(obj, *, display_id, **kwargs):
"""
Update an existing display.
Parameters:
- obj: object to display
- display_id: str - ID of display to update
- **kwargs: additional display arguments
"""
class DisplayHandle:
"""
Handle for display updates.
Returned by display() when display_id is provided,
allows updating the display content.
"""
def __init__(self, display_id=None):
"""Initialize display handle with optional ID."""
def display(self, obj, **kwargs):
"""Display object using this handle."""
def update(self, obj, **kwargs):
"""Update display using this handle."""
def publish_display_data(data, metadata=None, source=None, *, transient=None, **kwargs):
"""
Publish display data for consumption by frontends.
Parameters:
- data: dict - Display data keyed by MIME type
- metadata: dict, optional - Metadata for display
- source: str, optional - Source of the display
- transient: dict, optional - Transient display data
"""Usage example:
from IPython.display import display, clear_output, HTML, Image
import time
# Display various objects
display("Hello World")
display(HTML("<h1>HTML Content</h1>"))
display(Image("path/to/image.png"))
# Display with ID for later updates
display("Initial content", display_id="my_display")
time.sleep(1)
update_display("Updated content", display_id="my_display")
# Clear output
clear_output(wait=True)
display("New content after clear")Rich display objects for different media types and formats.
class DisplayObject:
"""
Base class for rich display objects.
All display objects inherit from this class and can be displayed
using the display() function or by being the last expression in a cell.
"""
def __init__(self, data=None, url=None, filename=None, metadata=None):
"""
Initialize display object.
Parameters:
- data: raw data for the object
- url: URL to fetch data from
- filename: local filename to read data from
- metadata: dict of metadata
"""
class HTML(DisplayObject):
"""HTML display object for rendering HTML content."""
def __init__(self, data=None, url=None, filename=None, metadata=None):
"""Create HTML display object."""
class Markdown(DisplayObject):
"""Markdown display object for rendering Markdown content."""
def __init__(self, data=None, url=None, filename=None, metadata=None):
"""Create Markdown display object."""
class Image(DisplayObject):
"""Image display object for displaying images."""
def __init__(self, data=None, url=None, filename=None, format=None,
embed=None, width=None, height=None, retina=False,
unconfined=False, metadata=None):
"""
Create image display object.
Parameters:
- format: str - Image format ('png', 'jpeg', 'svg', etc.)
- embed: bool - Whether to embed image data
- width: int - Display width in pixels
- height: int - Display height in pixels
- retina: bool - Whether image is high-DPI
- unconfined: bool - Whether to remove size constraints
"""
class Video(DisplayObject):
"""Video display object for displaying videos."""
def __init__(self, data=None, url=None, filename=None, embed=False,
mimetype=None, width=None, height=None, html_attributes="controls"):
"""
Create video display object.
Parameters:
- embed: bool - Whether to embed video data
- mimetype: str - Video MIME type
- html_attributes: str - HTML attributes for video element
"""
class Audio(DisplayObject):
"""Audio display object for displaying audio files."""
def __init__(self, data=None, url=None, filename=None, rate=None,
autoplay=False, normalize=True, **kwargs):
"""
Create audio display object.
Parameters:
- rate: int - Sample rate in Hz
- autoplay: bool - Whether to autoplay
- normalize: bool - Whether to normalize audio
"""Specialized display objects for text-based content.
class Pretty(DisplayObject):
"""Pretty-printed text display object."""
def __init__(self, data, url=None, filename=None, metadata=None):
"""Create pretty-printed display object."""
class Code(DisplayObject):
"""Code display object with syntax highlighting."""
def __init__(self, data=None, url=None, filename=None, language=''):
"""
Create code display object.
Parameters:
- language: str - Programming language for syntax highlighting
"""
class JSON(DisplayObject):
"""JSON display object with interactive tree view."""
def __init__(self, data=None, url=None, filename=None, expanded=False,
metadata=None, root='root', **kwargs):
"""
Create JSON display object.
Parameters:
- expanded: bool - Whether to expand tree by default
- root: str - Root node name
"""
class Latex(DisplayObject):
"""LaTeX display object for mathematical expressions."""
def __init__(self, data=None, url=None, filename=None, metadata=None):
"""Create LaTeX display object."""
class SVG(DisplayObject):
"""SVG display object for vector graphics."""
def __init__(self, data=None, url=None, filename=None, metadata=None):
"""Create SVG display object."""
class Math(DisplayObject):
"""Math display object for mathematical expressions (alias for Latex)."""
def __init__(self, data=None, url=None, filename=None, metadata=None):
"""Create Math display object."""
class Javascript(DisplayObject):
"""JavaScript display object for executing JavaScript code."""
def __init__(self, data=None, url=None, filename=None, metadata=None, lib=None, css=None):
"""
Create JavaScript display object.
Parameters:
- lib: list - JavaScript libraries to include
- css: list - CSS stylesheets to include
"""
class GeoJSON(DisplayObject):
"""GeoJSON display object for geographic data visualization."""
def __init__(self, data=None, url_template=None, layer_options=None,
hovertext='', metadata=None, **kwargs):
"""
Create GeoJSON display object.
Parameters:
- url_template: str - URL template for tile layers
- layer_options: dict - Options for map layers
- hovertext: str - Text to show on hover
"""
class ProgressBar(DisplayObject):
"""Progress bar display object for showing task progress."""
def __init__(self, total, **kwargs):
"""
Create progress bar display object.
Parameters:
- total: int - Total number of items to process
"""Display objects for web content and interactive elements.
class IFrame(DisplayObject):
"""IFrame display object for embedding web content."""
def __init__(self, src, width=400, height=300, **kwargs):
"""
Create IFrame display object.
Parameters:
- src: str - URL of content to embed
- width: int - Frame width in pixels
- height: int - Frame height in pixels
"""
class YouTubeVideo(DisplayObject):
"""YouTube video display object."""
def __init__(self, id, width=400, height=300, **kwargs):
"""
Create YouTube video display object.
Parameters:
- id: str - YouTube video ID
"""
class VimeoVideo(DisplayObject):
"""Vimeo video display object."""
def __init__(self, id, width=400, height=300, **kwargs):
"""
Create Vimeo video display object.
Parameters:
- id: str - Vimeo video ID
"""
class FileLink(DisplayObject):
"""File link display object for downloadable files."""
def __init__(self, path, url_prefix='', result_html_prefix='',
result_html_suffix='<br>'):
"""
Create file link display object.
Parameters:
- path: str - Path to file
- url_prefix: str - URL prefix for links
- result_html_prefix: str - HTML before link
- result_html_suffix: str - HTML after link
"""
class FileLinks(DisplayObject):
"""Multiple file links display object."""
def __init__(self, path, included_suffixes=None, **kwargs):
"""
Create multiple file links display object.
Parameters:
- path: str - Directory path
- included_suffixes: list - File extensions to include
"""Functions for specific MIME type formatting.
def display_pretty(obj, raw=False, metadata=None):
"""Display pretty-printed representation of object."""
def display_html(obj, raw=False, metadata=None):
"""Display HTML representation of object."""
def display_markdown(obj, raw=False, metadata=None):
"""Display Markdown representation of object."""
def display_svg(obj, raw=False, metadata=None):
"""Display SVG representation of object."""
def display_png(obj, raw=False, metadata=None):
"""Display PNG representation of object."""
def display_jpeg(obj, raw=False, metadata=None):
"""Display JPEG representation of object."""
def display_latex(obj, raw=False, metadata=None):
"""Display LaTeX representation of object."""
def display_json(obj, raw=False, metadata=None):
"""Display JSON representation of object."""
def display_javascript(obj, raw=False, metadata=None):
"""Display JavaScript representation of object."""
def display_webp(obj, raw=False, metadata=None):
"""Display WebP representation of object."""
def display_pdf(obj, raw=False, metadata=None):
"""Display PDF representation of object."""Usage example:
from IPython.display import *
# Display various media types
display(HTML("<h2>HTML Content</h2>"))
display(Markdown("## Markdown Content"))
display(Image("image.png", width=300))
display(Video("video.mp4", width=500, height=300))
# Display code with syntax highlighting
display(Code("""
def hello(name):
print(f"Hello {name}!")
""", language='python'))
# Display interactive JSON
data = {"users": [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]}
display(JSON(data, expanded=True))
# Display LaTeX math
display(Latex(r"$\int_0^1 x^2 dx = \frac{1}{3}$"))
# Embed web content
display(YouTubeVideo("dQw4w9WgXcQ"))
display(IFrame("https://example.com", width=600, height=400))class DisplayHandle:
"""
Handle for display updates.
Returned by display() when display_id is provided,
allows updating the display content.
"""
def __init__(self, display_id=None):
"""Initialize display handle with optional ID."""
def display(self, obj, **kwargs):
"""Display object using this handle."""
def update(self, obj, **kwargs):
"""Update display using this handle."""
class DisplayPublisher:
"""
Publisher for display data.
Manages publication of display data to frontends
and handles display ID tracking.
"""
def publish(self, data, metadata=None, source=None, **kwargs):
"""Publish display data."""Install with Tessl CLI
npx tessl i tessl/pypi-ipythondocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10