Skip to content
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
31 changes: 21 additions & 10 deletions branca/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,20 @@ class Figure(Element):
A percentage defining the aspect ratio of the Figure.
It will be ignored if height is not None.
title : str, default None
Figure title.
Figure title. Also used as the accessible name of the iframe when the
Figure is displayed in a notebook.
figsize : tuple of two int, default None
If you're a matplotlib addict, you can overwrite width and
height. Values will be converted into pixels in using 60 dpi.
For example figsize=(10, 5) will result in
width="600px", height="300px".
lang : str, default "en"
Language of the generated HTML document.
"""

_template = Template(
"<!DOCTYPE html>\n"
"<html>\n"
'<html lang="{{this.lang}}">\n'
"<head>\n"
"{% if this.title %}<title>{{this.title}}</title>{% endif %}"
" {{this.header.render(**kwargs)}}\n"
Expand All @@ -359,6 +362,7 @@ def __init__(
ratio: str = "60%",
title: Optional[str] = None,
figsize: Optional[Tuple[int, int]] = None,
lang: str = "en",
):
super().__init__()
self._name = "Figure"
Expand All @@ -374,6 +378,7 @@ def __init__(
self.height = height
self.ratio = ratio
self.title = title
self.lang = lang
if figsize is not None:
self.width = str(60 * figsize[0]) + "px"
self.height = str(60 * figsize[1]) + "px"
Expand Down Expand Up @@ -412,24 +417,25 @@ def render(self, **kwargs) -> str:
def _repr_html_(self, **kwargs) -> str:
"""Displays the Figure in a Jupyter notebook."""
html = escape(self.render(**kwargs))
title = f' title="{escape(self.title, quote=True)}"' if self.title else ""
if self.height is None:
iframe = (
'<div style="width:{width};">'
'<div style="position:relative;width:100%;height:0;padding-bottom:{ratio};">' # noqa
'<span style="color:#565656">Make this Notebook Trusted to load map: File -> Trust Notebook</span>' # noqa
'<iframe srcdoc="{html}" style="position:absolute;width:100%;height:100%;left:0;top:0;' # noqa
'<iframe srcdoc="{html}"{title} style="position:absolute;width:100%;height:100%;left:0;top:0;' # noqa
'border:none !important;" '
"allowfullscreen webkitallowfullscreen mozallowfullscreen>"
"</iframe>"
"</div></div>"
).format(html=html, width=self.width, ratio=self.ratio)
).format(html=html, title=title, width=self.width, ratio=self.ratio)
else:
iframe = (
'<iframe srcdoc="{html}" width="{width}" height="{height}"'
'<iframe srcdoc="{html}"{title} width="{width}" height="{height}"'
'style="border:none !important;" '
'"allowfullscreen" "webkitallowfullscreen" "mozallowfullscreen">'
"</iframe>"
).format(html=html, width=self.width, height=self.height)
).format(html=html, title=title, width=self.width, height=self.height)
return iframe

def add_subplot(self, x: int, y: int, n: int, margin: float = 0.05) -> "Div":
Expand Down Expand Up @@ -640,6 +646,8 @@ class IFrame(Element):
height. Values will be converted into pixels in using 60 dpi.
For example figsize=(10, 5) will result in
width="600px", height="300px".
title : str, default None
Accessible name for the iframe.
"""

def __init__(
Expand All @@ -649,13 +657,15 @@ def __init__(
height: Optional[str] = None,
ratio: str = "60%",
figsize: Optional[Tuple[int, int]] = None,
title: Optional[str] = None,
):
super().__init__()
self._name = "IFrame"

self.width = width
self.height = height
self.ratio = ratio
self.title = title
if figsize is not None:
self.width = str(60 * figsize[0]) + "px"
self.height = str(60 * figsize[1]) + "px"
Expand All @@ -671,21 +681,22 @@ def render(self, **kwargs) -> str:
html = "data:text/html;charset=utf-8;base64," + base64.b64encode(
html.encode("utf8"),
).decode("utf8")
title = f' title="{escape(self.title, quote=True)}"' if self.title else ""

if self.height is None:
iframe = (
'<div style="width:{width};">'
'<div style="position:relative;width:100%;height:0;padding-bottom:{ratio};">' # noqa
'<iframe src="{html}" style="position:absolute;width:100%;height:100%;left:0;top:0;' # noqa
'<iframe src="{html}"{title} style="position:absolute;width:100%;height:100%;left:0;top:0;' # noqa
'border:none !important;">'
"</iframe>"
"</div></div>"
).format(html=html, width=self.width, ratio=self.ratio)
).format(html=html, title=title, width=self.width, ratio=self.ratio)
else:
iframe = (
'<iframe src="{html}" width="{width}" style="border:none !important;" '
'<iframe src="{html}"{title} width="{width}" style="border:none !important;" '
'height="{height}"></iframe>'
).format(html=html, width=self.width, height=self.height)
).format(html=html, title=title, width=self.width, height=self.height)
return iframe


Expand Down
24 changes: 24 additions & 0 deletions tests/test_accessibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Tests for accessible HTML attributes."""

import branca.element as elem


def test_iframe_title_is_escaped():
iframe = elem.IFrame(
html="<p>test content<p>",
width=60,
height=45,
title='Test "content"',
)

assert 'title="Test &quot;content&quot;"' in iframe.render()


def test_figure_accessibility_attributes():
figure = elem.Figure(title='Map of "places"', lang="fr")

document = figure.render()
notebook = figure._repr_html_()

assert '<html lang="fr">' in document
assert 'title="Map of &quot;places&quot;"' in notebook