In [1]:
from flexx.pyscript import py2js, evalpy
We can transpile strings of Python code:
In [2]:
js = py2js('for i in range(10): print(i)')
print(js)
Or actual Python functions:
In [3]:
def foo(x):
res = []
for i in range(x):
res.append(i**2)
return res
js = py2js(foo)
print(js)
Let's try that again, but now with a list comprehension. (The JS is valid and will run fine, though its less readable.)
In [4]:
def foo(x):
return [i**2 for i in range(x)]
js = py2js(foo)
print(js)
Classes are also supported, but not in the notebook (apparently Python cannot retrieve the source code in this case).
In [5]:
class Bar:
def spam(self):
return 3 + 4
#js = py2js(Bar)
# This only works if Bar is defined in an actual module.
Using evalpy
you can evaluate Python code in NodeJS:
In [6]:
evalpy('print(None)')
Out[6]: