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

Add "alt" to supported image metadata keys #646

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions doc/code-cells.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Member

Choose a reason for hiding this comment

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

In JupyterLab, it looks like those values have to be integers, not strings with "px". Otherwise they have no effect.
Also, if both are given, height seems to be ignored.

In the nbsphinx output that's different.

" \"alt\": \"this is my alt text\"}})"
Copy link
Member

Choose a reason for hiding this comment

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

The alt attribute doesn't seem to be supported by JupyterLab, or am I doing something wrong?

]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
6 changes: 5 additions & 1 deletion src/nbsphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Copy link
Author

Choose a reason for hiding this comment

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

Please verify that what I've done here with {%- vs {% is appropriate -- I'm not 100% sure about the effect on whitespace in the intermediate RST.

{% endif %}
{%- elif datatype in ['text/markdown'] %}

Expand Down