Issue
In IPython (and therefore Jupyter notebooks), I can define methods such as _repr_html_
and _repr_svg_
on an object, and these will be used when calling IPython.display.display(my_obj)
. But if multiple such methods are defined and possible to use (e.g. viewing a notebook in a web browser, and displaying an object which has both _repr_html_
and _repr_svg_
methods), which one will take priority? In the documentation notes at https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.display there is the following list, but it doesn't state that this is in priority order
- HTML
- JSON
- PNG
- JPEG
- SVG
- LaTeX
Is there documentation somewhere on this that I have missed?
Solution
The priority is decided by the frontend, not IPython. IPython sends them all (unless you use display with exclude
or include
keywords. Reason being that once the notebook is saved you might want to open with another frontend with a different display priority order.
Classic notebook has the following order (outputarea.js line 1080):
OutputArea.display_order = [
MIME_JAVASCRIPT,
MIME_HTML,
MIME_MARKDOWN,
MIME_LATEX,
MIME_SVG,
MIME_PNG,
MIME_JPEG,
MIME_GIF,
MIME_PDF,
MIME_TEXT
];
For JupyterLab this is a bit more complicated, each registered mimetype can get a rank assigned to it (and likely changed), As extension can install mime-handler this will depends on the extensions you have.
Nbconvert also have it's own order for html-like exporters:
display_data_priority = List(['text/html', 'application/pdf', 'text/latex', 'image/svg+xml', 'image/png', 'image/jpeg', 'text/markdown', 'text/plain']
and for tex-like exporter:
'display_data_priority' : ['text/latex', 'application/pdf', 'image/png', 'image/jpeg', 'image/svg+xml', 'text/markdown', 'text/plain']
I wrote a demo for JupyterCon 2017 – Jupyter: Kernels, Protocols, and the IPython Reference Implementation (around t=25min) to show that notebooks could have a dropdown to switch which representation(s) is shown on a per-cell basis which stills need to be actually implemented and merged in the main branch if you have the usage/motivation to contribute.
Answered By - Matt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.