Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Display SVG in IPython notebook from a function

Writer Matthew Martinez

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

from IPython.display import SVG
SVG(url=')

The following displays nothing:

from IPython.display import SVG
def show_svg(): SVG(url=')

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

2 Answers

You need to display the SVG like

from IPython.display import SVG, display
def show_svg(): display(SVG(url='))

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.

0

Add return to your function :

from IPython.display import SVG
def show_svg(): return SVG(url=')

Then call your functions as the last line in cell:

show_svg()
9

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy