Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info
iconfalse

Quick video tutorial: Using Phyton with TK Data Access, Using Phyton with Script Task, Using Phyton with Code Behind

Overview

In many of your projects, the programming will consist of C# or VB.Net 100% managed code designed to run in the Microsoft .NET framework.

...

  • Using the Script>Task for Python: Execute Python code (.PY) and using the Script Task interface to set and get parameters (no code needed in the Studio side).
  • Python namespace (Python for .Net): Create .Net code using the Python namespace to interact with the Python objects.
  • Python using TKDataAccess toolkit: Create Python code using the TKDataAccess.py (provide by us) to interact with the Studio projects.


...

System Requirements

The first step required to enable Python programming is to include an interpreter to this language. An interpreter is a program that reads and executes code.

Interpreters and compilers are similar, since they both recognize and process source code. However, a compiler does not execute the code like an interpreter does. Instead, a compiler simply converts the source code into machine code, which can be run directly by the operating system as an executable program. Interpreters bypass the compilation process and execute the code directly.


Python Interpreter

The first requirement your computer must have is to contain a Python interpreter. To download it, use this link.

...

Click on the button and browse for the Python Engine installed and select the python.exe file.


Python for .Net

Python for .NET is a package that gives Python programmers nearly seamless integration with the .NET Common Language Runtime (CLR) and provides a powerful application scripting tool for .NET developers.

...

This is required only if you intend use the Python namespace in the Studio Scripts and Display CodeBehinds.


...

Creating Scripts in Python

To create scripts based on Python programming language, navigate to Scripts > Tasks, select a blank new row and double-click on Code column. You should see Python available for selection in the ComboBox.

...

Warning

After making changes, you need click the Apply Changes button.


...

Python Namespace

Must use for .Net developers, the Python namespace can be used in any script editor inside your project environment (Tasks, Classes and CodeBehind). Basically, all you need is to install Python for .NET.

...

Warning

Those methods are disabled for Mono project and HTML5 displays.


...

TKDataAccess.py

Must use for Python developers, you can create code in the Python environment and use the TKDataAccess.py to interact with the projects. 

...

Code Block
ExecuteClassMethodOnServer(className, methodName, parameters)

className = name of the class in the remote project.
methodName = name of the method in the remote class.
parameters = if any, necessary to the invoke the remove method.


...

Example

In the sections below there are details on the different ways to use Python in a project.


Using Namespace in CodeBehind

In this first example, there are two input parameters called val1 and val2 that will be summarized and the result will be stored in the result variable.

...

Code Block
//Defining where locate the .py files that will use
string  pyDefinition  =  @Info.GetExecutionFolder()  +  @"\Calc\algorithm.py"; string pyWorkingFolder = @Info.GetExecutionFolder();

try
{
string error;
Dictionary<string,  object>  param  =  new  Dictionary<string,  object>();

       //Defining the used imports from Python	
string imports = "";
imports += "import sys" + Environment.NewLine;
imports += "from Calc.algorithm import Algorithm" + Environment.NewLine;

      //Creating a .Net object from a Python object	
object algorithm =Python.CreatePythonObjectFromPyFile(pyDefinition, "Algorithm", null);
//Creating a .Net object from a Python object	
object val1 = Python.ToPython(@Tag.val1);
object val2 = Python.ToPython(@Tag.val2);

      //Setting the parameters with the .Net objects that will be used to execute the Python code
param.Clear(); param.Add("algorithm", algorithm); param.Add("val1", val1);
param.Add("val2", val2);

      //Call method to execute Python code
error  =  Python.ExecuteCode("result  =  algorithm.Sum(val1,  val2)",  pyWorkingFolder,  param,  true); 

if (!string.IsNullOrEmpty(error))
throw new Exception(error);

       //Set .Net object with result Python object , return of algorithm.Sum Python method
object result = param["result"];

//Copy .Net object to Tag
Python.CopyPythonObjectToTag(result, @Tag.result.GetName());
}
catch (Exception ex)
{
@Info.Trace("Python: " + (ex.InnerException == null ? ex.Message : ex.InnerException.Message));
}


Using Tasks

In this scenario we configure a task for Python language. In the Python file name field set the Python file to be executed. In this example we used the Main2.py.

...

Code Block
import sys

value = sys.argv[1] print("Value: " + value) print("That’s  all  folks!")


Using TKDataAccess.Py

In this example we will call a file named Main.py, which contains code that copies the content from tag1 (source) to tag2 (target).

...