question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

elegant way to display gif file in notebook?

See original GitHub issue

Hi,

Is there any way to display gif file in super short way? I have at two ways but none of them are really what I want

  • Using HTML
def display_gif(fn):
    from IPython import display
    return display.HTML('<img src="{}">'.format(fn))

This does not play nicely with browser’s cache. If I remake an image with the same filename, the new image won’t be updated.

  • Rename .gif to .png and then use display.Image(fn).

thanks.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

35reactions
gnestorcommented, Jul 26, 2017

As discussed, there are couple different ways to do this:

HTML

from IPython.display import HTML
HTML('<img src="../giphy.gif">')

Image(filename=*) or Image(url=*) by adding a png extension to GIF file

from IPython.display import Image
Image(filename="../giphy.gif.png")

Image(data=*)

with open('../giphy.gif','rb') as f:
    display(Image(data=f.read(), format='png'))

Ideally, we want to be able to Image(filename=*) or Image(url=*) without being required to rename the file and be able to use format="gif". Is there any reason that GIF is not supported format yet? If not, I say we support it 👍

17reactions
maedoccommented, Jun 11, 2020

Alright, here’s my submission despite the issue being closed: when I tried the other options above, the exported HTML file doesn’t show the GIF, since it doesn’t get embedded base64, so I have the following working solution, (in Python 3.6+)

def show_gif(fname):
    import base64
    from IPython import display
    with open(fname, 'rb') as fd:
        b64 = base64.b64encode(fd.read()).decode('ascii')
    return display.HTML(f'<img src="data:image/gif;base64,{b64}" />')

show_gif('pikachu.gif')
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I embed a gif in Jupyter notebook? - Stack Overflow
I've been trying to display a gif in Jupyter notebook and have had some trouble. I keep getting a blank image file.
Read more >
Embedding GIFs in a Jupyter Notebook! - YouTube
Tutorial on embedding GIFs within a Jupyter Notebook.The notebook can be found in the "Jupyter" folder within the below repo.
Read more >
Easily display animated media content inside your Jupyter ...
This tutorial aims to show you how easy it is to create and insert dynamic content in your jupyter notebooks. Exporting this notebook...
Read more >
Is it possible to insert an animated image into Mathematica ...
This suggests one solution: using a Manipulate or Animate , with those bunch of images. But is there a less tedious and more...
Read more >
Using the Jupyter Notebook environment | ArcGIS API for Python
The page you see in this image is called the Notebook Dashboard. ... and the circle next to the kernal name (Python 3...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found