I like IPython's Markdown cells for incorporating HTML and other rich content inside notebooks. I would like to know if a command output can be formatted similarly, in output cells.
Here is one of my functions outputting HTML:
print_html():
print """
<h2>Matplotlib's chart gallery (Click a chart to see the code to create it)</h2><br>
<div align="center"> <iframe title="Matplotlib Gallery" width="950"
height="250" src="http://matplotlib.org/gallery.html#api" frameborder="0"
allowfullscreen></iframe></div>
"""
The HTML code above, if placed in markdown (input) cell, produces nice link to the Matplotlib library. But in output cell it is just plain text. Any way to make it rich content?
First Markdown cell By default, the first cell in a notebook is a code cell. If you want to begin with a Markdown cell, change the cell type, you have the following options: Press Ctrl + M . Click the Select cell language icon on the cell toolbar and select Convert to markdown.
To show the full data without any hiding, you can use pd. set_option('display. max_rows', 500) and pd.
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. This is a simple way to suppress unwanted output.
Markdown cell displays text which can be formatted using markdown language. In order to enter a text which should not be treated as code by Notebook server, it must be first converted as markdown cell either from cell menu or by using keyboard shortcut M while in command mode.
Found a solution here: http://mail.scipy.org/pipermail/ipython-user/2012-April/009838.html
Quoting the solution here for ref:
Brian Granger:
" Have the function return the raw HTML wrapped in an HTML object:
from IPython.core.display import HTML
...
...
def foo():
raw_html = "<h1>Yah, rendered HTML</h1>"
return HTML(raw_html)
"
Now calling foo() does give rich formatted html as I wanted.
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