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 step 2 you can edit the action name link it to other actions step 3 create the variables needed for the configuration of the action, and then add them to the configuration panel in output 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) 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 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 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 import json def create json object() \# create a dictionary representing the json object json object = { "name" "john doe", "age" 30, "email" "johndoe\@example com" } return json object \# create the json object json object = create json object() \# print the json object print(json dumps(json object, indent=4)) check out the result 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 def add numbers(num1, num2) \# calculate the sum of the two numbers result = num1 + num2 return result \# input numbers number1 = <%number1%> number2 = <%number2%> \# add the numbers and get the result result = add numbers(number1, number2) \# print the result print(result) number1 and number2 are process variables that you need to create upfront, like this we assign to those integers some default values (3 and 6), thus the result is 9 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 create variables for the file and base64 content and them to the configuration like this go to python action create a variable of type string (we named it fisier base64 ) and add the following script import base64 def get file length(base64 data) file data = base64 b64decode(base64 data) return len(file data) \# example usage base64 variable = "<%fisier base64%>" # replace with your actual base64 variable file length = get file length(base64 variable) print("file length ", file length) 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 python action read the string and returned the file length if you want to output files from python action, use base64 to file action, using the same conversion principles