Skip to content

Fix html repr in untrusted notebooks (plain text fallback) #4053

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

Merged
merged 4 commits into from
May 20, 2020
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
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ Bug fixes
- Fix bug in time parsing failing to fall back to cftime. This was causing time
variables with a time unit of `'msecs'` to fail to parse. (:pull:`3998`)
By `Ryan May <https://github.com/dopplershift>`_.
- Fix html repr in untrusted notebooks: fallback to plain text repr. (:pull:`4053`)
By `Benoit Bovy <https://github.com/benbovy>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
14 changes: 10 additions & 4 deletions xarray/core/formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,20 @@ def array_section(obj):
)


def _obj_repr(header_components, sections):
def _obj_repr(obj, header_components, sections):
"""Return HTML repr of an xarray object.

If CSS is not injected (untrusted notebook), fallback to the plain text repr.

"""
header = f"<div class='xr-header'>{''.join(h for h in header_components)}</div>"
sections = "".join(f"<li class='xr-section-item'>{s}</li>" for s in sections)

return (
"<div>"
f"{ICONS_SVG}<style>{CSS_STYLE}</style>"
"<div class='xr-wrap'>"
f"<pre class='xr-text-repr-fallback'>{escape(repr(obj))}</pre>"
"<div class='xr-wrap' hidden>"
f"{header}"
f"<ul class='xr-sections'>{sections}</ul>"
"</div>"
Expand Down Expand Up @@ -257,7 +263,7 @@ def array_repr(arr):

sections.append(attr_section(arr.attrs))

return _obj_repr(header_components, sections)
return _obj_repr(arr, header_components, sections)


def dataset_repr(ds):
Expand All @@ -272,4 +278,4 @@ def dataset_repr(ds):
attr_section(ds.attrs),
]

return _obj_repr(header_components, sections)
return _obj_repr(ds, header_components, sections)
6 changes: 6 additions & 0 deletions xarray/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ body.vscode-dark {
}

.xr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}

.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}

.xr-header {
padding-top: 6px;
padding-bottom: 6px;
Expand Down
2 changes: 0 additions & 2 deletions xarray/static/html/icons-svg-inline.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<title>Show/Hide data repr</title>
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<title>Show/Hide attributes</title>
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ def test_repr_of_dataset(dataset):
assert "&lt;IA&gt;" in formatted


def test_repr_text_fallback(dataset):
formatted = fh.dataset_repr(dataset)

# Just test that the "pre" block used for fallback to plain text is present.
assert "<pre class='xr-text-repr-fallback'>" in formatted


def test_variable_repr_html():
v = xr.Variable(["time", "x"], [[1, 2, 3], [4, 5, 6]], {"foo": "bar"})
assert hasattr(v, "_repr_html_")
Expand Down