Displaying gifs in jupyter notebook using widgets

LM23 :

How do you use python widgets to display gifs in a jupyeter notebook.

I have tried:

gif_box = widgets.Image("sample.gif")

or

gif_box = widgets.Video("sample.gif")

but receiver error:

TypeError: __init__() takes 1 positional argument but 2 were given

Regardless of what I try it won't work

khuynh :

You need to read your image into a file handler and read it into a byte string before you can pass it into the widget like so:

# read file as bytes and get file handler. use `with` to ensure 
# your file is closed after you're done reading it
with open("sample.gif", "rb") as file:
    # read file as string into `image` 
    image = file.read()

widgets.Image(
    value=image,
    format='gif'
)

See the docs for the Image widget.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=237854&siteId=1