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 (e.g., 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 (e.g., 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?