Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed HTML into IPython output?

Is it possible to embed rendered HTML output into IPython output?

One way is to use

from IPython.core.display import HTML HTML('<a href="http://example.com">link</a>') 

or (IPython multiline cell alias)

%%html <a href="http://example.com">link</a> 

Which return a formatted link, but

  1. This link doesn't open a browser with the webpage itself from the console. IPython notebooks support honest rendering, though.
  2. I'm unaware of how to render HTML() object within, say, a list or pandas printed table. You can do df.to_html(), but without making links inside cells.
  3. This output isn't interactive in the PyCharm Python console (because it's not QT).

How can I overcome these shortcomings and make IPython output a bit more interactive?

like image 537
Anton Tarasenko Avatar asked Sep 06 '14 08:09

Anton Tarasenko


People also ask

How do you display HTML content in Python?

In order to display the HTML file as a python output, we will be using the codecs library. This library is used to open files which have a certain encoding. It takes a parameter encoding which makes it different from the built-in open() function.

Can we use HTML in Jupyter Notebook?

Notebooks may be exported to a range of static formats, including HTML (for example, for blog posts), reStructuredText, LaTeX, PDF, and slide shows, via the nbconvert command. Furthermore, any . ipynb notebook document available from a public URL can be shared via the Jupyter Notebook Viewer <nbviewer>.


2 Answers

This seems to work for me:

from IPython.core.display import display, HTML display(HTML('<h1>Hello, world!</h1>')) 

The trick is to wrap it in "display" as well.

Source: http://python.6.x6.nabble.com/Printing-HTML-within-IPython-Notebook-IPython-specific-prettyprint-tp5016624p5016631.html

like image 131
Harmon Avatar answered Oct 08 '22 10:10

Harmon


Some time ago Jupyter Notebooks started stripping JavaScript from HTML content [#3118]. Here are two solutions:

Serving Local HTML

If you want to embed an HTML page with JavaScript on your page now, the easiest thing to do is to save your HTML file to the directory with your notebook and then load the HTML as follows:

from IPython.display import IFrame  IFrame(src='./nice.html', width=700, height=600) 

Serving Remote HTML

If you prefer a hosted solution, you can upload your HTML page to an Amazon Web Services "bucket" in S3, change the settings on that bucket so as to make the bucket host a static website, then use an Iframe component in your notebook:

from IPython.display import IFrame  IFrame(src='https://s3.amazonaws.com/duhaime/blog/visualizations/isolation-forests.html', width=700, height=600) 

This will render your HTML content and JavaScript in an iframe, just like you can on any other web page:

<iframe src='https://s3.amazonaws.com/duhaime/blog/visualizations/isolation-forests.html', width=700, height=600></iframe>
like image 43
duhaime Avatar answered Oct 08 '22 11:10

duhaime