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

html repr #78

Merged
merged 12 commits into from
May 4, 2022
Merged

html repr #78

merged 12 commits into from
May 4, 2022

Conversation

TomNicholas
Copy link
Member

My attempt at an arbitrarily-deeply nested html repr.

html_repr

That's for the test datatree, whose string repr currently looks like this:

DataTree('root', parent=None)
│   Dimensions:  (y: 3, x: 2)
│   Dimensions without coordinates: y, x
│   Data variables:
│       a        (y) int64 6 7 8
│       set0     (x) int64 9 10
├── DataTree('set1')
│   │   Dimensions:  ()
│   │   Data variables:
│   │       a        int64 0
│   │       b        int64 1
│   ├── DataTree('set1')
│   └── DataTree('set2')
├── DataTree('set2')
│   │   Dimensions:  (x: 2)
│   │   Dimensions without coordinates: x
│   │   Data variables:
│   │       a        (x) int64 2 3
│   │       b        (x) float64 0.1 0.2
│   └── DataTree('set1')
└── DataTree('set3')

I'm pretty pleased with this, but open to feedback.

Some things I think could be improved are:

  • Making the group names stand out more
  • Maybe listing groups after data variables?
  • It should also show if the root node has a parent (i.e. it's not really the root).

Not really sure how to test it exactly either.

  • Closes html repr #75
  • Tests added
  • Passes pre-commit run --all-files
  • New functions/methods are listed in api.rst

@jsignell you will probably be interested to see this - thanks for making the xarray html repr code so neat that I was able to easily extend it!

@rabernat @jbusecke opinions welcome too

@TomNicholas TomNicholas added the enhancement New feature or request label Apr 23, 2022
@jsignell
Copy link

Thanks for doing the ping @TomNicholas! Let me also ping @benbovy since he did the majority of the work on the xarray repr.

@jbusecke
Copy link
Contributor

Is there a way to indent everything but groups? That might help clarify that groups is the 'top level' organizer and everything beneath it is just another leaf-dataset? Unless I totally did not understand that right, hehe.

@TomNicholas
Copy link
Member Author

Is there a way to indent everything but groups? That might help clarify that groups is the 'top level' organizer and everything beneath it is just another leaf-dataset? Unless I totally did not understand that right, hehe.

I don't think that makes sense - Variables and sub-Groups exist at the same level within a given Group. In the topmost (i.e. root) group you can select a Variable via dt[var_name] or a group via dt[group_name]. So I don't think it would make sense to do what you're suggesting?

@jbusecke
Copy link
Contributor

jbusecke commented Apr 25, 2022

You might be right, my main reason to say that is that the output is quite large (long scroll) and visually very homogenous to look at. This can make it hard to find the thing you are looking for.
But actually there might be an easier way: Do you think its possible/useful to fold the full groups by default? So for example if you unfold 'Groups', you could have a list of set1/set2/... that can each be unfolded to get the datasets repr. I think this would make handling many groups easier, since the current way seems to end up being a very long scroll for 3+ groups?

Additionally some visual elements like in the dask client repr could help the user grasp the high level structure of the tree more easily. These rounded boxes (or something similar) could be color coded for e.g. (leaf, branch etc).

@TomNicholas
Copy link
Member Author

Do you think its possible/useful to fold the full groups by default?

This line should control how many items a group dropdown must have before it is folded by default. If we set it to 0 it should fold all groups be default.

Additionally some visual elements like in the dask client repr could help the user grasp the high level structure of the tree more easily. These rounded boxes (or something similar) could be color coded for e.g. (leaf, branch etc).

This is trickier, but I agree some kind of graphical representation would be nice. I do like the way the printable string repr shows you the structure. This is so much harder for me to implement though that I might leave it to a second PR.

@jbusecke
Copy link
Contributor

This is trickier, but I agree some kind of graphical representation would be nice. I do like the way the printable string repr shows you the structure. This is so much harder for me to implement though that I might leave it to a second PR.

Yeah absolutely, this was a 'nice to have' from my end. Not required. Root level folding seems very beneficial for large sets of groups, so that would be nice to implement here (and it seems quite easy).

@benbovy
Copy link
Member

benbovy commented Apr 26, 2022

Thanks for the ping @jsignell, and also glad to see that it was easy to reuse the xarray repr code here (with the hope that the hacks used there won't have too much impact here).

This is trickier, but I agree some kind of graphical representation would be nice. I do like the way the printable string repr shows you the structure. This is so much harder for me to implement though that I might leave it to a second PR.

I think this could be already achieved - although a bit less fancy than the dask repr - with some basic UI design applied to the group names like font-weight or color variation(s), or by prepending repeated symbol(s) (dot, cross, etc.) to those names so that it informs about their hierarchical level.

@dcherian
Copy link

This diff is impressively small!

@TomNicholas
Copy link
Member Author

TomNicholas commented Apr 26, 2022

by prepending repeated symbol(s) (dot, cross, etc.) to those names so that it informs about their hierarchical level.

Makes sense @benbovy - that is basically how anytree achieves this:

DataTree('root', parent=None)
│ Dimensions: (y: 3, x: 2)
│ Dimensions without coordinates: y, x
│ Data variables:
│ a (y) int64 6 7 8
│ set0 (x) int64 9 10
├── DataTree('set1')
│ │ Dimensions: ()
│ │ Data variables:
│ │ a int64 0
│ │ b int64 1
│ ├── DataTree('set1')
│ └── DataTree('set2')
├── DataTree('set2')
│ │ Dimensions: (x: 2)
│ │ Dimensions without coordinates: x
│ │ Data variables:
│ │ a (x) int64 2 3
│ │ b (x) float64 0.1 0.2
│ └── DataTree('set1')
└── DataTree('set3')

I imagine I could use similar logic to how anytree's RenderTree just uses 3 different characters (vertical=, continuation=├──, end=└──).

Comment on lines +24 to +26
"<ul class='xr-sections'>"
f"<div style='padding-left:2rem;'>{children_li}<br></div>"
"</ul>"
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm currently padding the sub-groups here to indent them, but I think this is where I would need to add lines instead.

@TomNicholas TomNicholas added this to the 0.1 milestone Apr 27, 2022
@TomNicholas
Copy link
Member Author

I'm going to merge this now and leave #87 up to discuss how to improve the html repr in the future.

@TomNicholas TomNicholas merged commit 42aa869 into xarray-contrib:main May 4, 2022
@TomNicholas TomNicholas deleted the html_repr branch May 4, 2022 22:43
@TomNicholas TomNicholas added the html repr HTML representation in a notebook label May 18, 2022
flamingbear pushed a commit to flamingbear/rewritten-datatree that referenced this pull request Jan 19, 2024
* html repr displays data in root group

* displays under name 'xarray.DataTree'

* creates a html repr for each sub-group, but it looks messed up

* correctly indents sub-groups

* show names of groups

* refactoring

* dodge type hinting bug

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix failing test in merg

* fix bug caused by merge

* whatsnew

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request html repr HTML representation in a notebook
Projects
None yet
Development

Successfully merging this pull request may close these issues.

html repr
5 participants