2013 Summer Project Week:Python Embedding

From NAMIC Wiki
Jump to: navigation, search
Home < 2013 Summer Project Week:Python Embedding

Key Investigators

  • GRC: Rui Li, Jim Miller
  • Kitware: Jean-Christophe Fillion-Robin
  • Isomics: Steve Pieper


Python embedding library to manage calling python functions from C++. This will eliminate the code duplication in Slicer.

Approach, Plan

Discuss with Jc and Steve regarding how to incorporate into Slicer. Currently it is used as a downloadable library during superbuild, similar to SlicerExecutionModel.


Eliminate all the compile time warnings and fixed some stylistic issues. The updated code is pushed on to github at https://github.com/grclirui/PythonCppAPI.git and ready to use.

Delivery Mechanism

PythonCppAPI is a stand alone library. To use it within Slicer, similar to SlicerExecutionModel and other external libraries, an External_PythonCppAPI.cmake is needed in SuperBuild. During superbuild, it will be checked out and built as a library. The sample usage of the library is shown in the following code snippet:

 // Ok. Now we can create an instance of the interprter itself.
 PythonCppApiCallFunction pyCppApiCallFunction(argc, argv);
 // to check if the python interpreter is available and working,
 // execute a simple python statement.
 pyCppApiCallFunction.ExecuteString("print 'hello'");
 // execute the script in a file
 // Load the test module into the interpreter
 ArgMap args; // our argument list that maps string values 
 bool ok;
 // Call a simple function, that takes no arguments
 // and returns nothing     
 pyCppApiCallFunction.CallFunction("simple", args);
 // Call a function that takes two long arguments and returns
 // a long      
 PythonCppApiVariant ret(0); // value returned by python is stored here
 args["10"] = PythonLong;
 args["20"] = PythonLong;
 ret = pyCppApiCallFunction.CallFunction("multiply", args);
 std::cout << ret.toLong(&ok, 0) << '\n';