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 options to control expand/collapse of sections in display of Dataset and DataArray #5126

Merged
merged 5 commits into from
Apr 27, 2021

Conversation

tomwhite
Copy link
Contributor

@tomwhite tomwhite commented Apr 7, 2021

@dcherian
Copy link
Contributor

Thanks @tomwhite !

I don't know this part of the code at all but maybe @jsignell or @benbovy can take a look.

Copy link
Contributor

@jsignell jsignell left a comment

Choose a reason for hiding this comment

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

This seems like a nice complete solution to me! I just found the one typo :)

xarray/core/options.py Outdated Show resolved Hide resolved
@tomwhite
Copy link
Contributor Author

Thanks for the review @jsignell!

@max-sixty
Copy link
Collaborator

@tomwhite would you like to add a note to whatsnew, and then we can merge?

@tomwhite
Copy link
Contributor Author

@max-sixty yes - I've rebased and updated whats-new.

@max-sixty
Copy link
Collaborator

Great — looks like one small linting issue, and then ping when green and we can hit the big button

@keewis
Copy link
Collaborator

keewis commented Apr 27, 2021

that linting issue is was on master, too (there's a new version of black), so we might not have to actually fix it here.

@max-sixty
Copy link
Collaborator

Thanks @keewis . I'll merge this and then #5220 on green too

@max-sixty max-sixty merged commit f3d0ec5 into pydata:master Apr 27, 2021
@max-sixty
Copy link
Collaborator

Thanks for the PR @tomwhite , we're happy to have you as a contributor!

@tomwhite
Copy link
Contributor Author

Thank you!

if _get_boolean_with_default("display_expand_data", default=True):
data_repr = short_data_repr(arr)
else:
data_repr = inline_variable_array_repr(arr, OPTIONS["display_width"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

this raises for

ds = xr.tutorial.open_dataset("rasm")
with xr.set_options(display_expand_data=False):
    display(ds.Tair)

i.e. with the data being a MemoryCachedArray. Should this be something like

Suggested change
data_repr = inline_variable_array_repr(arr, OPTIONS["display_width"])
data_repr = inline_variable_array_repr(arr.variable, OPTIONS["display_width"])

or should we let display_expand_data have no effect for MemoryCachedArray (the expanded repr is already really small)? The difference would be: [2029500 values with dtype=float64] for display_expand_data=True vs ... for display_expand_data=False

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks a lot for spotting @keewis .

No strong view on which of those we go ahead with — @tomwhite are you familiar with this? Otherwise @keewis do you want to make a call?

@tomwhite how would you feel about following up your first PR with a second? :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's more helpful to always have the expanded repr, but I'm not sure how complicated that special-case would be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for finding this @keewis. I can put together a PR - I'm just trying to find a simple test case that exposes the problem (the tests don't currently use the tutorial as far as I can tell).

Copy link
Collaborator

Choose a reason for hiding this comment

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

this should work with any opened file, so the test case should use either open_dataset or open_dataarray (creating the MemoryCachedArray by hand would also work but might be a bit more difficult). Something like

@requires_netCDF4
def test_repr_memory_cached_collapsed(temp_path):
    arr = xr.DataArray([0, 1], dims="x")
    arr.to_netcdf(temp_path, engine="netcdf4")

    with xr.open_dataarray(temp_path) as arr, xr.set_options(display_expand_data=True):
        # check the result of repr
        ...

might work? (temp_path should be a builtin pytest fixture to create a temporary file and return the path, but I'm not sure I got the name right)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've opened #5235 with a possible fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: Provide option for collapsing the HTML display in notebooks
5 participants