Skip to content

Commit ac5f9b4

Browse files
flying-sheeppre-commit-ci[bot]agoose77
authored
Fix image metadata (#588)
* Fix image metadata * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * update remaining --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Angus Hollands <goosey15@gmail.com>
1 parent 1efadf7 commit ac5f9b4

4 files changed

Lines changed: 17 additions & 9 deletions

File tree

myst_nb/core/render.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,14 @@ def render_image(self, data: MimeData) -> list[nodes.Element]:
661661
image_options = self.renderer.get_cell_level_config(
662662
"render_image_options", data.cell_metadata, line=data.line
663663
)
664+
# Overwrite with metadata stored in output
665+
image_options.update(
666+
{
667+
key: str(value)
668+
for key, value in data.output_metadata.get(data.mime_type, {}).items()
669+
if key in {"width", "height"}
670+
}
671+
)
664672
for key, spec in [
665673
("classes", options_spec.class_option),
666674
("alt", options_spec.unchanged),

tests/test_parser/test_complex_outputs.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
<container classes="cell_output" nb_element="cell_code_output">
110110
<container nb_element="mime_bundle">
111111
<container mime_type="image/jpeg">
112-
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
112+
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" height="400" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
113113
<container mime_type="text/plain">
114114
<literal_block classes="output text_plain" language="myst-ansi" xml:space="preserve">
115115
<IPython.core.display.Image object>
@@ -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'}" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
133+
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
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'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
247+
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" 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/test_complex_outputs.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
<literal_block language="ipython3" linenos="False" xml:space="preserve">
108108
Image('example.jpg',height=400)
109109
<container classes="cell_output" nb_element="cell_code_output">
110-
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
110+
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" height="400" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
111111
<section ids="displaying-a-plot-with-its-code" names="displaying\ a\ plot\ with\ its\ code">
112112
<title>
113113
Displaying a plot with its code
@@ -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'}" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
126+
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
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'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
211+
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" 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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
<literal_block language="ipython3" linenos="False" xml:space="preserve">
108108
Image('example.jpg',height=400)
109109
<container classes="cell_output" nb_element="cell_code_output">
110-
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
110+
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" height="400" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
111111
<section ids="displaying-a-plot-with-its-code" names="displaying\ a\ plot\ with\ its\ code">
112112
<title>
113113
Displaying a plot with its code
@@ -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'}" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
126+
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
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'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
171+
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" 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">

0 commit comments

Comments
 (0)