Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 



If you are a beginner in SAP Data Intelligence (DI) and python, this blog post is for you. In this post I am going to show how you can create a python custom operator in DI based on my personal experience. Spoiler – it’s fast and easy 😊  For this purpose, we are going to use three simple examples:


  • Hello World




  • Sum of two numbers using input ports




  • Sum of two input values using configuration window




Let’s start by logging into SAP Data Intelligence and opening DI Modeler. Go to the “Operators” tab and click on “+” button to create an operator.

 


 

Then enter a name for your operator and choose “Python3 Operator” for the base operator field.

 


 

Add an output port: name “output”, data type – string.

 




 

Example 1: Hello World


 

We will start with the simplest example – print “Hello World”. Go to the “Script” tab, delete “file://script.py” string. Add the code below, upload icon for the operator and click on “Save”.


 
string = "Hello World"
api.send("output", string)

Now we are going to use this operator in the pipeline. Go to the “Graphs” tab on the left side and click on “+” button. Search for your operator in the search box (in my example it is “Python3 example”) and drag and drop it. Add “Terminal” operator and connect them. Each port has a port type. Moreover, the input and output ports must be compatible. In our example the python operator has the output port with the data type string, and “Terminal” has an input port – string, too. Save the pipeline and run it.

 


 

As soon as the pipeline has a status “running” right click on the terminal and click on “Open UI”. A new tab should be opened in the web browser:




 

Example 2:  Sum of two numbers using input ports


 

We are going to reuse the existing operator from the first example. Right click on the python operator and click on “Open operator editor”. Add two input ports “input1” and “input2” with the type “int64”. Through these input ports the python operator will receive two numbers. After this add the code below in the “Script” tab and save the operator:
def on_input(input1, input2):
sums = input1 + input2
api.send("output", str(sums))
api.set_port_callback(["input1","input2"], on_input)

Explanation:  Operators in DI react to events via their input ports. Here, we defined the function on_input that runs only when it calls (event - data is received by input ports). In this example the operator waits on input1 and input2. The variable sums has a data type integer, and the output – string. That’s why we are using the function str() to convert integer into string.

In order to use the modified operator in the modeler, you should delete it from the pipeline and drag &drop it one more time. In the pipeline below there are two more python operators that send numbers 2 and 3.


 

Example 3: Sum of two input values using configuration.


 

In the “Operator Editor” open the “Configuration” tab. To add parameters, click on the “+” button. We are going to create two input fields “input1” and “input2” for two numbers:

 


 

After that go to “Script” to change it. You will get an access to the parameters (input1 and input2) through api.config. Save the operator.
sums = api.config.input1 + api.config.input2
api.send("output", str(sums))

In the configuration window for the python operator enter numbers, save and run the pipeline.


 



So, now it's your turn to create your own first python operator in DI 😊 You can get access to the trial version here. Moreover, you will find SAP Data Intelligence tutorials for beginners here.

The provided examples should be noticed like the small illustration of python operator in SAP Data Intelligence. In the next blog post I will demonstrate you the combination of ABAP CDS Reader and python operator.

I hope you find this post helpful. For any questions or feedback just leave a comment below this post. Thanks for reading. Stay tuned 😊
15 Comments