Skip to content

Commit

Permalink
Fix image metadata (#588)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent 1efadf7 commit ac5f9b4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
8 changes: 8 additions & 0 deletions myst_nb/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,14 @@ def render_image(self, data: MimeData) -> list[nodes.Element]:
image_options = self.renderer.get_cell_level_config(
"render_image_options", data.cell_metadata, line=data.line
)
# Overwrite with metadata stored in output
image_options.update(
{
key: str(value)
for key, value in data.output_metadata.get(data.mime_type, {}).items()
if key in {"width", "height"}
}
)
for key, spec in [
("classes", options_spec.class_option),
("alt", options_spec.unchanged),
Expand Down
6 changes: 3 additions & 3 deletions tests/test_parser/test_complex_outputs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
<container classes="cell_output" nb_element="cell_code_output">
<container nb_element="mime_bundle">
<container mime_type="image/jpeg">
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" height="400" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
<container mime_type="text/plain">
<literal_block classes="output text_plain" language="myst-ansi" xml:space="preserve">
<IPython.core.display.Image object>
Expand All @@ -130,7 +130,7 @@
<container classes="cell_output" nb_element="cell_code_output">
<container nb_element="mime_bundle">
<container mime_type="image/png">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<container mime_type="text/plain">
<literal_block classes="output text_plain" language="myst-ansi" xml:space="preserve">
<Figure size 432x288 with 1 Axes>
Expand Down Expand Up @@ -244,7 +244,7 @@
<container classes="cell_output" nb_element="cell_code_output">
<container nb_element="mime_bundle">
<container mime_type="image/png">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<container mime_type="text/latex">
<math_block classes="output text_latex" nowrap="False" number="True" xml:space="preserve">
\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)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_render_outputs/test_complex_outputs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<literal_block language="ipython3" linenos="False" xml:space="preserve">
Image('example.jpg',height=400)
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" height="400" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
<section ids="displaying-a-plot-with-its-code" names="displaying\ a\ plot\ with\ its\ code">
<title>
Displaying a plot with its code
Expand All @@ -123,7 +123,7 @@
plt.ylabel(r'a y label with latex $\alpha$')
plt.legend();
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<section ids="tables-with-pandas" names="tables\ (with\ pandas)">
<title>
Tables (with pandas)
Expand Down Expand Up @@ -208,7 +208,7 @@
f = y(n)-2*y(n-1/sym.pi)-5*y(n-2)
sym.rsolve(f,y(n),[1,4])
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<container cell_index="25" cell_metadata="{}" classes="cell" exec_count="7" nb_element="cell_code">
<container classes="cell_input" nb_element="cell_code_source">
<literal_block language="ipython3" linenos="False" xml:space="preserve">
Expand Down
6 changes: 3 additions & 3 deletions tests/test_render_outputs/test_complex_outputs_latex.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<literal_block language="ipython3" linenos="False" xml:space="preserve">
Image('example.jpg',height=400)
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" height="400" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">
<section ids="displaying-a-plot-with-its-code" names="displaying\ a\ plot\ with\ its\ code">
<title>
Displaying a plot with its code
Expand All @@ -123,7 +123,7 @@
plt.ylabel(r'a y label with latex $\alpha$')
plt.legend();
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<section ids="tables-with-pandas" names="tables\ (with\ pandas)">
<title>
Tables (with pandas)
Expand Down Expand Up @@ -168,7 +168,7 @@
f = y(n)-2*y(n-1/sym.pi)-5*y(n-2)
sym.rsolve(f,y(n),[1,4])
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<container cell_index="25" cell_metadata="{}" classes="cell" exec_count="7" nb_element="cell_code">
<container classes="cell_input" nb_element="cell_code_source">
<literal_block language="ipython3" linenos="False" xml:space="preserve">
Expand Down

0 comments on commit ac5f9b4

Please sign in to comment.