Scripting

Python

10min

In this article, we will explore the power of Python scripting actions in PROCESIO. Discover how Python scripting action can enhance your automation workflows and enable you to perform complex tasks with ease. From data manipulation to advanced calculations, Python action in PROCESIO opens up a world of possibilities. Dive into the world of Python scripting and unleash the full potential of PROCESIO's automation capabilities.

Would you like to learn how to use this powerful action? Dive into this article where each step of using Python action is explained in an incremental order of complexity. It starts with printing "Hello world" and covers topics such as working with files in Python scripts.

How to configure the Python action?

Go to the Process Designers section and open a new or an existing process. Actions configuration can be performed once they are dragged and dropped on canvas.Β 

Step 1:Β Drag the Python action from Toolbar (Scripting folder) and drop it on the canvas; click on the action to open the Properties Panel.

Document image
ο»Ώ

Step 2:Β You can edit the action name. Link it to other actions:

Document image
ο»Ώ

Step 3: Create the variables needed for the configuration of the action, and then add them to the configuration panel in Output:

Document image
ο»Ώ

The output of the Python action is always a JSON having the following structure:

{

"result": result (JSON/JSON Array/Object)

}

Read below how you can extract only the JSON/Json Array/Object you need πŸ’‘

ο»Ώ

Step 4: This is why we will also add a JSON Mapper action to the process. The JSON Mapper action will extract only the object needed from the Python response (json).

ο»Ώ

Document image
ο»Ώ

For JSON Mapper configuration:

  • input JSON - is the output variable from Python action (PythonResult variable)
  • query: $.result
  • Output: a new variable of type object that will fetch only the desired object from the actual Python output
Document image
ο»Ώ

Step 5: Let's add the script to the Python action!

To clearly understand what we have done in step 4, let's run the process, and check the outputs of the 2 actions.

For this simple demonstration, keep the default script as is print("Hello world").

You can see how with JSON Mapper action we have extracted the object from the Python output:

Document image
ο»Ώ

Step 6: Let's do a more complicated scenario.

Python action has a list of available libraries to be used: JSON, Base64.

Add this script to your Python action:

Python
ο»Ώ

Check out the result:

Document image
ο»Ώ

Step 7: Let's use variables in Python action!

How do variables work in a Python action? Prior to processing the script in Python action, PROCESIO replaces the variable with the value of the variable and then utilizes it within the code.

Try out this script:

Python
ο»Ώ

ο»Ώ

number1 and number2 are process variables that you need to create upfront, like this:

ο»Ώ

Document image
ο»Ώ

We assign to those integers some default values (3 and 6), thus the result is 9:

Document image
ο»Ώ

Files cannot be added in Python script. Read below steps to discover how you can still achieve this! πŸ’‘

ο»Ώ

Step 8: Python script does not process files, however, it does process strings. So how can we use this information? PROCESIO has a File to BASE64 action, that converts the file into a string. And this result can be used in Python action.

Add a File to BASE64 action in your process:

Document image
ο»Ώ

Create variables for the file and Base64 content and them to the configuration like this:

Document image
ο»Ώ

Go to Python action.

Create a variable of type string (we named it Fisier_Base64) and add the following script:

Python
ο»Ώ

Run the process, give it a file for input and check instances.

You will see the string created in File to Base64 action to be processed by the Python action:

Document image
ο»Ώ

Python action read the string and returned the file length:

Document image
ο»Ώ

If you want to output files from Python action, use Base64 to File action, using the same conversion principles.

ο»Ώ

Updated 16 Feb 2024
Doc contributor
Doc contributor
Did this page help you?