Download the Solution Example here: Python.dbsln
Software Version: 10.
0.6Keywords: Python, Scripts1
Summary
This
solutionexample demonstrates the
Integrationintegration of Python
and .NET scriptsscripts 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 project 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.
Code Block |
---|
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.
Code Block |
---|
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.
Code Block |
---|
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:
Code Block |
---|
Script.Class.ClassPython.multiply(Tag.Tag[1], Tag.Tag[2]) |
Class Code:
Code Block |
---|
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
→
Python can be used on the system directly calling shell commands, or using the .NET Python integrations tools.
The Example shows the various ways you can use the Python language in your solutions.
For this Solution Example to work, you must first install Python. Learn more at Python Integration.
Example 1: Python Standard
In this example, we use the system to code directly with Python, accessing Tag values.
Example 2: PythonShellBasic
In this example, the code calls the execution of the external file using the Python Shell, passing parameters and collecting the result.
Example 3: PythonShellDataAccess
In this example, the code calls the execution of the external file using the Python Shell.
Reference
See Python Integration for more information.
In this section:
Page Tree | ||
---|---|---|
|