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

Ensure hvplot.<ext> hooks are run on every import #1359

Merged
merged 10 commits into from
Sep 13, 2024
Merged

Conversation

philippjfr
Copy link
Member

The hvplot.<ext> import mechanism is a convenient way to allow users to have to avoid running the holoviews/panel extensions. However since imports are cached only the first import actually embeds the extension JS code, meaning that if you re-run the cell(s) containing import hvplot.pandas (or some other integration) then the JS will no longer be available and on subsequent reloads/re-runs of the notebook plots may not appear. This probably accounts for most issues encountered and described in #1232.

Here we add an IPython hook which simply deletes the modules before every cell execution. This is a big hammer but the best @hoxbro and I could find and at least it is restricted to IPython environments.

@hoxbro
Copy link
Member

hoxbro commented Jun 20, 2024

We need to update the logic to only register hvplot extensions once, have updated it for some but we should update it for all.

 Screenshot 2024-06-20 14 44 48

hvplot/__init__.py Outdated Show resolved Hide resolved
Copy link
Member

@maximlt maximlt left a comment

Choose a reason for hiding this comment

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

This is indeed quite a hammer but it's fixing a pretty bad issue (although it's hard to tell if it's the source of the majority of the reported problems). Not sure whether we could abuse Python's import system to achieve the same behavior, that would anyway be a big hammer too, and the one suggested at least scopes it to IPython usage.

I'd appreciate some comments in the code that explain why we have to resort to running code on each cell execution and removing modules from sys.modules 🙃

Isn't this a good example of why relying on imports with side-effects is a bad idea in Python? Should we consider migrating to a more explicit API, even if it'd mean having to type a bit more?

While this code change improves the situation, I still think there's a major UX issue. We're injecting crucial JS/CSS in the notebook, that can be easily removed by users by mistake, without letting them know that this is wrong. This is a general issue through HoloViz packages that needs .extension() to be called (at least HoloViews and GeoViews display some logos). I would be in favor for showing a very explicit message. This might be a good topic for the next HoloViz meeting.

Copy link
Member

@maximlt maximlt left a comment

Choose a reason for hiding this comment

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

I think that as it stands this PR leads to import hvplot.<ext> fully loading the extension (JS/CSS) on every call. I don't think it's too bad, just noting this behavior change.

image

@maximlt
Copy link
Member

maximlt commented Jul 16, 2024

We need to look more into how this is going to affect Polars' users.

@maximlt maximlt marked this pull request as ready for review September 13, 2024 10:06
@maximlt maximlt requested a review from hoxbro September 13, 2024 10:06
@maximlt
Copy link
Member

maximlt commented Sep 13, 2024

@hoxbro can you please review?

Copy link

codecov bot commented Sep 13, 2024

Codecov Report

Attention: Patch coverage is 73.95833% with 25 lines in your changes missing coverage. Please review.

Project coverage is 88.73%. Comparing base (6c96c7e) to head (763392d).
Report is 24 commits behind head on main.

Files with missing lines Patch % Lines
hvplot/cudf.py 0.00% 13 Missing ⚠️
hvplot/intake.py 0.00% 7 Missing ⚠️
hvplot/__init__.py 64.28% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1359      +/-   ##
==========================================
+ Coverage   87.39%   88.73%   +1.34%     
==========================================
  Files          50       51       +1     
  Lines        7490     7592     +102     
==========================================
+ Hits         6546     6737     +191     
+ Misses        944      855      -89     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@hoxbro hoxbro left a comment

Choose a reason for hiding this comment

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

A few comments, you can ignore them if you don't agree 👍

hvplot/__init__.py Outdated Show resolved Hide resolved
hvplot/__init__.py Outdated Show resolved Hide resolved
@maximlt maximlt requested a review from hoxbro September 13, 2024 10:33
@maximlt maximlt merged commit cbf8700 into main Sep 13, 2024
9 checks passed
@maximlt maximlt deleted the import_hook_fix branch September 13, 2024 12:03
@michaelaye
Copy link
Contributor

oh my word, this explains so much, thanks for hunting it down...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants