We can evaluate python code to javascript using "py.js" library.
The basic usage looks like:
py.eval("python code", context);
The previous method chains the following calls:
- Tokenize
- Parse
- Evaluate
** We can do this process manually calling these methods separately.
By default the context has some native python methods such "abs", "len", etc... if we want to include our own types we need define them. For example, if we want to add our own method whe need extend "PY_def" (that represents a python 'def'):
var PY_t = new py.PY_def.fromJSON(function() {
var args = py.PY_parseArgs(arguments, ['str']);
return py.str.fromJSON(_t(args.str.toJSON()));
});
All these "base" types can be found at 'web/static/lib/py.js/lib/py.js'.