使用小部件在 jupyter notebook 中显示 gif

Displaying gifs in jupyter notebook using widgets

如何使用 python 小部件在 jupyeter 笔记本中显示 gif。

我试过:

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

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

但是接收错误:

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

不管我怎么试都不行

您需要将图像读入文件处理程序并将其读入字节字符串,然后才能像这样将其传递到小部件中:

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

查看 Image 小部件的 docs