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.
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? 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With