sympy / live

SymPy Live Shell based on the JupyterLite and Pyodide
https://live.sympy.org
10 stars 5 forks source link

Plots are not drawn with matplotlib #18

Open sylee957 opened 1 year ago

sylee957 commented 1 year ago

I see that the matplotlib is not recognized by the default backend in plots.

image

However, pyodide has matplotlib installed, so import matplotlib prepending just works

image

I wonder if this can be appended to the templates, or sympy plot should recognize this to load matplotlib on demand. (if loading matplotlib is heavy)

asmeurer commented 1 year ago

Apparently running import matplotlib installs matplotlib on the fly. But somehow this doesn't happen for the import matplotlib line that happens internally in the plotting module. Ideally any import that happens internally in SymPy should also install the module if it can, because those imports represent optional dependencies which enhance SymPy's capabilities.

NumPy is the same way. If you call lambdify, it will create a function that doesn't use the NumPy backend. But if you import numpy first it uses numpy:

Screen Shot 2023-04-07 at 1 57 47 PM
asmeurer commented 1 year ago

Another thought: basically all external imports in SymPy go through this import_module() function. So we could potentially add some code in there to automatically import things if we detect we are in SymPy Live (this could either be added directly in SymPy or monkeypatched in SymPy Live).

Although I would like to understand why this is happening first. Maybe the problem is that pyodide intercepts import statements syntatically but import_module is using __import__. If that's the case, it would be better for pyodide to hook into Python's import mechanisms directly.