python - 使用小部件在 jupyter 笔记本中显示 gif

标签 python jupyter-notebook ipywidgets

如何使用 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'
)

请参阅docs对于Image小部件。

关于python - 使用小部件在 jupyter 笔记本中显示 gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60640375/

相关文章:

python - 在 jupyter notebook 中运行 pytest 测试函数

ipython - 如何停止执行 Jupyter Notebook 中的所有单元格

jupyter-notebook - 使用带有选项 manual=True 的 ipywidget interactive()

python - ipywidgets,如何更改 slider 的值显示精度

python - 使用python将带有特殊字符的值插入mysql表时出错

python - Python 处理重复项

visual-studio-code - Markdown:待办事项列表未显示在 VSCode 的 Jupyter Notebook 单元中

documentation - ipywidgets.Button 的 'icon' 参数是什么?

python - 我可以在 try-except block 中获取其中一项 Assets 的字符串吗?

python - 将 datetime64 列拆分为 pandas 数据框中的日期和时间列