Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPython Notebook: render cell as HTML from variable content

In iPython Notebook I have a cell where I define a variable (say html) and assign to it an html content (response content of a http get request). I would like to render this variable into another cell as html. Is it possible? How can I do it?

eg:

[1] html = '''<html><h1>a heading</h1><ul><li>one</li><li>two</li><li>three</li></ul></html>'''

I would like to render this in the next cell.

like image 405
mettjus Avatar asked Jun 11 '14 09:06

mettjus


People also ask

How do I export a Jupyter Notebook output to HTML?

This method is as simple as clicking File, Download as, HTML (. html). Jupyter will then download the notebook as an HTML file to wherever the browser defaults for downloaded files. Screenshot of export by author.

What does %% capture do in Jupyter?

What does %% capture do in Jupyter? Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams.

Can you use variables in markdown Jupyter?

Surprisingly, Jupyter Notebooks do not support the inclusion of variables in Markdown Cells out of the box. If you still use Jupyter Notebooks there is a readily solution: the Python Markdown extension. It is part of the nbextensions package which is easy to install and configure.


1 Answers

I was able to achieve the desired goal by:

from IPython.display import display, HTML
display(HTML(response.content))

I found the solution here.

More info can be found here.

like image 157
mettjus Avatar answered Nov 07 '22 11:11

mettjus