Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make output cells like Markdown

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?

like image 888
nom-mon-ir Avatar asked Dec 07 '12 20:12

nom-mon-ir


People also ask

How do you change cell type to markdown?

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.

How do you show full output in Jupyter Notebook?

To show the full data without any hiding, you can use pd. set_option('display. max_rows', 500) and pd.

What does %% capture do?

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.

What is a markdown cell?

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.


1 Answers

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.

like image 190
nom-mon-ir Avatar answered Oct 06 '22 07:10

nom-mon-ir