Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: output metadata overwrites image size for all following images #609

Merged
merged 6 commits into from
Jun 27, 2024
Merged
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
2 changes: 1 addition & 1 deletion myst_nb/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def render_image(self, data: MimeData) -> list[nodes.Element]:
# TODO backwards-compatible re-naming to image_options?
image_options = self.renderer.get_cell_level_config(
"render_image_options", data.cell_metadata, line=data.line
)
).copy()
# Overwrite with metadata stored in output
image_options.update(
{
Expand Down
4 changes: 4 additions & 0 deletions tests/notebooks/complex_outputs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@
]
},
"metadata": {
"image/png": {
"width": 432,
"height": 288
},
"needs_background": "light"
},
"output_type": "display_data"
Expand Down
77 changes: 77 additions & 0 deletions tests/notebooks/metadata_image_output.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/test_parser/test_complex_outputs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="288" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png" width="432">
<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'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" 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
19 changes: 19 additions & 0 deletions tests/test_render_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from myst_nb.core.render import EntryPointError, load_renderer
from sphinx.util.fileutil import copy_asset_file


def test_load_renderer_not_found():
Expand Down Expand Up @@ -116,6 +117,24 @@ def test_metadata_image(sphinx_run, clean_doctree, file_regression):
)


@pytest.mark.sphinx_params(
"metadata_image_output.ipynb",
conf={"nb_execution_mode": "force"},
)
def test_metadata_image_output(
sphinx_run, clean_doctree, file_regression, get_test_path
):
"""Test configuring image attributes to be rendered from cell metadata."""
asset_path = get_test_path("example.jpg")
copy_asset_file(str(asset_path), str(sphinx_run.app.srcdir))
sphinx_run.build()
assert sphinx_run.warnings() == ""
doctree = clean_doctree(sphinx_run.get_resolved_doctree("metadata_image_output"))
file_regression.check(
doctree.pformat().replace(".jpeg", ".jpg"), extension=".xml", encoding="utf-8"
)


@pytest.mark.sphinx_params(
"metadata_figure.ipynb",
conf={"nb_execution_mode": "off", "nb_cell_metadata_key": "myst"},
Expand Down
4 changes: 2 additions & 2 deletions tests/test_render_outputs/test_complex_outputs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="288" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png" width="432">
<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'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" 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
4 changes: 2 additions & 2 deletions tests/test_render_outputs/test_complex_outputs_latex.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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'}" height="400" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png">
<image candidates="{'*': '_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png'}" height="288" uri="_build/jupyter_execute/16832f45917c1c9862c50f0948f64a498402d6ccde1f3a291da17f240797b160.png" width="432">
<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'}" height="400" uri="_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png">
<image candidates="{'*': '_build/jupyter_execute/8c43e5c8cccf697754876b7fec1b0a9b731d7900bb585e775a5fa326b4de8c5a.png'}" 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
21 changes: 21 additions & 0 deletions tests/test_render_outputs/test_metadata_image_output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<document source="metadata_image_output">
<section ids="output-metadata" names="output\ metadata">
<title>
Output metadata
<container cell_index="1" cell_metadata="{'tags': ['skip-execution']}" classes="cell tag_skip-execution" exec_count="1" nb_element="cell_code">
<container classes="cell_input" nb_element="cell_code_source">
<literal_block language="ipython3" linenos="False" xml:space="preserve">
# Outputs included with width/height in output metadata,
# cell is not executed
from IPython.display import Image
Image(filename="./example.jpg", width=500, height=100)
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" height="100" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg" width="500">
<container cell_index="2" cell_metadata="{}" classes="cell" exec_count="1" nb_element="cell_code">
<container classes="cell_input" nb_element="cell_code_source">
<literal_block language="ipython3" linenos="False" xml:space="preserve">
# No outputs, cell is executed, image should have original size (370, 254)
from IPython.display import Image
Image(filename="./example.jpg")
<container classes="cell_output" nb_element="cell_code_output">
<image candidates="{'*': '_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg'}" uri="_build/jupyter_execute/a4c9580c74dacf6f3316a3bd2e2a347933aa4463834dcf1bb8f20b4fcb476ae1.jpg">