-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 #3425
Html repr #3425
Conversation
I don't really like how the |
Honestly this is looking super good and ready to merge already! Thanks @jsignell The only thing that looks funny to me is the alternating black-gray text coloring below: |
Yeah I agree that is a strange part of the original PR. The version in the fiddle doesn't do that, so I'd really like to hear back from @benbovy about whether the fiddle css is more uptodate. |
Julia, it's fantastic to see you working on this! Thanks so much! |
xarray/core/formatting_html.py
Outdated
d['id'] = 'section-' + str(uuid.uuid4()) | ||
|
||
# TODO: no value preview if not in memory | ||
d['preview'] = format_values_preview(obj.values, max_char=70) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same concern as above
This is really nice @jsignell !
We did a lot of experiments, even some after starting the implementation in #1820, so I admit this is quite messy. I also did some clean-up and tweaks directly in that PR, so unfortunately there is no fiddle of reference that exactly corresponds to what's in here. Sorry for that! The html/css in this PR is the cleanest one, I think. The latest fiddles (for
In the HTML repr, the width of the space character is less large that for the monospace font used in the text repr, so I originally used this trick to better distinguish between the values in the inline data repr. Now that each value is encapsulated in it's own |
Yeah the data preview is also shifted vertically. |
I just updated the gist with these changes:
|
Is there ever a situation in which dimensions would be expandable? It seems like it is hard-coded to collapsed. I am trying to generalize the css to reduce the length. |
I don't think it would ever be expanded. The collapsed icon is just there for visual consistency. |
Ok I think I've addressed all the comments. I just updated the gist with the latest output. Tomorrow I'll start adding tests. The css is now ~300 lines and it gets injected everytime the dataset is rendered. I'm not sure if that is considered too long and if it is I don't quite know what to do about it. |
You could consider running the CSS through some sort of preprocessing tool to minify it, e.g., to remove all the spaces. But my guess is that it's probably fine. It would be informative to measure the size of the output HTML encoded as UTF-8 bytes for some example datasets. For context, a simple line chart from matplotlib is ~20 KB when saved as a PNG image. Anything in that ball-park would be OK, up to perhaps a few hundred KB for really big datasets with lots of variables. |
xarray/core/common.py
Outdated
@@ -134,6 +134,12 @@ def __array__(self: Any, dtype: DTypeLike = None) -> np.ndarray: | |||
def __repr__(self) -> str: | |||
return formatting.array_repr(self) | |||
|
|||
def _repr_html_(self): | |||
if OPTIONS["display_style"] == "classic": | |||
classic = repr(self).replace("<", "<").replace(">", ">") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use html.escape
here: https://docs.python.org/3/library/html.html#html.escape
xarray/core/common.py
Outdated
def _repr_html_(self): | ||
if OPTIONS["display_style"] == "classic": | ||
classic = repr(self).replace("<", "<").replace(">", ">") | ||
return "<pre>{repr}</pre>".format(repr=classic) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use an f-string here:
return "<pre>{repr}</pre>".format(repr=classic) | |
return f"<pre>{classic}</pre>" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realize that xarray was already supporting >=3.6 👍
We probably should be careful to use |
Codecov Report
@@ Coverage Diff @@
## master #3425 +/- ##
=========================================
- Coverage 96.16% 95.06% -1.1%
=========================================
Files 65 66 +1
Lines 13436 13587 +151
=========================================
- Hits 12921 12917 -4
- Misses 515 670 +155
Continue to review full report at Codecov.
|
I'm still worried about size, but minifying the css and even minifying the html did not make a big difference. It looks like the html_repr for rasm is 38kb . |
How are you measuring this? I see:
|
|
Ok I am going to write some tests now. I updated the gist. Do people like the names "classic" and "html" for the display_style options? Another option would be "classic" and "rich". |
Or maybe "text" and "html"? Hopefully "html" will eventually become the classic display :) |
I added some tests. I can add more if anyone has a test case that they want to be sure is covered. |
Ok I think it's fixed now |
Yes it looks nice! |
It isn't too hard to get it working with dark theme, but requires slight tweaks to the colors to align with those provided as vars by jlab. |
Awesome. Thanks @jsignell Let's leave dark theme compatibility for a future PR. |
Should we expand the |
Thanks a lot @jsignell ! It is great to see this merged finally! |
Thanks @jsignell (and all). I'm really jazzed about this. |
this is awesome! I was so excited about it that I updated my little blog post with the latest xarray master https://predictablynoisy.com/xarray-explore-ieeg I was a little sad at the gigantic repr that came out of my DataArray, and now it is nice and tidy html! |
Cool! I guess this post about how you use notebooks in your blog is still up to date? I looks like we'll need to fix a couple of issues (the Besides variable names, we probably need to somehow handle overflow for variables that have a long list of dimension labels. |
Actually, no that's totally out of date now haha. Thanks for reminding me I should update that edit: realized I should have said what I'm actually using - I'm using this:https://jupyterbook.org/features/page.html |
I am opening a PR. I didn't escape dtypes and this dtype is |
To verify proper escaping, we could validate the generated HTML as XML: This could be a good use case for Hypothesis. Or you could manually build a dataset with variables of every dtype. |
I opened a PR and included a small test. It can probably go in while we work on a more complete testing infrastructure. |
These are handled in the same way as long variable names, they truncate with an ellipsis and then expand on hover. |
Oh yes I missed it. |
* upstream/master: Escaping dtypes (pydata#3444) Html repr (pydata#3425)
…e-multiple-dims * upstream/master: change ALL_DIMS to equal ellipsis (pydata#3418) Escaping dtypes (pydata#3444) Html repr (pydata#3425)
* upstream/master: Another groupby.reduce bugfix. (pydata#3403) add icomoon license (pydata#3448) change ALL_DIMS to equal ellipsis (pydata#3418) Escaping dtypes (pydata#3444) Html repr (pydata#3425)
* upstream/master: upgrade black verison to 19.10b0 (pydata#3456) Remove outdated code related to compatibility with netcdftime (pydata#3450) Remove deprecated behavior from dataset.drop docstring (pydata#3451) jupyterlab dark theme (pydata#3443) Drop groups associated with nans in group variable (pydata#3406) Allow ellipsis (...) in transpose (pydata#3421) Another groupby.reduce bugfix. (pydata#3403) add icomoon license (pydata#3448) change ALL_DIMS to equal ellipsis (pydata#3418) Escaping dtypes (pydata#3444) Html repr (pydata#3425)
commit bc39877 Merge: 507b1f6 278d2e6 Author: dcherian <deepak@cherian.net> Date: Tue Oct 29 09:36:30 2019 -0600 Merge remote-tracking branch 'upstream/master' into dask-tokenize * upstream/master: upgrade black verison to 19.10b0 (pydata#3456) Remove outdated code related to compatibility with netcdftime (pydata#3450) Remove deprecated behavior from dataset.drop docstring (pydata#3451) jupyterlab dark theme (pydata#3443) Drop groups associated with nans in group variable (pydata#3406) Allow ellipsis (...) in transpose (pydata#3421) Another groupby.reduce bugfix. (pydata#3403) add icomoon license (pydata#3448) change ALL_DIMS to equal ellipsis (pydata#3418) Escaping dtypes (pydata#3444) Html repr (pydata#3425) commit 507b1f6 Author: dcherian <deepak@cherian.net> Date: Tue Oct 29 09:34:47 2019 -0600 Fix window test commit 4ab6a66 Author: dcherian <deepak@cherian.net> Date: Thu Oct 24 14:30:57 2019 -0600 Implement __dask_tokenize__
This PR supersedes #1820 - see that PR for original discussion. See this gist to try out the new MultiIndex and options functionality.
black . && mypy . && flake8
whats-new.rst
for all changes andapi.rst
for new APITODO: