Differences

This shows you the differences between two versions of the page.

Link to this comparison view

functions:python [2025/01/21 01:59]
optrix created
functions:python [2025/01/21 02:12] (current)
optrix
Line 3: Line 3:
 The **python** method is a [[style|function method]] that launches a Python script in response to an API request and returns any information that the script writes to the console. The **python** method is a [[style|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 Parameters===+===Additional Info.json Parameters===
  
-This method requires a **path** parameter - this is the path to the Python script. This path can include [[substitutions|substituions]].+This method requires a **path** parameter - this is the path to the Python script. This path can include [[substitutions|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... 
 + 
 +<code> 
 +Hello World 
 +Sending Query.... 
 +Running Analytics... 
 +---- 
 +Time, Value 
 +10:00:00,10 
 +10:00:10,11 
 +10:00:20,12 
 +</code> 
 + 
 +...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... 
 + 
 +<code> 
 +
 +    "name": "Testing", 
 +    "method": "python", 
 +    "path": "[ROOT]/myscript.py", 
 +    "parameters": [{  
 +             "name": "Message", 
 +             "type": "string",  
 +             "tag": "msg", 
 +             "default": "Hello World" 
 +    }]. 
 +   "folder": "Testing" 
 +
 +</code> 
 + 
 +...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). 
 + 
 +<code> 
 +
 +   "msg": "Hello World" 
 +
 +</code> 
 + 
 +===Calling the Function=== 
 + 
 +The python code would then be run with the following command... 
 + 
 +<code> 
 +python c:\ARDI\web\sites\default\functions\testing.py mytempfile.json 
 +</code> 
 + 
 +===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.