TranscryptOrg / Transcrypt

Python 3.9 to JavaScript compiler - Lean, fast, open!
https://www.transcrypt.org
Apache License 2.0
2.86k stars 214 forks source link

Can't import matplotlib #764

Closed drscotthawley closed 1 year ago

drscotthawley commented 3 years ago

Hi. Thanks for sharing this tool. I'm sure I'm doing something wrong, but haven't been able to figure out from the docs what that might be.
I was able to run the hello example just fine, but a simple scientific plot fails on line 3. I haven't seen any notes in the documenation about potential matplotlib problems, and haven't noticed other issues or StackOverflow posts about people having trouble with matplotlib, so I assume it's something I'm doing.
Could someone offer any feedback? Thanks. (I will say that I can import matplotlib in my python installation without problems!)

My code reads:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc
#(...and then it goes on...)

Running transcrypt yields

$ transcrypt -b wave.py 

Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.7.16
Copyright (C) Geatec Engineering. License: Apache 2.0

Saving target code in: /tmp/wave/__target__/org.transcrypt.__runtime__.js
Saving minified target code in: /tmp/wave/__target__/org.transcrypt.__runtime__.js

Error while compiling (offending file last):
    File '/tmp/wave/wave.py', line 3, at import of:
    File '/home/myusername/anaconda3/envs/spnet/lib/python3.7/site-packages/matplotlib/animation.py', line 3, at import of:
    File 'matplotlib', line 1586, namely:

    Can't import from module 'matplotlib'

Aborted
JennaSys commented 3 years ago

Since Transcrypt is converting the Python code to a JavaScript equivalent, most 3rd party Python libraries are not supported unless they are pure Python (including dependencies), and do not rely on any C libraries. NumPy is an exception thanks to the Numscrypt project.

If you are trying to plot Python data in a browser, have you looked into Dash yet?

drscotthawley commented 3 years ago

Thanks for clarifying @JennaSys. I'm one of those people who only uses the "Python Scientific Stack", who often operates as if matplotlib is an integral part of Python, which is obviously untrue.

I'm familiar with Plot.ly products but was under the mistaken impression that they couldn't work offline. I'll look into Dash. (I just took a "Dash" course but it was a different Dash!) Seems like Dash can be bundled & run offline, so that's encouraging. Thanks!

Another alternative, I spent some time last night with Pyodide and got a rough version of my simulation idea working.

Feel free to close this.

JennaSys commented 3 years ago

An update if it helps, since you are on the data science side of things, you might be interested in the IDOM project that takes an encapsulated though more constrained approach to what Transcrypt does (let's you create front-end web applications with Python).

JdeH commented 3 years ago

Maybe it's also worthwhile to take a look at:

http://www.transcrypt.org/live/transcrypt/demos/plotly_demo/plotly_demo.html

JennaSys commented 1 year ago

Closing this issue due to no further comments.