From 5ca4146549f1f67ce8d50ba65b4e98f05af75765 Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Tue, 12 Apr 2022 19:42:28 -0400 Subject: [PATCH 1/2] Add "alt" to known image metadata keys --- src/nbsphinx.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/nbsphinx.py b/src/nbsphinx.py index dc2d03d5..97476fb7 100644 --- a/src/nbsphinx.py +++ b/src/nbsphinx.py @@ -181,7 +181,11 @@ {%- set height = output.metadata[datatype].height %} {%- if height %} :height: {{ height }} -{% endif %} +{%- endif %} +{%- set alt = output.metadata[datatype].alt %} +{%- if alt %} + :alt: {{ alt }} +{%- endif %} {% endif %} {%- elif datatype in ['text/markdown'] %} From 1924ce169190ab7dd79ace5b95d6b7b8840d34d1 Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Tue, 12 Apr 2022 19:42:53 -0400 Subject: [PATCH 2/2] DOC: example of width/height/alt metadata --- doc/code-cells.ipynb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/code-cells.ipynb b/doc/code-cells.ipynb index 1d550e22..b4453641 100644 --- a/doc/code-cells.ipynb +++ b/doc/code-cells.ipynb @@ -444,6 +444,32 @@ "fig" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `width`, `height`, and `alt` attributes of the eventual HTML output can be set via cell metadata corresponding to the image format like so:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%config InlineBackend.figure_formats = ['png']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(fig, metadata={\"image/png\": {\"width\": \"150px\", \"height\": \"100px\",\n", + " \"alt\": \"this is my alt text\"}})" + ] + }, { "cell_type": "markdown", "metadata": {},