制作像 Markdown 这样的输出单元格 [英] Make output cells like Markdown

查看:29
本文介绍了制作像 Markdown 这样的输出单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 IPython 的 Markdown 单元将 HTML 和其他丰富的内容整合到笔记本中.我想知道在输出单元格中是否可以类似地格式化命令输出.

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.

这是我输出 HTML 的函数之一:

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>
    """

上面的 HTML 代码,如果放在 markdown(输入)单元格中,会生成到 Matplotlib 库的很好的链接.但在输出单元格中它只是纯文本.有什么办法可以让它内容丰富?

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?

推荐答案

在这里找到了解决方案:http://mail.scipy.org/pipermail/ipython-user/2012-April/009838.html

Found a solution here: http://mail.scipy.org/pipermail/ipython-user/2012-April/009838.html

在此处引用解决方案以供参考:

Quoting the solution here for ref:

布莱恩·格兰杰:

"让函数返回包装在 HTML 对象中的原始 HTML:

" 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)

"

现在调用 foo() 确实提供了我想要的格式丰富的 html.

Now calling foo() does give rich formatted html as I wanted.

这篇关于制作像 Markdown 这样的输出单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆