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

ENH: pass-through image metadata #588

Merged
merged 4 commits into from
Apr 12, 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
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">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand this image gets the height=400 metadata. However, it seems to end up being css instead of html attribute.

imatge

From the description and the xml here I expected to get <img ... height="400px"> instead of the <img ... style="height: 400px;"> that shows up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, seems like this one is the only one that should get that, weird that the others do, I’ll check that out.

Regarding CSS: That’s another issue, but it should definitely be fixed, I agree!

<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">
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
<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">
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
<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
Loading