[IPython-dev] Notebook: grabbing HTML for an arbitrary object

Stéfan van der Walt stefan at sun.ac.za
Thu Dec 5 17:44:13 EST 2013


On Fri, Dec 6, 2013 at 12:16 AM, Jacob Vanderplas
<jakevdp at cs.washington.edu> wrote:
> To be more concrete, I want a function ``get_html_rep()`` so that the
> following two cells show the same result in the notebook, for an arbitrary
> object ``myobj``:
>
> In [1]:
> from IPython.display import HTML
> html = get_html_rep(myobj)
> HTML(html)

Perhaps something like this will work:


from IPython import InteractiveShell

def get_html_rep(obj):
    shell = InteractiveShell.instance()
    format = shell.display_formatter.format
    rendered, _ = format(obj, ('text/plain', 'text/html'))
    return rendered['text/html'] if 'text/html' in rendered else
rendered['text/plain']


Stéfan



More information about the IPython-dev mailing list