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
- Setting up the Server Settings;
- Setting up the workspace;
- Setting up a script data source;
- Setting up the data source;
- 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
|
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: def debug(message): debug("Hello world!") exit(1)`` Note: The |
3 | Save the text file as |
4 | Upload the |
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 |
4 | Right click the |
5 | In the Table Options dialog, set the Table scope to Workspace. |
6 | Double click on the |
7 | Enter the following as Connection string: |
8 | Enter the following as Query:
The “#10” in the inputData example indicate new-line characters. I.e. we define the following dummy CSV data: |
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, |
2 | Add a new expression attribute, |
3 | Set the Type to Lookup. |
4 | Set the Input table to Cases_base. |
5 | Set the expression to |
6 | Set the expression level to root. |
7 | Click on OK. |
8 | Add another lookup expression attribute, for the 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)
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 |
3 | Modify the |
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.
Updated about a year ago