-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Display nested attribute dictionaries collapsibly in notebook output #10663
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
base: main
Are you sure you want to change the base?
Display nested attribute dictionaries collapsibly in notebook output #10663
Conversation
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
Thanks Thomas for sharing! My initial thought is that this feels a little complex to add into Xarray for a relatively niche use-case (sorry!). In particular, new CSS gets added to the saved output of every Xarray object, whether it uses dict attributes or not. One small fix that might help a bit for your use-case is to is clip long attributes in the HTML repr at some max line length, similar to what we already do in the text repr:
Then at least Xarray's HTML repr would not take up the entire screen. |
Hi @shoyer thanks for taking a look. We were trying to come up with the most general form of an improved repr that would still work for us, but it may be that this is still too niche. Given the "do not subclass" recommendation, is there any recommended way to implement a custom I ran across the same question here, but it doesn't seem like it went anywhere. As far as CSS bloat, this PR adds 702 bytes to an 8048 byte stylesheet. If CSS file size is the main concern, I think it would likely be possible to cut that down more by re-using rules from existing collapsible elements. I'm happy to take a shot at that if it would make a difference, but I suspect the issue here is more about if this functionality is generally useful enough. Please tell me if I'm wrong, of course. |
Thinking about this is a little more, I am coming around to the idea that expandable HTML reprs of attribute values could be broadly useful. The structure would look something like:
I can see a case for implementing default HTML reprs for built-in Python objects like There's some related discussion here about nested inline reprs here: #4324 |
That generally seems reasonable. I took a try at implementing a quick version of what I think you're describing. The behavior is: For each attribute, we look for a Here's an example with a test dask array subclass: ![]() All of the special casing makes the code quite a bit messier, but I suppose that's the tradeoff for a more general solution. I added a test notebook you can try running if you want to play with a live version. |
In general I am pro more interactive and rich displays so this is a welcome direction! I'm not ready to comment on generalization and protocols yet (but think I have thoughts). My quick note on a possible failure mode of this PR is that there are some pathological dictionaries that this display will need to protect against. For example this code gives a recursion error: # /// script
# requires-python = ">=3.12"
# dependencies = [
# "xarray @ git+https://github.com/thomasteisberg/xarray.git@html-repr-nested-dictionary",
# ]
# ///
import xarray as xr
a = dict()
a['b']=a
ds = xr.Dataset(
{},
attrs={
"a":a,
}
)
ds |
That's a good point @ianhi. One option would be to include a maximum recursion depth for dictionaries. Looking forward to seeing your thoughts on the rest of it. |
This change allows nested dictionaries within Dataset attributes to be displayed as collapsible sections in notebook output (
_repr_html_
).I'm working on a project that produces xarray Datasets with large nested attribute dictionaries. I couldn't find any way of overriding the
_repr_html_
output without subclassing Dataset. We've temporarily implemented an accessor as a work-around, but I'm hoping this change in display format is generally useful enough to make it into xarray.I'd appreciate any feedback on this display format and if xarray would be interested in incorporating something like this.
Minimal example
Before:
After:
There's an example of how we're using this (currently through an accessor) here: https://www.thomasteisberg.com/xopr/demo-notebook/