-
Notifications
You must be signed in to change notification settings - Fork 96
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
Area definition html representation for Jupyter notebooks #450
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #450 +/- ##
==========================================
+ Coverage 94.06% 94.23% +0.17%
==========================================
Files 85 87 +2
Lines 13250 13458 +208
==========================================
+ Hits 12463 12682 +219
+ Misses 787 776 -11
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
You may want to try installing the pre-commit hooks. It looks like you've made the style checkers very angry 😉 Other than the style issues, what happens if matplotlib and/or cartopy are not installed? These are not hard requirements of pyresample so we should still show something useful if they are not installed. |
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.
In addition to my previous comment, I think you need to make sure that all of these new non-python files are in the MANIFEST.in so they are included in the distributed package. Lastly, would it be possible to just include the .css
and .html
files as strings in the new formatting_html.py
module? And can we prefix formatting_html.py
with an underscore so users are tempted to look into it?
This is really cool. Thanks for putting in the time to get it working.
@djhoese Yeah I already saw it :-D. I think most of the complaints are in the plotting which I need to refactor a little anyway. Good point about matplotlib/cartopy didn't think about that. But in that case I think it should just fall back to the normal string representation. I will also Look into the other suggestions you made. |
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.
A couple suggestions and questions, but otherwise coveralls is still saying the SwathDefinition branch of the formatting is not covered. That and the PNG output option. I see mention of swath definitions in your test so any idea why those branches aren't being covered?
pyresample/_formatting_html.py
Outdated
try: | ||
import cartopy | ||
cart = True | ||
except ModuleNotFoundError: | ||
cart = False |
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.
This is a personal preference, but you could avoid the extra boolean variable (here and for the xarray case) and do cartopy = None
in the except case.
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.
Do you want me to change it?
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.
Sure. It's just me being nitpicky.
|
||
try: | ||
import xarray as xr | ||
from xarray.core.formatting_html import _obj_repr, datavar_section |
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.
Do we have to use a private function (_obj_repr
)? How does it differ from repr(xarr_obj)
?
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.
Unfortunately yes. repr(xarr_obj)
produces the string representation which is also used as a fallback/ in the non notebook case. I could just "copy" the function into pyresample and make it non private?
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.
And your usage differs from the public array_repr
and dataset_repr
in that same formatting_html
module in xarray (these functions use _obj_repr
)?
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.
Yes because I want to be able to customize the header and the displayed sections (I only want to display the data variables from the whole xarray.Dataset
representation). Obviously that is a (personal) design choice which I am happy to talk about. I just tried to reuse as much as was already available but we could also implement something of our own.
Just to give you an idea of the differences (top: _obj_repr
, bottom: dataset_repr
):
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.
Very interesting. Yeah I see what you mean. I was going to say maybe we copy that functionality here, but then noticed it is loading the static icon SVGs so that gets awkward too. I guess that function is small too but it feels weird to copy them.
@mraspaud @pnuu any opinions on this for what "feels" right from the above screenshots? The bottom has so much extra "cruft" for things that aren't used, but maybe that is OK since we are just completely depending on upstream xarray's "private" functions. Or... @BENR0 what about a PR to xarray (in the long term) that let's you exclude empty sections? That would still include the Attributes but maybe that's a good thing? I could see it being beneficial to users to see the extra metadata hanging around the swath definition.
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.
Using a private function/method from another libraries feels wrong I have to say. I agree that the bottom is maybe too verbose, but having the dimension size explicitly here I think is nice actually.
I think the |
If the PNG stuff isn't used in the final output then I guess I'm ok with it being removed. Although...it might be nice to export an area definition to a PNG. A test wouldn't be too bad as long as you just checked that it was saving and maybe check that it isn't all black. I guess it is up to you what is best for the future. Or maybe @mraspaud has an opinion. |
The problem with the testing of the plotting function is that I actually don't save the plot to disc but generate the string representation to include it in the html later on. I guess I could check if |
Given that we're basically depending on another program to output the PNG representation I am OK with just knowing that it isn't empty (0 size). |
Ah I see you added specific tests that check savefig. |
@lru_cache(None) | ||
def _load_static_files(): |
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.
Is there any reason for this not to be 1?
pyresample/area_config.py
Outdated
|
||
Args: | ||
area_file (str): Path to area yaml file. | ||
|
||
Returns: | ||
str: rst list formatted string. |
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.
For future reference I think I'd like pyresample to migrate to type annotations and no type information in the docstring.
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.
Co-authored-by: David Hoese <[email protected]>
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.
LGTM. I think the import of the private function from xarray is indeed fragile, as there is no guaranty of that function's future. However, is seems to be something that is helpful, and I would thus suggest opening an issue in xarray to make this function public instead.
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.
LGTM!
pre-commit.ci autofix |
for more information, see https://pre-commit.ci
Opened an issue about the private |
This adds
_repr_html_
to theAreaDefinition
class. In Jupyter notebooks therefore instead of a string a nicely formatted text is show together with a map overview (cartopy plot with borders and coastlines) as can be seen in the example below. The map by default is folded right now to save space (for the screenshot I unfolded it).The way this is setup is heavily influenced by how xarray implements the
_repr_html_
. Basically I used the same directory/ file structure. I also copied some code (the html with the svg icons, the function to read the static data) which I did not attribute yet.While functionally ready some considerations before this get merged:
Apart from some refactoring of the plotting function (removing unecessary code and comments) I was thinking about maybe moving the plotting function into the class?
Only the
AreaDefinition
has a html representation as of now. It would be nice if something similar could be done forSwathDefinition
to make the look and feel more homogeneous.Tests added
Tests passed
Passes
git diff origin/main **/*py | flake8 --diff
Fully documented