如何从对象中检索“丰富显示”HTML?

时间:2014-10-19 22:36:51

标签: ipython-notebook

如何检索IPython笔记本生成的HTML字符串以显示该对象(在Python中,我不是在从浏览器检查HTML源代码之后)?

1 个答案:

答案 0 :(得分:1)

直接致电_repr_html_

from IPython.display import display
class Foo(object):

    def _repr_html_(self):
        return "<b>HTML</b>"

display(Foo())  #display HTML in bold
Foo()._repr_html_()  # return "<b>HTML</b>"

仅当对象实现_repr_*_本身时才有效。如果在IPython上为一个对象注册了格式化程序,你可以粗略地访问它:get_ipython().display_formatter.formatters['text/html'].for_type(your_object)但是你只需要很少的机会使用这个方法。

相关问题