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')
Is there a way to display an SVG from within a function (or a class)?
You need to
display
the SVG likeYou 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 ordinaryprint
statement, but can handle different representations like images, html, latex, etc. For details have a look at the rich display documentation.Add
return
to your function :Then call your functions as the last line in cell: