In IPython you can have alternative representations of the an object, as seen here. However I was unable to find a bare bones example of an implementation of the alternative display logic. Obviously you can do more with it than what I show here but I thought a simple demonstration would be useful.
For some background, a __repr__ method in Python will return a text representation of an object. Since everything is an object in Python this is how Python returns output when displaying a string object like 'Hello world' e.g. "Hello world" is returned or for an int object like 5, "5" is returned. IPython extends this idea to more than mere text representations to include SVG, JPG, PNG, LaTex, HTML, and JSON representations. This is how, for instance, a pandas dataframe can be displayed as an HTML table. IPython's display methods are used to display all rich representations in a Notebook.
There is a hierarchy of display order, which means if IPython can return a richer display format it will pick the highest in the hierarchy. The lowest is plain text, followed by JSON, PNG, JPEG, SVG, LaTeX, and finally HTML. However, this display order can be overridden by using a specific display method, display_PNG for when an object has both a PNG and a LaTex repr methods.
The following is a simple example showing some repr methods followed by an example using an override call.
class test(object):
def __init__(self):
pass
def __repr__(self):
"Lowest in the display order"
txt = "2 + 2 = 4"
return txt
# def _repr_png_(self):
# "Higher than text in the display order"
# with open("PNG_doc.resized.png",'rb') as f:
# txt = f.read()
# return txt
# def _repr_svg_(self):
# "Higher then text in the display order"
# txt = """<svg width="100" height="100">
# <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
# </svg>"""
# return txt
# def _repr_latex_(self):
# "Second highest in the display order"
# txt = "$$x^2 = 4$$"
# return txt
# def _repr_html_(self):
# "Highest in the display order"
# txt = "<strong>What?</strong>"
# return txt
T = test()
T
2 + 2 = 4
class test(object):
def __init__(self):
pass
def __repr__(self):
"Lowest in the display order"
txt = "2 + 2 = 4"
return txt
def _repr_png_(self):
"Higher than text in the display order"
with open("PNG_doc.resized.png",'rb') as f:
txt = f.read()
return txt
# def _repr_svg_(self):
# "Higher then text in the display order"
# txt = """<svg width="100" height="100">
# <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
# </svg>"""
# return txt
# def _repr_latex_(self):
# "Second highest in the display order"
# txt = "$$x^2 = 4$$"
# return txt
# def _repr_html_(self):
# "Highest in the display order"
# txt = "<strong>What?</strong>"
# return txt
T = test()
T
class test(object):
def __init__(self):
pass
def __repr__(self):
"Lowest in the display order"
txt = "2 + 2 = 4"
return txt
def _repr_png_(self):
"Higher than text in the display order"
with open("PNG_doc.resized.png",'rb') as f:
txt = f.read()
return txt
def _repr_svg_(self):
"Higher then text in the display order"
txt = """<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>"""
return txt
# def _repr_latex_(self):
# "Second highest in the display order"
# txt = "$$x^2 = 4$$"
# return txt
# def _repr_html_(self):
# "Highest in the display order"
# txt = "<strong>What?</strong>"
# return txt
T = test()
T
class test(object):
def __init__(self):
pass
def __repr__(self):
"Lowest in the display order"
txt = "2 + 2 = 4"
return txt
def _repr_png_(self):
"Higher than text in the display order"
with open("PNG_doc.resized.png",'rb') as f:
txt = f.read()
return txt
def _repr_svg_(self):
"Higher then text in the display order"
txt = """<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>"""
return txt
def _repr_latex_(self):
"Second highest in the display order"
txt = r"$$x^2 = \frac{16}{4}$$"
return txt
# def _repr_html_(self):
# "Highest in the display order"
# txt = "<strong>What?</strong>"
# return txt
T = test()
T
class test(object):
def __init__(self):
pass
def __repr__(self):
"Lowest in the display order"
txt = "2 + 2 = 4"
return txt
def _repr_png_(self):
"Higher than text in the display order"
with open("PNG_doc.resized.png",'rb') as f:
txt = f.read()
return txt
def _repr_svg_(self):
"Higher then text in the display order"
txt = """<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>"""
return txt
def _repr_latex_(self):
"Second highest in the display order"
txt = "$$x^2 = 4$$"
return txt
def _repr_html_(self):
"Highest in the display order"
txt = "<strong>What? .... HTML!</strong>"
return txt
T = test()
T
from IPython.display import display_svg
display_svg(T)