diff --git a/branca/element.py b/branca/element.py
index 853ca16..d1356a9 100644
--- a/branca/element.py
+++ b/branca/element.py
@@ -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(
"\n"
- "\n"
+ '\n'
"
\n"
"{% if this.title %}{{this.title}}{% endif %}"
" {{this.header.render(**kwargs)}}\n"
@@ -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"
@@ -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"
@@ -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 = (
''
'
' # noqa
'Make this Notebook Trusted to load map: File -> Trust Notebook' # noqa
- '"
"
"
- ).format(html=html, width=self.width, ratio=self.ratio)
+ ).format(html=html, title=title, width=self.width, ratio=self.ratio)
else:
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":
@@ -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__(
@@ -649,6 +657,7 @@ def __init__(
height: Optional[str] = None,
ratio: str = "60%",
figsize: Optional[Tuple[int, int]] = None,
+ title: Optional[str] = None,
):
super().__init__()
self._name = "IFrame"
@@ -656,6 +665,7 @@ def __init__(
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"
@@ -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 = (
'"
- ).format(html=html, width=self.width, ratio=self.ratio)
+ ).format(html=html, title=title, width=self.width, ratio=self.ratio)
else:
iframe = (
- ''
- ).format(html=html, width=self.width, height=self.height)
+ ).format(html=html, title=title, width=self.width, height=self.height)
return iframe
diff --git a/tests/test_accessibility.py b/tests/test_accessibility.py
new file mode 100644
index 0000000..2d5bc1d
--- /dev/null
+++ b/tests/test_accessibility.py
@@ -0,0 +1,24 @@
+"""Tests for accessible HTML attributes."""
+
+import branca.element as elem
+
+
+def test_iframe_title_is_escaped():
+ iframe = elem.IFrame(
+ html="test content
",
+ width=60,
+ height=45,
+ title='Test "content"',
+ )
+
+ assert 'title="Test "content""' 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 '' in document
+ assert 'title="Map of "places""' in notebook