Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display SVG in IPython notebook from a function

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)?

like image 279
prooffreader Avatar asked May 19 '15 19:05

prooffreader


People also ask

How do I display SVG images in Jupyter Notebook?

Steps to reproduce Create a Jupyter Notebook with this content (or use the attached file gitlab-show-svg. ipynb or have a look at this project): # use SVG as backend %config InlineBackend. figure_format = 'svg' # plot a random series import numpy as np import pandas as pd pd.

How do I display an image in IPython notebook?

To display the image, the Ipython. display() method necessitates the use of a function. In the notebook, you can also specify the width and height of the image.

What does %% capture do?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

How do you display plots in Jupyter Notebook?

Jupyter Notebook - Big Data Visualization Tool IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library. The inline option with the %matplotlib magic function renders the plot out cell even if show() function of plot object is not called.


1 Answers

You need to display the SVG like

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')) 

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.

like image 175
Jakob Avatar answered Sep 22 '22 13:09

Jakob