matplotlib-pyodide
matplotlib-pyodide copied to clipboard
Matplotlib web assembly backend causes failures in webworker
Matplotlib tries to run from js import document. This causes an exception when Pyodide is being run from within a webworker.
The wasm backend wouldn't work in a webworker because it requires the DOM. However, running data-only backends in matplotlib seems useful and desirable.
Is this also true if you do
import matplotlib
matplotlib.use('Agg')
?
Unfortunately I couldn't actually test that as when I tried to change the backend I ran into https://github.com/iodide-project/pyodide/issues/441
That issue certainly was not matplotlib specific. I initially bumped into it running my own code, but I chose matplotlib just as an example that removes as much extra complications out of it...
I also recently encountered this problem and matplotlib.use() (as suggested in https://github.com/iodide-project/pyodide/issues/442#issuecomment-498784449) doesn't work because the backend has to be set before the matplotlib import.
The following approach -overriding the MPLBACKEND env var right up front- appears to work:
import os
os.environ['MPLBACKEND'] = 'AGG'
# keep calm & carry on as usual e.g.:
import matplotlib as mpl