问题描述
我喜欢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
在此处引用解决方案:
Brian Granger:
Brian Granger:
"让函数返回包装在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!