Skip to content

Commit ecb3b6e

Browse files
aeisenbarthagoose77flying-sheep
authored
FIX: output metadata overwrites image size for all following images (#609)
* Add test case for image with output metadata and image without * Make a copy of cell config before modifying * Fix image metadata --------- Co-authored-by: Angus Hollands <[email protected]> Co-authored-by: Philipp A. <[email protected]>
1 parent 8cae00b commit ecb3b6e

8 files changed

+128
-7
lines changed

myst_nb/core/render.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ def render_image(self, data: MimeData) -> list[nodes.Element]:
660660
# TODO backwards-compatible re-naming to image_options?
661661
image_options = self.renderer.get_cell_level_config(
662662
"render_image_options", data.cell_metadata, line=data.line
663-
)
663+
).copy()
664664
# Overwrite with metadata stored in output
665665
image_options.update(
666666
{

tests/notebooks/complex_outputs.ipynb

+4
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@
280280
]
281281
},
282282
"metadata": {
283+
"image/png": {
284+
"width": 432,
285+
"height": 288
286+
},
283287
"needs_background": "light"
284288
},
285289
"output_type": "display_data"

tests/notebooks/metadata_image_output.ipynb

+77
Large diffs are not rendered by default.

tests/test_parser/test_complex_outputs.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
<container classes="cell_output" nb_element="cell_code_output">
131131
<container nb_element="mime_bundle">
132132
<container mime_type="image/png">
133-
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
133+
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="288" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png" width="432">
134134
<container mime_type="text/plain">
135135
<literal_block classes="output text_plain" language="myst-ansi" xml:space="preserve">
136136
<Figure size 432x288 with 1 Axes>
@@ -244,7 +244,7 @@
244244
<container classes="cell_output" nb_element="cell_code_output">
245245
<container nb_element="mime_bundle">
246246
<container mime_type="image/png">
247-
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
247+
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
248248
<container mime_type="text/latex">
249249
<math_block classes="output text_latex" nowrap="False" number="True" xml:space="preserve">
250250
\displaystyle \left(\sqrt{5} i\right)^{\alpha} \left(\frac{1}{2} - \frac{2 \sqrt{5} i}{5}\right) + \left(- \sqrt{5} i\right)^{\alpha} \left(\frac{1}{2} + \frac{2 \sqrt{5} i}{5}\right)

tests/test_render_outputs.py

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33

44
from myst_nb.core.render import EntryPointError, load_renderer
5+
from sphinx.util.fileutil import copy_asset_file
56

67

78
def test_load_renderer_not_found():
@@ -116,6 +117,24 @@ def test_metadata_image(sphinx_run, clean_doctree, file_regression):
116117
)
117118

118119

120+
@pytest.mark.sphinx_params(
121+
"metadata_image_output.ipynb",
122+
conf={"nb_execution_mode": "force"},
123+
)
124+
def test_metadata_image_output(
125+
sphinx_run, clean_doctree, file_regression, get_test_path
126+
):
127+
"""Test configuring image attributes to be rendered from cell metadata."""
128+
asset_path = get_test_path("example.jpg")
129+
copy_asset_file(str(asset_path), str(sphinx_run.app.srcdir))
130+
sphinx_run.build()
131+
assert sphinx_run.warnings() == ""
132+
doctree = clean_doctree(sphinx_run.get_resolved_doctree("metadata_image_output"))
133+
file_regression.check(
134+
doctree.pformat().replace(".jpeg", ".jpg"), extension=".xml", encoding="utf-8"
135+
)
136+
137+
119138
@pytest.mark.sphinx_params(
120139
"metadata_figure.ipynb",
121140
conf={"nb_execution_mode": "off", "nb_cell_metadata_key": "myst"},

tests/test_render_outputs/test_complex_outputs.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
plt.ylabel(r'a y label with latex $\alpha$')
124124
plt.legend();
125125
<container classes="cell_output" nb_element="cell_code_output">
126-
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
126+
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="288" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png" width="432">
127127
<section ids="tables-with-pandas" names="tables\ (with\ pandas)">
128128
<title>
129129
Tables (with pandas)
@@ -208,7 +208,7 @@
208208
f = y(n)-2*y(n-1/sym.pi)-5*y(n-2)
209209
sym.rsolve(f,y(n),[1,4])
210210
<container classes="cell_output" nb_element="cell_code_output">
211-
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
211+
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
212212
<container cell_index="25" cell_metadata="{}" classes="cell" exec_count="7" nb_element="cell_code">
213213
<container classes="cell_input" nb_element="cell_code_source">
214214
<literal_block language="ipython3" linenos="False" xml:space="preserve">

tests/test_render_outputs/test_complex_outputs_latex.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
plt.ylabel(r'a y label with latex $\alpha$')
124124
plt.legend();
125125
<container classes="cell_output" nb_element="cell_code_output">
126-
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
126+
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="288" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png" width="432">
127127
<section ids="tables-with-pandas" names="tables\ (with\ pandas)">
128128
<title>
129129
Tables (with pandas)
@@ -168,7 +168,7 @@
168168
f = y(n)-2*y(n-1/sym.pi)-5*y(n-2)
169169
sym.rsolve(f,y(n),[1,4])
170170
<container classes="cell_output" nb_element="cell_code_output">
171-
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
171+
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
172172
<container cell_index="25" cell_metadata="{}" classes="cell" exec_count="7" nb_element="cell_code">
173173
<container classes="cell_input" nb_element="cell_code_source">
174174
<literal_block language="ipython3" linenos="False" xml:space="preserve">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<document source="metadata_image_output">
2+
<section ids="output-metadata" names="output\ metadata">
3+
<title>
4+
Output metadata
5+
<container cell_index="1" cell_metadata="{'tags': ['skip-execution']}" classes="cell tag_skip-execution" exec_count="1" nb_element="cell_code">
6+
<container classes="cell_input" nb_element="cell_code_source">
7+
<literal_block language="ipython3" linenos="False" xml:space="preserve">
8+
# Outputs included with width/height in output metadata,
9+
# cell is not executed
10+
from IPython.display import Image
11+
Image(filename="./example.jpg", width=500, height=100)
12+
<container classes="cell_output" nb_element="cell_code_output">
13+
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" height="100" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg" width="500">
14+
<container cell_index="2" cell_metadata="{}" classes="cell" exec_count="1" nb_element="cell_code">
15+
<container classes="cell_input" nb_element="cell_code_source">
16+
<literal_block language="ipython3" linenos="False" xml:space="preserve">
17+
# No outputs, cell is executed, image should have original size (370, 254)
18+
from IPython.display import Image
19+
Image(filename="./example.jpg")
20+
<container classes="cell_output" nb_element="cell_code_output">
21+
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">

0 commit comments

Comments
 (0)