简体   繁体   中英

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

I am writing a small python package for interactive data visualisation. We would like to include gifs with the package so users can run an example and learn how to interact with the figure.

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.

The code is wrote to be used within a Jupyter notebook. 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.

Currently we display the gifs using:

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).

EDIT:

I believe I can access the .gif but cannot display it in a jupyter notebook (see below)

There are similar stack overflow questions ( This question ) which suggest using importlib.resources module from the standard library. However, they return a BinaryIO instance I don't know how to deal with see:

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. How can I open this .gif and display it in a jupyter notebook?

Use any method to find the path to your gif file and open it for reading; pkg_resources.open_binary() will do, though as this is an actual file on disk there are simpler methods. But now you have an open filehandle, not the content itself-- same as when you call open() on a file. To get the image data, you need to read() from the filehandle.

You then have a bytes object, not a text string. That's not a problem since Image() accepts bytes in the constructor:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM