There is a new alternative to js2py called PythonMonkey which lets you import JavaScript files directly from Python and execute them.
https://github.com/Distributive-Network/PythonMonkey
JavaScript numar.js file:
module.exports = function numar(){
return 1; //return 1 if the checkbox is
}
Python main.py file:
import pythonmonkey as pm
numar = pm.require('./numar')
print(numar()) # this outputs "1.0"
Answer from Will Pringle on Stack OverflowThere is a new alternative to js2py called PythonMonkey which lets you import JavaScript files directly from Python and execute them.
https://github.com/Distributive-Network/PythonMonkey
JavaScript numar.js file:
module.exports = function numar(){
return 1; //return 1 if the checkbox is
}
Python main.py file:
import pythonmonkey as pm
numar = pm.require('./numar')
print(numar()) # this outputs "1.0"
This is still an issue in js2py and this link may help you. https://github.com/PiotrDabkowski/Js2Py/issues/123#issuecomment-866218046
The only way to solve this is to modify translated file.
# translate myfunction.js, numar.js files to python
import js2py
js2py.translate_file('numar.js', 'numar.py')
js2py.translate_file('myfunction.js', 'myfunction.py')
// myfunction.js
var numar = require("./numar");
function myfunction() {
var b = numar();
return b;
}
// numar.js
function numar(){
return 1; //return 1 if the checkbox is
}
# then modify translated myfunction.py file like this
__all__ = ['myfunction']
# Don't look below, you will not understand this Python code :) I don't.
from js2py.pyjs import *
from numar import numar
# setting scope
var = Scope( JS_BUILTINS )
set_global_object(var)
# Code follows:
var.registers(['numar', 'myfunction'])
@Js
def PyJsHoisted_myfunction_(this, arguments, var=var):
var = Scope({'this':this, 'arguments':arguments}, var)
var.registers(['b'])
var.put('b', var.get('numar')())
return var.get('b')
PyJsHoisted_myfunction_.func_name = 'myfunction'
var.put('myfunction', PyJsHoisted_myfunction_)
var.put('numar', numar.numar)
pass
pass
# Add lib to the module scope
myfunction = var.to_python()
# import in your code
from myfunction import myfunction
print(myfunction.myfunction())
You can find the code here.
https://github.com/lacmansoftware/python-js2py-require-solution
Videos
Find a JavaScript interpreter that has Python bindings. (Try Rhino? V8? SeaMonkey?). When you have found one, it should come with examples of how to use it from Python.
Python itself, however, does not include a JavaScript interpreter.
To interact with JavaScript from Python I use webkit, which is the browser renderer behind Chrome and Safari. There are Python bindings to webkit through Qt. In particular there is a function for executing JavaScript called evaluateJavaScript().
Here is a full example to execute JavaScript and extract the final HTML.
You can also use Js2Py which is written in pure python and is able to both execute and translate javascript to python. Supports virtually whole JavaScript even labels, getters, setters and other rarely used features.
import js2py
js = """
function escramble_758(){
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write(a+c+b)
}
escramble_758()
""".replace("document.write", "return ")
result = js2py.eval_js(js) # executing JavaScript and converting the result to python string
Advantages of Js2Py include portability and extremely easy integration with python (since basically JavaScript is being translated to python).
To install:
pip install js2py
Using PyV8, I can do this. However, I have to replace document.write with return because there's no DOM and therefore no document.
import PyV8
ctx = PyV8.JSContext()
ctx.enter()
js = """
function escramble_758(){
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write(a+c+b)
}
escramble_758()
"""
print ctx.eval(js.replace("document.write", "return "))
Or you could create a mock document object
class MockDocument(object):
def __init__(self):
self.value = ''
def write(self, *args):
self.value += ''.join(str(i) for i in args)
class Global(PyV8.JSClass):
def __init__(self):
self.document = MockDocument()
scope = Global()
ctx = PyV8.JSContext(scope)
ctx.enter()
ctx.eval(js)
print scope.document.value
According to https://stackoverflow.com/a/30537286/6353933,
import js2py
js = """
function escramble_758(){
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write(a+c+b)
}
escramble_758()
""".replace("document.write", "return ")
result = js2py.eval_js(js) # executing JavaScript and converting the result to python string
Advantages of Js2Py include portability and extremely easy integration with python (since basically JavaScript is being translated to python).
To install:
pip install js2py
PyExecJS seems to be a good option.
>>> import execjs
>>> execjs.eval("'red yellow blue'.split(' ')")
['red', 'yellow', 'blue']
>>> ctx = execjs.compile("""
... function add(x, y) {
... return x + y;
... }
... """)
>>> ctx.call("add", 1, 2)
3
to install:
$ pip install PyExecJS