Versions Compared

Key

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

Python and .NET.

TutorialsPython and .NET Integration | Tutorial | Reference


This Tutorial Teaches you to:

  • Create ...
  • Set up ,,,

Prerequisites:

Table of Contents
maxLevel2
minLevel2
indent10px
excludeTutorial
stylenone

1

. Create a Simple Text Report

We'll create a shift report showing tank status.

  1. Navigate to Reports → Forms
  2. Click first row to add new form
  3. Configure:
    • Name: ShiftReport
    • SaveFormat: Text
    • SaveFileName: C:/Reports/Shift.txt
  4. Press Enter

2. Design Report Content

  1. Go to Reports → Forms Editor
  2. Double click ShiftReport 
  3. Enter the report template below, then Save the report:
================================
    TANK FARM SHIFT REPORT
    Date: {Server.DateTimeInfo.Date}
    Time: {Server.DateTimeInfo.Time}
================================

TANK STATUS SUMMARY
-------------------
Tank 1:
  Level: {Tag.TankFarm/Tank1/Level} ft
  Temperature: {Tag.TankFarm/Tank1/Temp} °F
  Pressure: {Tag.TankFarm/Tank1/Pressure} PSI
  Status: {Tag.TankFarm/Tank1/Status}

Tank 2:
  Level: {Tag.TankFarm/Tank2/Level} ft
  Temperature: {Tag.TankFarm/Tank2/Temp} °F
  Pressure: {Tag.TankFarm/Tank2/Pressure} PSI
  Status: {Tag.TankFarm/Tank2/Status}

PRODUCTION METRICS
------------------
Total Volume: {Tag.TankFarm/Metrics/TotalVolume} gal
Flow Rate: {Tag.TankFarm/Metrics/FlowRate} gpm
Daily Production: {Tag.TankFarm/Metrics/DailyProduction} gal

================================
End of Report

3. Test Report Generation

  1. Start runtime 
  2. Open PropertyWatch
  3. Go to List2 and type: Report.Form.ShiftReport.Save
  4. Change the value from 0 to 1
  5. Check the folder to see generated report

4. Schedule Automatic Reports

Create scheduled task for shift reports:

  1. Go to Reports → Forms
  2. Change the SaveTrigger to: Server.Hour
  3. The report will be generated every hour once the solution is running.

5. Monitor Report Status

. Installation and Setup

The Python Shell integration requires Python releases 3.7 onwards and the installation of Python.NET.


  • You can download Python here.
  • Check the User admin privileges checkbox, and select Customize installation.

Image Added

  • During the installation of the Python Engine,select the option to “Install for all users”.

Image Added

  • If You install Python under your local user, you may have issues when running the solution as a service, or when deploying the solution for production.
Tip

This image may be slightly different for your Python version, but always enable the options equivalent to:

  • Add Python.exe to PATH

Especially in the production server.

  • Install with admin rights to all users,
  • Customize the installation to installation in the Program Files, instead of Users folder


  • Once you've downloaded, open the command prompt as an ADMINISTRATOR and type “pip install pythonnet”. For this work, you'll need internet access. If internet is not available, download and install manually.
  • Once Python.NET has been installed, you can start using Python in your solution.

Image Added

Note

Note: For Python versions above 3.12, Pythonnet is not available. For more information, visit their documentation.


  • Go to Solution / Settingsand locate the Python Interpreter box. Click the "..." button, navigate to find the installed Python Engine, and select the python.exe file.

Image Added

Python Interpreter

Local

Defines the path of the Python installation folder on the local machine.

Server

Defines the path of the Python installation folder on the remote server.



2. Using Python in the Solution


Scripts Tasks and Classes

  • Scripts / Tasks: Event driven or periodical tasks, running on the server computer. They can be written in Python, C# or VB.NET.
  • Scripts / Classes: library of methods and functions that can be used on both server and client machines. They can be written in Python, C# or VB.NET.

Displays CodeBehind and Graphical Elements 

The Code Behind for the displays can be written in C#, VB.NET or JavaScript, not Python directly. But from the CodeBehind you can call tasks and methods, which can be written in Python. 

Shell Integration

  • Shell Integration: allows you to execute Python code from external files.

This integration allows you to execute Python code from external files. Just create a Script Task with the following code:

Code Block
languagepy
titleShell Integration example
# This code call the execution of the external file using Python Shell 
# with the optional args defined in this initial section
#
# The macro _ExecutionPath_ is replaced by the path where the solution is set to execute
# Replace that macro by a specific path, or user other built-in macros as needed
#

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


Tip
titlePython Shell Version and Tests

Any Python scripts you create in Scripts / Tasks can be edited and tested in Scripts / Code Editor. To test your script, click on [Monitor Icon] in the top toolbar. This will run your code and display messages in the Output Window, under the script editor.

This integration will work if your Python version is 3.7 or newer, with Python.NET installed, and the executable is correctly mapped in Solution / Settings.

  • During runtime, open Reports → Reports Monitor
  • Verify report status:
    • Name: ShiftReport
    • LastStatus: 0
    • SavedFileName: Shows generated file path
  • Check for any errors in LastStatusMessage


    Excerpt Include
    Python and .NET Integration
    Python and .NET Integration
    nopaneltrue


    In this section...

    Page Tree
    root@parent