通过函数在IPython笔记本中显示SVG [英] Display SVG in IPython notebook from a function

查看:217
本文介绍了通过函数在IPython笔记本中显示SVG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IPython笔记本中,以下代码在单元格下方显示SVG:

In IPython notebook, the following code displays the SVG below the cell:

from IPython.display import SVG
SVG(url='http://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg')

以下内容不显示任何内容:

The following displays nothing:

from IPython.display import SVG
def show_svg():
    SVG(url='http://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg')

是否可以从函数(或类)中显示SVG?

Is there a way to display an SVG from within a function (or a class)?

推荐答案

您需要display像SVG

from IPython.display import SVG, display
def show_svg():
    display(SVG(url='http://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg'))

第一个示例是SVG对象返回自身,然后由IPython显示设备显示.当您想以自定义方法创建SVG对象时,需要注意显示.
display调用与普通的print语句相似,但可以处理不同的表示形式,例如图像,html,乳胶等.有关详细信息,请参见

You first example works as the SVG object returns itself an is subsequently displayed by the IPython display machinery. As you want to create your SVG object in a custom method, you need to take care of the displaying.
The display call is similar to the ordinary print statement, but can handle different representations like images, html, latex, etc. For details have a look at the rich display documentation.

这篇关于通过函数在IPython笔记本中显示SVG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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