Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
stefan_schnell
Active Contributor
1,858
Over one year ago I published a blog how to use Python seamlessly inside ABAP. Now shivamshukla asked whether it is possible to realize ML on this way. So I tried the last days the possibilities to use TensorFlow seamlessly inside ABAP. TensorFlow is an end-to-end open source platform for machine learning. And what should I say, it worked without any problems.

I started the installation of Python, but here I used version 3.7.2, and PyWin32, here I used version 224, as I described in my blog. Now I installed TensorFlow 1.13. In a normal case you can do it easily with PIP. I used the manually way - more complicated but I saw in detail what happens.



In the next step I added in the security configuration of the SAP Logon a user rule to allow the access to the Python.Demo control.



Now I changed my Python COM server program, I added two functions TensorFlow1 and TensorFlow2 - not very imaginative but enough for testing. These functions contains test routines to check the installation of TensorFlow. Both delivers a result back.
# -*- coding: iso-8859-15 -*-
#-Begin-----------------------------------------------------------------
import sys, win32com.server.register
import platform, struct
import tensorflow as tf

class PythonDemo:
#To create a new GUID use the commands:
#>>> import pythoncom
#>>> print(pythoncom.CreateGuid())
_reg_clsid_ = "{D1B0A23F-0B6E-46D6-8880-744DBFFB86CD}"
_reg_progid_ = "Python.Demo"
_reg_desc_ = "Python Demo COM Server"
_public_methods_ = ["HelloWorld", "HelloYou", "TensorFlow1", "TensorFlow2"]

#-Function HelloWorld-------------------------------------------------
def HelloWorld(self):
return "Hello World from Python " + platform.python_version() + \
" on " + platform.system() + " (" + platform.architecture()[0] + \
")"

#-Function HelloYou---------------------------------------------------
def HelloYou(self, name="Anybody"):
return "Hello " + str(name) + " from Python"

#-Function TensorFlow1------------------------------------------------
def TensorFlow1(self):
hello = tf.constant('Hello World from TensorFlow')
sess = tf.Session()
rc = sess.run(hello)
sess.close()
return str(rc)

#-Function TensorFlow2------------------------------------------------
def TensorFlow2(self):
a = tf.constant(10)
b = tf.constant(32)
sess = tf.Session()
rc = sess.run(a + b)
sess.close()
return str(rc)

#-Sub Main--------------------------------------------------------------
def main():
if sys.argv[1].lower() == "--register":
print("Registering COM server...")
if sys.argv[1].lower() == "--unregister":
print("Unregistering COM server...")
win32com.server.register.UseCommandLine(PythonDemo)

#-Main------------------------------------------------------------------
if __name__=="__main__":
main()

#-End-------------------------------------------------------------------

Then I registered the COM server and tried the following ABAP report...
"-Begin-----------------------------------------------------------------
REPORT Z_PYTHON.

DATA:
lo_python TYPE OLE2_OBJECT,
lv_return TYPE STRING,
lo_list TYPE OLE2_OBJECT
.

CREATE OBJECT lo_python 'Python.Demo'.
CHECK sy-subrc = 0 AND lo_python-Handle > 0 AND lo_python-Type = 'OLE2'.

CALL METHOD OF lo_python 'HelloWorld' = lv_return.
WRITE: / lv_return.

CALL METHOD OF lo_python 'HelloYou' = lv_return
EXPORTING
#1 = 'Stefan'.
WRITE: / lv_return.

CALL METHOD OF lo_python 'TensorFlow1' = lv_return.
WRITE: / `Method TensorFlow1: ` && lv_return.

CALL METHOD OF lo_python 'TensorFlow2' = lv_return.
WRITE: / `Method TensorFlow2: ` && lv_return.

"-End-------------------------------------------------------------------

...with this result.



As I said above, it works so far without any problems. On this way it seems to possible to use ML seamlessly inside ABAP, in this case TensorFlow.
4 Comments
Labels in this area