Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nbconvert/exporters/asciidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def default_config(self):
"image/svg+xml",
"image/png",
"image/jpeg",
"image/gif",
"text/plain",
"text/latex",
]
Expand Down
1 change: 1 addition & 0 deletions nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def default_config(self):
"text/latex",
"image/png",
"image/jpeg",
"image/gif",
"text/plain",
]
},
Expand Down
1 change: 1 addition & 0 deletions nbconvert/exporters/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def default_config(self):
"text/latex",
"image/png",
"image/jpeg",
"image/gif",
"text/plain",
]
},
Expand Down
8 changes: 4 additions & 4 deletions nbconvert/preprocessors/extractoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class ExtractOutputPreprocessor(Preprocessor):
config=True
)

extract_output_types = Set({"image/png", "image/jpeg", "image/svg+xml", "application/pdf"}).tag(
config=True
)
extract_output_types = Set(
{"image/png", "image/jpeg", "image/svg+xml", "application/pdf", "image/gif"}
).tag(config=True)

def preprocess_cell(self, cell, resources, cell_index):
"""
Expand Down Expand Up @@ -90,7 +90,7 @@ def preprocess_cell(self, cell, resources, cell_index):
data = out.data[mime_type]

# Binary files are base64-encoded, SVG is already XML
if mime_type in {"image/png", "image/jpeg", "application/pdf"}:
if mime_type in {"image/png", "image/jpeg", "image/gif", "application/pdf"}:
# data is b64-encoded as text (str, unicode),
# we want the original bytes
data = a2b_base64(data)
Expand Down
1 change: 1 addition & 0 deletions nbconvert/utils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class NbConvertBase(LoggingConfigurable):
"image/svg+xml",
"image/png",
"image/jpeg",
"image/gif",
"text/markdown",
"text/plain",
],
Expand Down
4 changes: 4 additions & 0 deletions share/templates/asciidoc/index.asciidoc.j2
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
![jpeg]({{ output.metadata.filenames['image/jpeg'] | path2url }})
{% endblock data_jpg %}

{% block data_gif %}
![gif]({{ output.metadata.filenames['image/gif'] | path2url }})
{% endblock data_gif %}

{% block data_latex %}
{{ output.data['text/latex'] | convert_pandoc(from_format="latex", to_format="asciidoc")}}
{% endblock data_latex %}
Expand Down
3 changes: 3 additions & 0 deletions share/templates/base/display_priority.j2
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
{%- elif type == 'image/jpeg' -%}
{%- block data_jpg -%}
{%- endblock -%}
{%- elif type == 'image/gif' -%}
{%- block data_gif -%}
{%- endblock -%}
{%- elif type == 'text/plain' -%}
{%- block data_text -%}
{%- endblock -%}
Expand Down
26 changes: 26 additions & 0 deletions share/templates/classic/base.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,32 @@ alt="{{ alttext | escape_html }}"
</div>
{%- endblock data_jpg %}

{% block data_gif scoped %}
<div class="output_gif output_subarea {{ extra_class }}">
{%- if 'image/gif' in output.metadata.get('filenames', {}) %}
<img src="{{ output.metadata.filenames['image/gif'] | posix_path | escape_html }}"
{%- else %}
<img src="data:image/gif;base64,{{ output.data['image/gif'] | escape_html }}"
{%- endif %}
{%- set width=output | get_metadata('width', 'image/gif') -%}
{%- if width is not none %}
width={{ width | escape_html }}
{%- endif %}
{%- set height=output | get_metadata('height', 'image/gif') -%}
{%- if height is not none %}
height={{ height | escape_html }}
{%- endif %}
{%- if output | get_metadata('unconfined', 'image/gif') %}
class="unconfined"
{%- endif %}
{%- set alttext=(output | get_metadata('alt', 'image/gif')) or (cell | get_metadata('alt')) -%}
{%- if alttext is not none %}
alt="{{ alttext | escape_html }}"
{%- endif %}
>
</div>
{%- endblock data_gif %}

{% block data_latex scoped %}
<div class="output_latex output_subarea {{ extra_class }}">
{{ output.data['text/latex'] | e }}
Expand Down
30 changes: 30 additions & 0 deletions share/templates/lab/base.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,36 @@ jp-needs-dark-background
</div>
{%- endblock data_jpg %}

{% block data_gif scoped %}
<div class="jp-RenderedImage jp-OutputArea-output {{ extra_class }}">
{%- if 'image/gif' in output.metadata.get('filenames', {}) %}
<img src="{{ output.metadata.filenames['image/gif'] | posix_path | escape_html }}"
{%- else %}
<img src="data:image/gif;base64,{{ output.data['image/gif'] | escape_html }}"
{%- endif %}
{%- set width=output | get_metadata('width', 'image/gif') -%}
{%- if width is not none %}
width={{ width | escape_html }}
{%- endif %}
{%- set height=output | get_metadata('height', 'image/gif') -%}
{%- if height is not none %}
height={{ height | escape_html }}
{%- endif %}
class="
{%- if output | get_metadata('unconfined', 'image/gif') %}
unconfined
{%- endif %}
{%- if output | get_metadata('needs_background', 'image/gif') == 'light' %}
jp-needs-light-background
{%- endif %}
{%- if output | get_metadata('needs_background', 'image/gif') == 'dark' %}
jp-needs-dark-background
{%- endif %}
"
>
</div>
{%- endblock data_gif %}

{% block data_latex scoped %}
<div class="jp-RenderedLatex jp-OutputArea-output {{ extra_class }}" data-mime-type="text/latex">
{{ output.data['text/latex'] | e }}
Expand Down
8 changes: 8 additions & 0 deletions share/templates/markdown/index.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
{% endif %}
{% endblock data_jpg %}

{% block data_gif %}
{% if "filenames" in output.metadata %}
![gif]({{ output.metadata.filenames['image/gif'] | path2url }})
{% else %}
![gif](data:image/gif;base64,{{ output.data['image/gif'] }})
{% endif %}
{% endblock data_gif %}

{% block data_latex %}
{{ output.data['text/latex'] }}
{% endblock data_latex %}
Expand Down
12 changes: 12 additions & 0 deletions share/templates/rst/index.rst.j2
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@
{%- endif %}
{% endblock data_jpg %}

{% block data_gif %}
.. image:: {{ output.metadata.filenames['image/gif'] | urlencode }}
{%- set width=output | get_metadata('width', 'image/gif') -%}
{%- if width is not none %}
:width: {{ width }}px
{%- endif %}
{%- set height=output | get_metadata('height', 'image/gif') -%}
{%- if height is not none %}
:height: {{ height }}px
{%- endif %}
{% endblock data_gif %}

{% block data_markdown %}
{{ output.data['text/markdown'] | convert_pandoc("markdown", "rst") }}
{% endblock data_markdown %}
Expand Down
1 change: 1 addition & 0 deletions tests/preprocessors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def build_notebook(self, with_json_outputs=False, with_attachment=False):
nbformat.new_output("stream", name="stderr", text="f"),
nbformat.new_output("display_data", data={"image/png": "Zw=="}), # g
nbformat.new_output("display_data", data={"application/pdf": "aA=="}), # h
nbformat.new_output("display_data", data={"image/gif": "aQ=="}), # i
]
if with_json_outputs:
outputs.extend(
Expand Down
17 changes: 16 additions & 1 deletion tests/preprocessors/test_extractoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ class TestExtractOutput(PreprocessorTestsBase):
def build_preprocessor(self):
"""Make an instance of a preprocessor"""
preprocessor = ExtractOutputPreprocessor()
preprocessor.extract_output_types = {"text/plain", "image/png", "application/pdf"}
preprocessor.extract_output_types = {
"text/plain",
"image/png",
"application/pdf",
"image/gif",
}
preprocessor.enabled = True
return preprocessor

Expand Down Expand Up @@ -48,6 +53,12 @@ def test_output(self):
self.assertIn("application/pdf", output.metadata.filenames)
pdf_filename = output.metadata.filenames["application/pdf"]

# Check that gif was extracted as binary bytes (base64-decoded), not a string
output = nb.cells[0].outputs[8]
self.assertIn("filenames", output.metadata)
self.assertIn("image/gif", output.metadata.filenames)
gif_filename = output.metadata.filenames["image/gif"]

# Verify text output
self.assertIn(text_filename, res["outputs"])
self.assertEqual(res["outputs"][text_filename], b"b")
Expand All @@ -60,6 +71,10 @@ def test_output(self):
self.assertIn(pdf_filename, res["outputs"])
self.assertEqual(res["outputs"][pdf_filename], b"h")

# Verify gif output — must be bytes, not a raw base64 string
self.assertIn(gif_filename, res["outputs"])
self.assertEqual(res["outputs"][gif_filename], b"i")

def test_json_extraction(self):
nb = self.build_notebook(with_json_outputs=True)
res = self.build_resources()
Expand Down
2 changes: 1 addition & 1 deletion tests/preprocessors/test_tagremove.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ def test_output(self):
self.assertEqual(len(nb.cells[-1].outputs), 0)

# checks that we can remove individual outputs
self.assertEqual(len(nb.cells[0].outputs), 8)
self.assertEqual(len(nb.cells[0].outputs), 9)
Loading