Skip to content

Instantly share code, notes, and snippets.

@Tardo
Last active November 10, 2020 03:01
Show Gist options
  • Save Tardo/5ab5ffde220e73dad868dcc54afeda22 to your computer and use it in GitHub Desktop.
Save Tardo/5ab5ffde220e73dad868dcc54afeda22 to your computer and use it in GitHub Desktop.
py.js extended explanation #Odoo #JS

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'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment