简体   繁体   English

如何在Python包内的Jupyter Notebook中显示.gif

[英]How to display a .gif in a Jupyter Notebook within a Python package

I am writing a small python package for interactive data visualisation. 我正在编写一个用于交互数据可视化的小型python程序包。 We would like to include gifs with the package so users can run an example and learn how to interact with the figure. 我们希望在程序包中包含gif,以便用户可以运行示例并学习如何与图形交互。

Here is an example structure: 这是一个示例结构:

<package>
    +-->some_module/
        +--> static/
            +--> foo.gif
        +--> spam.py
    +-->examples/
       +--> example.ipynb

We have several classes (eg, spam.py may contain the class Spam), and these classes will have a method .show_gif() which will access the gif ( foo.gif ) within static and show it to the user. 我们有几个类(例如, spam.py可能包含Spam类),并且这些类将具有.show_gif()方法,该方法将访问static中的gif( foo.gif )并将其显示给用户。

The code is wrote to be used within a Jupyter notebook. 该代码被编写为在Jupyter笔记本中使用。 In the package we include some examples (eg, example.ipynb ), these will import spam and then call the .show_gif() method on the relevant class. 在包中,我们包括一些示例(例如example.ipynb ),这些示例将导入spam ,然后在相关类上调用.show_gif()方法。

Currently we display the gifs using: 目前,我们使用以下方式显示GIF:

from IPython.core.display import Image
Image(filename=path_to_gif)

which works fine when the gif is in a sub-directory of the folder the jupyter notebook is in, but not when the gif is within a sibling directory of the package (as in the above example). 当gif位于jupyter笔记本所在文件夹的子目录中时,此方法工作正常,但当gif位于程序包的同级目录中时则无效(如上例所示)。

EDIT: 编辑:

I believe I can access the .gif but cannot display it in a jupyter notebook (see below) 我相信我可以访问.gif,但不能在jupyter笔记本中显示它(请参见下文)

There are similar stack overflow questions ( This question ) which suggest using importlib.resources module from the standard library. 有类似的堆栈溢出问题( 此问题 ),建议使用标准库中的importlib.resources module However, they return a BinaryIO instance I don't know how to deal with see: 但是,它们返回一个我不知道如何处理的BinaryIO实例,请参见:

import some_module.static as static

…

def show_gif(self):
    image = pkg_resources.open_binary(static, 'foo.gif')
    return Image(data=image)

this throws an error because I am giving Image a wrong type for data. 这会引发错误,因为我给Image提供了错误的数据类型。 How can I open this .gif and display it in a jupyter notebook? 如何打开此.gif并将其显示在jupyter笔记本中?

Use any method to find the path to your gif file and open it for reading; 使用任何方法找到gif文件的路径并打开以进行阅读; pkg_resources.open_binary() will do, though as this is an actual file on disk there are simpler methods. pkg_resources.open_binary()可以,尽管这是磁盘上的实际文件,所以有一些更简单的方法。 But now you have an open filehandle, not the content itself-- same as when you call open() on a file. 但是现在您有了一个打开的文件句柄,而不是内容本身-与在文件上调用open()相同。 To get the image data, you need to read() from the filehandle. 要获取图像数据,您需要从文件句柄中read()

You then have a bytes object, not a text string. 然后,您将拥有一个bytes对象,而不是一个文本字符串。 That's not a problem since Image() accepts bytes in the constructor: 这不是问题,因为Image()接受构造函数中的bytes

with pkg_resources.open_binary(some_module, 'foo.gif') as src:
    imagedata = src.read()
image = Image(data=imagedata)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 google colab 在 jupyter notebook 中显示 GIF? - How to display a GIF in jupyter notebook using google colab? 如何在 Jupyter Python Notebook 中的 gmap 上显示文本? - How to display text on gmaps in Jupyter Python Notebook? 如何在jupyter笔记本中将python字符串显示为HTML - how to display a python string as HTML in jupyter notebook 如何在jupyter笔记本中的同一环境中在python包的本地副本和基本副本之间切换以进行开发 - How to switch between local copy and base copy of python package within same environment in a jupyter notebook for development 在Jupyter Notebook上安装python 3软件包 - Installing python 3 package on Jupyter Notebook 如何在 Jupyter notebook 中嵌入 gif? - How do I embed a gif in Jupyter notebook? Python Jupyter Notebook 内的多处理 - Python Multiprocessing within Jupyter Notebook 如何在 Visual Studio Code 中的 Jupyter Notebook 中显示所有 output? - How to display all output in Jupyter Notebook within Visual Studio Code? 如何在 Jupyter 笔记本中将视频文件的帧显示为 gif 而无需写入文件 - How to display frames of a video file as gif in Jupyter notebook without writing file 如何在 google colab(python,Jupyter notebook)中安装预定义的包(类)? - How to install a predefined package(class) in google colab(python,Jupyter notebook)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM