Skip to content

indexes section in the HTML repr #7185

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 16 commits into from
Oct 19, 2022
Merged

indexes section in the HTML repr #7185

merged 16 commits into from
Oct 19, 2022

Conversation

keewis
Copy link
Collaborator

@keewis keewis commented Oct 18, 2022

To see the effect, try this:

import xarray as xr
from xarray.core.indexes import Index


class CustomIndex(Index):
    def __init__(self, names, options):
        self.names = names
        self.options = options
    
    @classmethod
    def from_variables(cls, variables, options):
        names = list(variables.keys())
        
        return cls(names, options)
    
    def __repr__(self):
        options = (
            {"names": repr(self.names)}
            | {str(k): str(v) for k, v in self.options.items()}
        )
        
        return f"CustomIndex({', '.join(k + '=' + v for k, v in options.items())})"

    def _repr_html_(self):
        header_row = "<tr><td>KDTree params</td></tr>"
        option_rows = [
            f"<tr><td>{option}</td><td>{value}</td></tr>"
            for option, value in self.options.items()
        ]
        return f"<left><table>{header_row}{''.join(option_rows)}</table></left>"

ds = xr.tutorial.open_dataset("rasm")
ds1 = ds.set_xindex(["xc", "yc"], CustomIndex, param1="a", param2="b")

with xr.set_options(display_style="text"):
    display(ds1)

with xr.set_options(display_style="html"):
    display(ds1)

The repr looks a bit strange because I've been borrowing the variable CSS classes. Edit: @benbovy fixed that for me

Also, the discussion about what _repr_inline_ should include from #7183 is relevant here as well.

Show the names of all corresponding coordinates in the same
item (separated by line return).
@benbovy
Copy link
Member

benbovy commented Oct 18, 2022

Thanks @keewis for opening this PR.

I added some commits (hope you don't mind) to fix the CSS. I also grouped the items in the indexes section by unique index with index coordinates separated by line return, so it looks like the coordinate section while the multi-coordinate indexes are clearly visible.

@keewis keewis changed the title first draft of the indexes section in the HTML repr indexes section in the HTML repr Oct 19, 2022
@keewis
Copy link
Collaborator Author

keewis commented Oct 19, 2022

I added some commits (hope you don't mind) to fix the CSS

thanks a lot for that, @benbovy, I wouldn't have been able to figure that out on my own since my knowledge of CSS is pretty limited.

@benbovy
Copy link
Member

benbovy commented Oct 19, 2022

LGTM, that's awesome! It will be super handy for quick debugging and experimenting with custom indexes.

@dcherian dcherian added the plan to merge Final call for comments label Oct 19, 2022
@keewis keewis merged commit 8f34b32 into pydata:main Oct 19, 2022
@keewis keewis deleted the indexes-html-repr branch October 19, 2022 21:12
@benbovy
Copy link
Member

benbovy commented Oct 20, 2022

🚀

@benbovy benbovy mentioned this pull request Oct 25, 2022
49 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments topic-html-repr
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants