Skip to content

add gif support#2271

Open
m-reuter wants to merge 5 commits intojupyter:mainfrom
m-reuter:gif
Open

add gif support#2271
m-reuter wants to merge 5 commits intojupyter:mainfrom
m-reuter:gif

Conversation

@m-reuter
Copy link

@m-reuter m-reuter commented Feb 28, 2026

GIF Support Implementation for nbconvert

Overview

This document describes the implementation of native GIF image support in nbconvert, which eliminates the need for the monkey patch previously required for example in WhipperSnapPy (Deep-MI/WhipperSnapPy#59). See also Issue #2270 .

Problem Statement

Previously, nbconvert did not support the image/gif MIME type for display data outputs. This meant that animated GIFs generated by notebook cells would:

  1. Not be extracted from notebook outputs
  2. Fall back to text/plain representation
  3. Require external monkey patches in dependent projects to work properly

Solution Overview

The implementation adds native GIF support across three main areas. All changes are additive and existing conversion without gif will not break.

1. Output Extraction (ExtractOutputPreprocessor)

File: nbconvert/preprocessors/extractoutput.py

Changes:

  • Added "image/gif" to the extract_output_types Set (line 52)
  • Added "image/gif" to the binary MIME types that are base64-decoded (line 92)

Why: This ensures that GIF image data is properly extracted from notebook outputs and converted from base64 to binary format, just like PNG and JPEG images.

2. Display Priority

File: share/templates/base/display_priority.j2

Changes:

  • Added GIF handling to the display priority chain (after JPEG, before plain text)
  • Created a data_gif block for template inheritance

Why: This ensures that GIF outputs are selected and prioritized appropriately in the output rendering pipeline.

3. Template Rendering

Templates were updated across multiple export formats to handle GIF rendering:

HTML Templates

  • File: share/templates/classic/base.html.j2

    • Added data_gif block with full HTML img tag support
    • Supports both file-based and base64 data URIs
    • Includes metadata support (width, height, alt text, etc.)
  • File: share/templates/lab/base.html.j2

    • Added data_gif block with JupyterLab-specific CSS classes
    • Full feature parity with PNG/JPEG rendering

Markdown Template

  • File: share/templates/markdown/index.md.j2
    • Added data_gif block with Markdown image syntax
    • Supports both file references and base64 data URIs

ReStructuredText Template

  • File: share/templates/rst/index.rst.j2
    • Added data_gif block with RST image directive
    • Includes width/height metadata support

AsciiDoc Template

  • File: share/templates/asciidoc/index.asciidoc.j2
    • Added data_gif block with AsciiDoc image syntax

4. Exporters

  • added gif to exporters

5. Tests

  • added gif to tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant