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)?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With