[英]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.