You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 12 Next »

Download the Solution Example:


Summary

This example demonstrates the integration of Python scripts in the FrameworX environment, showcasing how various Python methods and external scripts can be triggered using tags and buttons in the user interface.


Technical Information

This solution demonstrates four key Python integrations within the FrameworX environment: PythonStandard, PythonDataAccess, PythonShellBasic, and ClassPython. Each integration method handles tag values, external scripts, or Python classes differently, and they are triggered by either tag changes or button presses. Below is a breakdown of each method:


PythonStandard Integration

This example showcases the use of inline Python code, executed directly in the FrameworX environment. The script subtracts the values of two tags and writes the result to another tag.

def sub(vaarg1 = @Tag.Tag1(0) arg2 = @Tag.Tag2(0) result = TK.ExecutePythonShell("_ExecutionPath_ExternalSum.py", [arg1, arg2]) @Tag.Result(0) = resultl1, val2):
    return val1 - val2

resultado = sub(@Tag.Tag1[1], @Tag.Tag2[1])
@Tag.Result[1] = resultado

Execution: This script is triggered when the tag Tag.TriggerPythonStandard changes value, typically controlled by a button press in the user interface.


PythonDataAccess Integration

This method calls external Python scripts that access the DataAccess API, allowing for interaction with external systems or databases. The result of the external script is then stored in a tag.

result = TK.ExecutePythonShell("_ExecutionPath_ExternalSumDataAccess.py", [])
@Tag.Result[2] = result

print("Result: " + str(result))

Execution: The script runs when the Tag.TriggerPythonDataAccess tag changes value. The external Python file ExternalSumDataAccess.py is executed, and the result is logged and stored in Tag.Result[2].


PythonShellBasic Integration

This example demonstrates calling external Python files using the Python Shell and passing optional arguments to the script.

arg1 = @Tag.Tag1(0) arg2 = @Tag.Tag2(0) result = TK.ExecutePythonShell("_ExecutionPath_ExternalSum.py", [arg1, arg2]) @Tag.Result(0) = result

Execution: When Tag.TriggerPythonShellBasic changes value, the script ExternalSum.py is executed, with the values of Tag.Tag1 and Tag.Tag2 passed as arguments. The result is stored in Tag.Result(0).


ClassPython Integration

This integration calls a Python class and executes a specific method defined within the class. Unlike the other scripts, which are triggered by tag changes, this method is called upon via a expression

Button Action: When the button is pressed, the following expression is executed:

Script.Class.ClassPython.multiply(Tag.Tag[1], Tag.Tag[2])


Class Code:

def main(self): return def sum(self, val1, val2): return val1 + val2 def multiply(self, val1: int, val2: int): return val1 * val2


Execution: The button triggers the multiply method, passing the values from Tag.Tag[1] and Tag.Tag[2] and storing the result in Tag.Result[2].


Reference

→ See Python Integration for more information.


In this section:

The root page @parent could not be found in space v10.

  • No labels