Mastering Jupyter’s Special Repr Methods – Part 1

Published on

in

,
Mastering Jupyter’s Special Repr Methods Part 1

In the last post, we explored the __repr__ method in Python, which provides a string representation of an object. However, in interactive environments like Jupyter Notebooks, we can go beyond simple string representations and create rich, formatted displays using special methods like _repr_html_, _repr_markdown_, and others.

What are the different types of repr methods?

These special methods let objects define how they should appear in different output formats. In this Part 1 overview, we’ll look at some of the most commonly used repr methods. Here are some of the commonly used repr methods:

  1. _repr_html_: Returns an HTML representation of the object.
  2. _repr_markdown_: Returns a Markdown representation of the object.
  3. _repr_latex_: Returns a LaTeX representation of the object.
  4. _repr_json_: Returns a JSON representation of the object.
  5. _repr_svg_: Returns an SVG representation of the object.
  6. _repr_jpeg_: Returns a JPEG image representation of the object.
  7. _repr_png_: Returns a PNG image representation of the object.
  8. _repr_pdf_: Returns a PDF representation of the object.
  9. _repr_mimebundle_: Returns a dictionary of MIME types and their corresponding representations.

Above all these only work in interactive environments that support rich media displays, such as Jupyter Notebooks. This is not inbuilt functionality in standard Python scripts or consoles. Whenever you try this method please Jupyter Notebook or Jupyter Lab. If above methods are not defined, Jupyter will fall back to using the __repr__ or __str__ methods.

Advertisements

Example of using _repr_html_

Here’s an example of how to implement the _repr_html_ method in a custom class:

class CustomReprDisplay:
    def _repr_html_(self):
        return "<div style='color:red'>CustomReprDisplay: This is a custom representation!</div>"

custom_display_obj = CustomReprDisplay()
custom_display_obj

When you run this code in a Jupyter Notebook, it will display the HTML content defined in the _repr_html_ method.

<div style='color:red'>CustomReprDisplay: This is a custom representation!</div>

This allows for a more visually appealing representation of the object compared to the default string representation. You may notice above custom repr method is not used double underscores like __repr__. So this is not a magic method but a special method used by Jupyter for rich display.

Advertisements

How it works?

When rendering the class instance in Jupyter Notebook, it checks for the presence of these special repr methods in a specific order. If it finds one, it calls that method to get the representation and displays it accordingly. If none of these methods are defined, it falls back to using the __repr__ or __str__ methods.

For example, if you define _repr_html_, _repr_svg_, and __repr__ in a class, Jupyter will use _repr_html_ for display since it checks for HTML representation first.

class CustomMutliRepr:
    def _repr_html_(self):
        print('CustomMutliRepr._repr_html_ called')
        return "<div style='color:blue'>This is HTML representation!</div>"
    
    def _repr_svg_(self):
        print('CustomMutliRepr._repr_svg_ called')
        return "<svg><text x='0' y='15' fill='red'>This is SVG representation!</text></svg>"
    
    def __repr__(self):
        print('CustomMutliRepr.__repr__ called')
        return "This is the default string representation."

custom_multi_repr_obj = CustomMutliRepr()
custom_multi_repr_obj

When you run this code in a Jupyter Notebook, it will display the HTML representation defined in the _repr_html_ method, ignoring the SVG and default string representations. The output will be:

CustomMutliRepr.__repr__ called
CustomMutliRepr._repr_html_ called
CustomMutliRepr._repr_svg_ called
<div style='color:blue'>This is HTML representation!</div>

You might notice that when the print statements run, several repr methods seem to be called. This happens because Jupyter checks all available representation methods to determine which one to use. If a specific _repr_*_ method isn’t defined, Jupyter falls back to the standard __repr__ or __str__ method. In this example, since _repr_html_ is defined, it’s used for display. If _repr_html_ isn’t available, Jupyter tries _repr_svg_, and if that too is missing, it ultimately falls back to __repr__.

👉 In the next part, we’ll explore the different custom repr methods and their use cases as a part 2.

References:

  1. https://wearexplorer.com/2025/10/20/understanding-python-repr-method/

👋

Advertisements

← Back

Thank you for your response. ✨

Rate your experience(required)

Leave a Reply


Join the Club

Stay updated with our latest tips and other news by joining our newsletter.

Advertisements

Discover more from WE ARE XPLORER

Subscribe now to keep reading and get access to the full archive.

Continue reading

Update cookies preferences