如何嵌入 HTML 到 iPython notebook的輸出

如何嵌入 HTML 到 iPython notebook的輸出

iPython notebook中能夠嵌入 HTML,也適用於JupyterHub和JupyterLab環境。不只能夠顯示經常使用的HTML標籤文本,甚至能夠嵌入腳本交互操做和Frame分隔框架。具體實現能夠有多種方法,效果以下。html

 

%%html
<a href="http://example.com">link</a>

<a href="http://example.com">link</a>python

from IPython.core.display import HTML
HTML('<a href="http://example.com">link to example.com</a>')

<a href="http://example.com">link to example.com</a>app

display(HTML('<h1>Hello, world!</h1>'))
print("Here's a link:")
display(HTML("<a href='http://www.google.com' target='_blank'>www.google.com</a>"))
print("some more printed text ...")
display(HTML('<p>Paragraph text here ...</p>'))

<h1>Hello, world!</h1>框架

Here's a link:

<a href='http://www.google.com' target='_blank'>www.google.com</a>ide

some more printed text ...

<p>Paragraph text here ...</p>google

from html import escape # Python 3 only :-)

class Todo:
    def __init__(self):
        self.items = []

    def add(self, text, completed):
        self.items.append({'text': text, 'completed': completed})

    def _repr_html_(self):
        return "<ol>{}</ol>".format("".join("<li>{} {}</li>".format(
            "☑" if item['completed'] else "☐",
            escape(item['text'])
        ) for item in self.items))

my_todo = Todo()
my_todo.add("Buy milk", False)
my_todo.add("Do homework", False)
my_todo.add("Play video games", True)

my_todo

<ol><li>☐ Buy milk</li><li>☐ Do homework</li><li>☑ Play video games</li></ol>spa

from IPython.display import IFrame

IFrame(src='https://s3.amazonaws.com/duhaime/blog/visualizations/isolation-forests.html', width=800, height=700)
<iframe
        width="800"
        height="700"
        src="https://s3.amazonaws.com/duhaime/blog/visualizations/isolation-forests.html"
        frameborder="0"
        allowfullscreen
    ></iframe>
相關文章
相關標籤/搜索