Integration with Python

Integration with Python opens up a great way to develop AI algorithms for your projects. Increase the power and reach of your existing Python code by integrating it into Action˳NET.

 

Overview

Programming on many of your projects will consist of C# or VB.NET 100% Managed code, designed to run in the Microsoft .NET framework.

Or Action.NET now includes Python as a programming language that can be used in Code Behind, Scripts, Tasks, and interactively through external Python code.

Python is an interpreted, high-level, general-purpose language. It is a popular language for Artificial Intelligence (AI) and Machine Learning (ML), which is very useful for use in algorithms such as predictive maintenance.

Or Action.NET can use any version of Python, 3.x, or the oldest and past 2.x.

Users can use Python in three different ways:

  • Using Script> Task: Towards Python: Run python code (. PY) and use the Script Task interface to set and get parameters (no code is required in Studio).

  • Namespace Python (Python to .Net): Create .Net code using the Python namespace to interact with Python objects.

  • Python using the toolkit TKDataAccess: Create Python code using the TKDataAccess.py (provided by us) to interact with Studio projects.

System requirements

Python Interpreter

The first step to using Python programming is to include a link to the version of the Python interpreter that you want to use in your project. You can download Python here.

During the installation of the Python Engine, we strongly recommend choosing the "Install for all users" option.

 

Now you need to add the interpreter to your project. Navigate to the tab Info>Settings and go to the board Python. Click the button, browse to find python engine installed, and select the python file.exe.

Python for .Net

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

Python for .NET allows Python code to interact with the CLR and can be used to embed Python in a .NET application. Installation files and documentation are available here.

Make sure your Python and Windows versions are 32-bit or 64-bit before downloading Python to .NET. This is only necessary if you intend to use the Python namespace in Studio Scripts and Display CodeBehinds.

Python scripting

To create scripts based on the Python programming language, navigate to Scripts> Tasks, select a new blank row, and double-click the Code column. After you click Code, a combo box appears. Select Python from the combobox.

After a new task is created, the language type cannot be changed using the Code column

 

After you create a new script task, you need to edit it on the CodeEditor tab.

The following settings are detailed:

  • File name: File name .py

  • Standard output: name of the tag that will receive all outputs printed via printing

  • Arguments: List of arguments. This list of parameters will be passed as arguments to the Python Engine (python.exe)

After making any changes, click the Apply Changes (Apply changes.

Python Namespace

The Python namespace can be used in any script editor (Tasks, Classes, or CodeBehind) within the project environment. To use the Python namespace, you simply need to install the package Python.NET, available on the Github.

The Python namespace provides several .Net methods that interact with Python code and objects. Here are some of these methods below:

Run a file .py using Python for .NET

string ExecutePyFile(string pyFileName, Dictionary<string, object> locals = null, bool keepValuesAsPython = false)   string pyFileName = Python file Dictionary<string, object> locals = Local variables inside Python code. Default is null. bool keepValuesAsPython = Keep retuned values as Python objects or convert to .NET objects. Default is false. string returns = If success return null else string contains error.

Run Python code using Python for .NET

string ExecuteCode(string code, string workingDirectory = null, Dictionary<string, object> locals = null, bool keepValuesAsPython = false)   string code = py file name string workingDirectory = Working directory. It will be added in 'sys.path' Dictionary<string, object> locals = Local variables inside Python code. Default is null. bool keepValuesAsPython = Keep retuned values as Python objects or convert to .NET objects. Default is false. string return = If success return null else string contains error.

Convert a Python value to a .NET value

public static object FromPython(object value)   object value = Python value object returns = NET value

Copy a Python object to a tag (Array or User Template)

Copy a tag (Array or User Template) to a Python object

Create a Python object from a Python class

Get all the attributes of a Python object

Set a new value for the attributes of a Python object

Pour a Python object into a string to send it to a TraceWindow

If you need to install other Python modules and libraries (such as numpy, pythonnet, matplotlib, etc.), you must install them in the same location as Python Engine (python.exe).

TKDataAccess.py

You can create code in the Python environment and use the TKDataAccess.py to interact with projects.

Or Action.NET provides the file TKDataAccess.py. When using it, you need to make sure that the installPath in the file TKDataAccess.py is in the correct way to install the Action.NET.

Below are some methods of TKDataAccess.py that you can use:

  • Open a connection to the server

     

  • Get a server connection status

     

  • Check your script's connection to the server

     

  • Disconnect from server

  • Set a wait flag of a server value

     

  • Retrieve a current value

 

  • Set a new value for an object

  • Run a method from a remote ScriptClass

Example

The following sections contain different ways to use Python in a project.

Using namespace in CodeBehind

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

The code that performs this action is presented below.

Using Tasks

In this scenario, we set up a task for the Python language. In the field of Python file name, you need to define the Python file that will run. In this example, we use Main2.py.

In the field Standard output, we select a tag called Output. This type of tag must be text. In the Arguments field, we select another type of tag called script.

Using the printing method, the Python file called Main2.py retrieves the input data and produces its value within a string. Sys.argv will receive the Tag.script and the output tag will receive all method values print ().

 

Using TKDataAccess.py

In this example, you need to call a file named Main.py, which contains the code that copies the contents of tag1 (origin) for tag2 (destination).

Python code using the module TKDataAccess.py in Main.py is described below:

So you need to create the code that is shown below or create a Python Script Task that runs the file Main.py, which contains the call to TKDataAccess shown above. So here, we'll use the Python namespace as described earlier.

The code is shown below.

On this page: