Skip to content

limit lines in html repr of dataset attrs #7653

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions xarray/core/formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,20 @@ def format_dims(dims, dims_with_index):
return f"<ul class='xr-dim-list'>{dims_li}</ul>"


def summarize_attrs(attrs):
def summarize_attrs(attrs, max_lines=15, min_lines=10):
def attr_cropper(obj):
if type(obj) is str:
lines = str.splitlines(obj)
if len(lines) > max_lines:
return (
escape("\n".join(lines[: min_lines // 2]))
+ f"\n<i>... (skipped {len(lines) - min_lines} lines)</i>\n"
+ escape("\n".join(lines[-min_lines // 2 :]))
)
return escape(str(obj))

attrs_dl = "".join(
f"<dt><span>{escape(str(k))} :</span></dt>" f"<dd>{escape(str(v))}</dd>"
f"<dt><span>{escape(str(k))} :</span></dt>" f"<dd>{attr_cropper(v)}</dd>"
for k, v in attrs.items()
)

Expand Down