process-mining
2021.10
true
UiPath logo, featuring letters U and I in white

Process Mining

Automation CloudAutomation Cloud Public SectorAutomation SuiteStandalone
Last updated Dec 20, 2024

Example: Creating a Python Script

Introduction

This example explains how to interface the UiPath Process Mining platform with external Python scripts to implement external data processing.

Note: The examples in this section are based on python version 3.

High-level Overview

A Python script is created which:

  • takes as input a .CSV file, to be specified on its command line as the sole required argument,
  • multiplies the Amount value by two,
  • writes the result to its standard-output channel.

Steps

  1. Setting up the Server Settings;
  2. Setting up the workspace;
  3. Setting up a script data source;
  4. Setting up the data source;
  5. Writing the script.

Setting up the Server Settings

The generic script datasource requires handlers for all external processes that you want to run.

Follow these steps to add a generic script handler.

Step

Action

1

Go to the Superadmin Settings tab.

2

Add a field GenericScriptHandlers with as value an object with one key, “py”, which has as value the path to your python executable. For example:

"GenericScriptHandlers": { "py": "P:/Python/bin/python.exe" }

3

Click on SAVE.

Setting up the Workspace

Start with creating a minimal script which does not yet do any data processing. This script will be used to verify that your python setup is working, and that your script is being called from the UiPath Process Mining platform.

This example script shows how to generate output that will appear in the script execution log, and how the exit code of a script influences the behavior of the UiPath Process Mining platform.

Step

Action

1

In your favorite editor, start a blank text file.

2

Enter the following text:

#!/usr/bin/python import sys def debug(message): sys.stderr.write(message) debug("Hello world!") exit(1)#!/usr/bin/python import sys def debug(message): sys.stderr.write(message) debug("Hello world!") exit(1)
Note: The debug(“Hello world!”) command is an example of how to use the standard error channel to output messages and debug output.

3

Save the text file as script.py.

4

Upload the script.py file to your workspace.

This script only prints a “Hello world!” message. The script interface uses standard output for data communication from the script to the UiPath Process Mining platform. If you want to include status messages in your script, you should write them to standard error instead.

Note: The example script exits with status code 1. Any status code other than 0 will be interpreted by the UiPath Process Mining platform as an error condition.

Setting up a Script Data Source

Next, set up a datasource table in the app which will call the script. Start with some dummy data, since your script will not yet process the data. At this stage, it will be verified that the script is executed as expected, i.e. that you can see the “Hello world!” message.

Step

Action

1

Open the app in your development environment.

2

Go to the Data tab and create a new Connection string table.

3

Rename the New_table to PythonExample.

4

Right click the PythonExample table and select Advanced > Options….

5

In the Table Options dialog, set the Table scope to Workspace.

6

Double click on the PythonExample table to open the Edit Connection String Table window.

7

Enter the following as Connection string:

``'driver={mvscript

8

Enter the following as Query:

``''

  • '&scriptFile=' + urlencode("script.py")
  • '&inputData=' + urlencode("a;b;c"+#10+"1;2;3"+#10+"4;5;6"+#10)``

The “#10” in the inputData example indicate new-line characters. I.e. we define the following dummy CSV data:

"a";"b";"c"

"1";"2";"3"

"4";"5";"6

9

Click on OK.

10

Click on YES.

The table refresh fails, and in the error log you should see the “Hello World!” message

The location of the script file is determined by the table scope. This can be set to either Server or Workspace, or None. If set to None, the path to the script file is absolute. If it is set to Server or Workspace, the script’s location is interpreted as a relative path.

Important: You need to get the “Hello World!” example working to be able to successfully complete the remaining steps to set up an interface with an external python script.

Setting up the Data Source

To export the data that you want to process in an external script, the first step is to select the fields that you need in your python script. The way to do this is by creating a new table which contains all the fields that the python script needs. This table will be exported in .CSV format later on so that the python script can read it in.

For this example, we have an application with the a Cases table and an Events table. See illustration below.



Follow these steps.

Step

Action

1

Create a new Global table, PythonInputData.

2

Add a new expression attribute, PythonInputData_Amount.

3

Set the Type to Lookup.

4

Set the Input table to Cases_base.

5

Set the expression to listtojson(text(double(records.Amount))).

6

Set the expression level to root.

7

Click on OK.

8

Add another lookup expression attribute, for the Case_ID.

Set the expression to listtojson(text(double(records.Case_ID))).
Note:
The format of the look-up expression attribute should be a list of strings. This is because the csvtable() function will be used to transform the data into .CSV format, which expects a list of (text) records.
Because the listtojson() function requires its input to be of type text, the format of the records in the resulting CSV file depends on the type of the selected attribute (in this case, Amount is of type Currency), and the currently active display format. Here the currency type is converted to double, to make it easier to parse the records later in the python script.

Writing the Script

In your text editor, update the script.py file with the following code.

#!/usr/bin/python
import csv
import sys
def debug(message):
    sys.stderr.write(message)
# Read the CSV header. This is used so that the script will output the fields
# in the same order that they were read in. This step is optional.
column_order = [];
with open(sys.argv[1]) as csv_file:
    reader = csv.reader(csv_file, delimiter=';')
    column_order = next(reader)
# Process the input file
with open(sys.argv[1]) as csv_file:
    reader = csv.DictReader(csv_file, delimiter=';')
    # Construct the output writer.
    writer = csv.DictWriter(
        sys.stdout,
        column_order,
        delimiter=';',
        restval='',
        quoting=csv.QUOTE_ALL
    )
    writer.writeheader()
    for row in reader:
        # Get data from row
        case_id = row['Case_ID']
        amount = int(row['Amount'])
        # Do computation
        amount = amount * 2
        # Write results
        writer.writerow({'Case_ID': case_id, 'Amount': amount})
# Exit indicating success
exit(0)#!/usr/bin/python
import csv
import sys
def debug(message):
    sys.stderr.write(message)
# Read the CSV header. This is used so that the script will output the fields
# in the same order that they were read in. This step is optional.
column_order = [];
with open(sys.argv[1]) as csv_file:
    reader = csv.reader(csv_file, delimiter=';')
    column_order = next(reader)
# Process the input file
with open(sys.argv[1]) as csv_file:
    reader = csv.DictReader(csv_file, delimiter=';')
    # Construct the output writer.
    writer = csv.DictWriter(
        sys.stdout,
        column_order,
        delimiter=';',
        restval='',
        quoting=csv.QUOTE_ALL
    )
    writer.writeheader()
    for row in reader:
        # Get data from row
        case_id = row['Case_ID']
        amount = int(row['Amount'])
        # Do computation
        amount = amount * 2
        # Write results
        writer.writerow({'Case_ID': case_id, 'Amount': amount})
# Exit indicating success
exit(0)

Follow the steps below.

Step

Action

1

Upload the new script to the Workspace, overwriting the existing file and go back to the application.

2

Right click on the PythonExample table and select Edit….

3

Modify the inputData parameter of the query string for the PythonExample table:

+ '&inputData=' + urlencode(csvtable( "Case_ID", jsontolist(PythonInputData.Case_ID), "Amount" , jsontolist(PythonInputData.Amount), ))

See illustration below.

4

Click on OK.

5

Click on YES (2x).

6

Click on OK.



The PythonExample table now has two datasource attributes, Amount and Case_ID. See illustration below.


Inspecting the Amount attribute reveals that all amounts have been multiplied by two. See illustration below an example of the output generated by the python script.



Was this page helpful?

Get The Help You Need
Learning RPA - Automation Courses
UiPath Community Forum
Uipath Logo White
Trust and Security
© 2005-2024 UiPath. All rights reserved.