PythonManager

This module is used to manage the Python interpreter and to create a Python object in JavaScript.
For documentation on how to call Python function, check the PyObjectWrapper class.

Classes

PyObjectWrapper

Methods

(inner) addSysPath(path)

Add a path to the Python sys.path, allowing the user to import a Python library located in that path.
Its also possible to edit the environment variable "PYTHONPATH" instead of using this function.
Note: Once a library is imported, Python will cache a copy of that library. So even if you remove the line calling "addSysPath", Python will still be able to import that library in every other script execution. The only way to remove a libray from the cache is to restart the application.
Example
PythonManager.addSysPath("C:\\Python27\\lib\\site-packages");
Parameters:
Name Type Description
path string path where some Python module are located.

(inner) createPyObject(path, moduleNameopt) → {PyObjectWrapper}

Create or return existing Python object created with the specified Python script and associated with Python 'moduleName' module.
The returned object will have a 'py' property having the functions declared in the Python script as properties, allowing the user to call those Python functions.
See the documentation on the PyObjectWrapper class for information on how to call Python function.
Note: Python module will be reloaded if the Python script has been changed. In this case the module functions will be updated, but the removed Python functions will remain in memory.
Example
var myPythonObject = PythonManager.createPyObject("C:/pathToScript/pythonScript.py");
Parameters:
Name Type Attributes Description
path string The path where a Python script is located.
moduleName string <optional>
The name of the Python module associated with loaded script. If not specified or empty, the script file base name is used.
Returns:
a JavaScript representation of a Python object, which can then be used to call Python function
Type
PyObjectWrapper

(inner) getPyObjects() → {Object.<string, ObjectWrapper>}

Return a collection of created Python objects (see createPyObject) by name of the Python module associated with loaded scripts.
Example
var pyObjects = PythonManager.getPyObjects();
Object.keys(pyObjects).forEach(function(k) {
  MessageLog.trace("Loaded script : " + k);
});

var myPythonObject;
if ('myScript' in pyObjects) {
  myPythonObject = pyObjects['myScript'];
}
else {
  myPythonObject = PythonManager.createPyObject("C:/pathToScript/pythonScript.py", "myScript");
}
Returns:
Object with Python objects as values and associated Python module names as keys
Type
Object.<string, ObjectWrapper>

(inner) wrapFunction(jsFunction) → {QObject}

Wrap a JavaScript function. The returned object can then be send as a parameter to a Python function
Examples
//JavaScript file
 function add(a,b,c)
 {
   return a+b+c;
 }
 
 function pythonScripting()
 {
   var myPythonObject = PythonManager.createPyObject("C:/pathToScript/pythonScript.py");
   var x = PythonManager.wrapFunction(add)
   //the following commented line could have been used instead to produce the same result
   //var x = PythonManager.wrapFunction( function( a,b,c) {  return a + b +c; } );
   myPythonObject.py.testJsFunction(x);
 }
 
 

Note: When calling a JavaScript function from Python, you need to use the "call" function.

 # Python file
 def testJsFunction(f):
   tuple = (5, 2, 8)
   result = f.call(tuple)
   print result # print 15
Parameters:
Name Type Description
jsFunction QObject a JavaScript function
Returns:
a wrapped JavaScript function
Type
QObject