Python (Method)

The python method is a function method that launches a Python script in response to an API request and returns any information that the script writes to the console.

Additional Info.json Parameters

This method requires a path parameter - this is the path to the Python script. This path can include substitutions.

Captured Output

To allow for diagnostic and debugging messages, the output returned to the user is split by separators. By default, this is a set of four minus-signs ('—-').

If these separators are present in the output of your program, only the text after the last separator will be sent.

So in the following output…

Hello World
Sending Query....
Running Analytics...
----
Time, Value
10:00:00,10
10:00:10,11
10:00:20,12

…only the lower half (from 'Time,Value') would actually be sent to the user.

If there are no separators found, the system returns the full output of your script.

How It Works

For this method, the user-provided parameters are converted into a JSON file. The full path of this file is then sent as the first parameter of your script.

For example, the following info.json file…

{
    "name": "Testing",
    "method": "python",
    "path": "[ROOT]/myscript.py",
    "parameters": [{ 
             "name": "Message",
             "type": "string", 
             "tag": "msg",
             "default": "Hello World"
    }].
   "folder": "Testing"
}

…would result in the following steps if it was called with the default parameter values.

Building the Input File

The following file would be created and saved to a temporary file at mytempfile.json (this is a simplified file name for ease-of-reading).

{
   "msg": "Hello World"
}

Calling the Function

The python code would then be run with the following command…

python c:\ARDI\web\sites\default\functions\testing.py mytempfile.json

Capturing Output

The output of the function would then be captured and sent to the user.

In more advanced scenarios, some style scripts might also be run to help translate the output between formats such as JSON to XML.