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

Matplotlib 3.8.4 update: restore WASM backend and partially restore HTML5 Canvas backend #64

Merged
merged 8 commits into from
Dec 16, 2024

Conversation

agriyakhetarpal
Copy link
Member

@agriyakhetarpal agriyakhetarpal commented Nov 29, 2024

This PR has been put together towards a resolution for pyodide/pyodide#4510, which aims to update the Matplotlib version for Pyodide in-tree from 3.5.2 to 3.8.4, and is the next step towards releasing matplotlib-pyodide version 0.2.3 – please see #60 for tracking the release. It restores the WASM backend to work with Matplotlib 3.8.4 and partially restores the HTML5 Canvas backend—an attempt was made to get it to work, but I did not see through it entirely—by internally redirecting it to use the WASM backend for rendering.

A complete restoration of the HTML5 backend or a reorganisation of the backends to a maintained or working state can be looked at later with a new matplotlib-pyodide release and a corresponding Pyodide release, and contributions towards this are very much welcome. Please see #65 for further discussions and additional context.

Specifically, the changes here are as follows:

  • build-backend related files (setuptools-specific) have been git-ignored
  • the event listener for the mouse movements has been silenced for now – it creates on the order of several hundred warnings in the console otherwise, which isn't good for UX and for those trying to debug things when working with Matplotlib
  • the WASM backend works with Matplotlib 3.8.4
  • there has been an attempt to update the HTML5 backend to use a path-based MathTextParser, since the bitmap-based one was deprecated in Matplotlib 3.4 and removed around 3.6. It is able to create and parse glyphs and draw them on the screen, but fonts are currently missing when drawn.
  • noting the above and noting the HTML5 Canvas backend's popularity, we have redirected the HTML5 backend to also use the WASM backend internally, so that users don't need to change anything for their existing apps or lecture material.

Copy link
Member

@ryanking13 ryanking13 left a comment

Choose a reason for hiding this comment

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

Thanks!

@ryanking13
Copy link
Member

Looks like matplotlib is complaining about the type of capstyle. Using enums may work.

from matplotlib._enums import CapStyle

self._capstyle = CapStyle(...)

Copy link
Member

@ryanking13 ryanking13 left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks!

@agriyakhetarpal
Copy link
Member Author

agriyakhetarpal commented Dec 10, 2024

Still not fully fixed yet!

@agriyakhetarpal agriyakhetarpal changed the title Browser backend: check existence of toolbar before initialising Matplotlib 3.8.4 update: restore WASM backend and partially restore HTML5 Canvas backend Dec 16, 2024
@agriyakhetarpal
Copy link
Member Author

Bumping to Chrome 131 here did not fix the previous issues, so I guess we'll tackle it later, it's not strictly necessary. I'll patch Matplotlib once again with these changes in pyodide/pyodide#4510 and see if everything is working – if yes, we can merge this safely.

@agriyakhetarpal
Copy link
Member Author

Thanks for the review, @ryanking13! I'd like to keep the HTML5 backend updates I made here and not completely remove them so that either I or someone else can return to them at some point and fill in the missing piece that is required to make them work. Since you've already approved these changes in the PR, I shall merge it now – but if you feel otherwise about anything, please let me know!

@agriyakhetarpal agriyakhetarpal merged commit 3aaa9a2 into pyodide:main Dec 16, 2024
4 checks passed
@agriyakhetarpal agriyakhetarpal deleted the check-toolbar-existence branch December 16, 2024 16:33
@hoodmane
Copy link
Member

Thanks so much for working on this @agriyakhetarpal!

@jsclass42
Copy link

jsclass42 commented Jan 9, 2025

First thank you all very much for such an amazing project!

One thing I would like to bring to your attention is the change introduced at 0.27.0 of backend approach really breaks backend like FigureCanvasSVG. This is what we get when we try: "UserWarning: FigureCanvasSVG is non-interactive, and thus cannot be shown". Prior to this update, to implement/extend a backend was simple and versatile, which basically override FigureManager show() method, say, to simply print out some information.

Can we at least have a mechanism to make this non-interactive optional, or allow a custom backend implementation to return a simple flag to comply with whatever requirement (in this case, pretend to be interactive, thus show() will be called upon).

Currently, we are unable to upgrade to 0.27.0 because of this.

Again, thanks!

@ryanking13
Copy link
Member

Thanks for the report @jsclass42, sadly, we are lacking human resources to manage two different backends and we are trying to find a way to support matplotlib-pyodide in a long term (#65). Meanwhile, you can try to patch matplotlib-pyodide, make your own backend and accomplish what you need.

@agriyakhetarpal
Copy link
Member Author

Yes, agree with Gyeongjae – I would be grateful if you could fully restore the previous behaviour and find out what's missing for the backend(s) to work as they did earlier. If we're able to, we can easily get a new release here and include it with Pyodide 0.27.1/0.27.2.

@jsclass42
Copy link

jsclass42 commented Jan 10, 2025

Thank you @ryanking13 for your reply and suggestion! I decided to look into the codes, including the matplotlib/backend_bases.py, and it turns out the break on my end had nothing to do with pyodide or matplotlib-pyodide.

If it helps anyone: if you are running into problem like '... in non-interactive, and thus cannot be shown', the warning comes from the base FigureManagerBase .show() method, if you extend the class and not override the show() method NonGuiException( f"{type(self.canvas).__name__} is non-interactive, and thus cannot be " f"shown") , or, when you think you are overriding the method in your custom backend but, do not actually register your backend.

Again, thanks!

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