Display SVG in IPython notebook from a function

前端 未结 2 1023
情歌与酒
情歌与酒 2021-01-31 17:30

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/         


        
相关标签:
2条回答
  • 2021-01-31 18:31

    Add return to your function :

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

    Then call your functions as the last line in cell:

    show_svg()
    
    0 讨论(0)
  • 2021-01-31 18:32

    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.

    0 讨论(0)
提交回复
热议问题