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

patch plt.show() and use AGG beneath #1023

Closed

Conversation

madhur-tandon
Copy link
Member

This is kinda WIP and may require improvements

But showcases how things like #983 can be fixed.

  • Add Tests
  • Add Comments

@madhur-tandon madhur-tandon added the sprint issue has been pulled into current sprint and is actively being worked on label Dec 6, 2022
Copy link
Contributor

@FabioRosado FabioRosado left a comment

Choose a reason for hiding this comment

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

Left a comment and a suggestion.

Is this a hard thing to patch upstream? Just wondering if it should be fixed there as well and then we can remove this code 🤔

We may need to add a test to confirm that the graph is shown correctly 😄

@@ -203,6 +203,21 @@ export class PyScriptApp {
// XXX: maybe the following calls could be parallelized, instead of
// await()ing immediately. For now I'm using await to be 100%
// compatible with the old behavior.

// needed for patch inside pyscript.py
await runtime.installPackage("matplotlib");
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm slightly concerned about installing matplotlib all the time even if we don't need it.

What do you think about adding the patch into a try/pass instead?

Comment on lines 20 to 35
os.environ["MPLBACKEND"] = "AGG"

import matplotlib.pyplot # noqa: E402

_old_show = matplotlib.pyplot.show
assert _old_show, "matplotlib.pyplot.show"


def show(*, block=None):
buf = io.BytesIO()
matplotlib.pyplot.savefig(buf, format="png")
buf.seek(0)
display(matplotlib.pyplot)
matplotlib.pyplot.clf()


matplotlib.pyplot.show = show
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we could do something like:

from contextlib import suppress

with suppress(ImportError):
import matplotlib.pyplot
_old_show = ...


That way if we can't import matplotlib we just pass.

@madhur-tandon
Copy link
Member Author

Hi @FabioRosado
As I said, this is kinda WIP right now, but fixes the issue hehe.
I think there needs to be more discussion on how to refine this 😅

But your concerns are valid.
In any case, this can't and shouldn't be patched upstream because we are modifying how plt.show() works here.

PS -- Jupyterlite also uses a similar kind of hack

@FabioRosado
Copy link
Contributor

Ah apologies, I just looked at the title and didn't read the main PR text

@madhur-tandon
Copy link
Member Author

Closing, since we don't really need this.
The solution to the original issue is using display correctly. I will comment there itself.

@madhur-tandon madhur-tandon deleted the patch-matplotlib-show branch December 8, 2022 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sprint issue has been pulled into current sprint and is actively being worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants