include HTML output in IPython notebook

How to embed HTML within IPython Notebook ?

This tutorial guides you on how to embed HTML within IPython Notebook. In our previous tutorial we had learnt how to embed images within IPython Notebook. Let’s see how to add HTML and display output within Notebook.

How to embed HTML within IPython Notebook ?

JupyterLab allows you to view and edit data in a wide variety of formats. In order to use different data formats as output in notebook, you need to use the relevant display API for the Python Kernel.

For example, IPython kernel supports convenient classes to display rich output directly below code cell within notebook.

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

When you run above code, it will display the HTML output in the notebook below the code cell as shown below.

include HTML output in IPython notebook

 

Also, you can use IPython display function with MIME types and MIME data as key value to construct a raw rich output message as shown below.

from IPython.display import display
display({'text/html': '<h1>Hello World</h1>', 'text/plain': 'Hello World'}, raw=True)

Try running above code in the code cell of IPython notebook. You will see the following output.

include add raw rich html output IPython notebook

 

To learn how to include or add images in Jupyter python notebook from local file or image URL please check this tutorial : Embed image in Jupyter Notebook from local or web resource.

Conclusion

Again remember that, JupyterLab support the following file and output format.

HTML

  • File extension: .html
  • MIME type: text/html

JupyterLab supports rendering HTML in cell output and editing HTML files as text in the file editor.

That’s it. Hope it helped 🙂

You’ll also like:

References

guest
0 Comments
Inline Feedbacks
View all comments