Skip to Content
Author's profile photo Kiruthika N

SHINE with Python runtime for SAP HANA XS Advanced

This blog describes how to develop Python based applications for SAP HANA Extended Application services Advanced model, using the SHINE application as an example.

A few words about SHINE before we begin.
SAP HANA Interactive Education, or SHINE, is a demo application that makes it easy to learn how to build applications on SAP HANA extended application services advanced model.SHINE is available for free download via the service market place and also available in the GitHub.The standard SHINE application showcases the use of Nodejs and Java runtimes in XS Advanced.

In this blog we describe the implementation of business logic as a python based microservice, in SHINE.

Scenario:

The SHINE application has a scenario which illustrates a comprehensive Purchase Order (PO) Worklist that acts as an interface for a Purchase Department Manager to manage the purchase orders created by his or her department.

In this blog, we showcase the usage of python runtime in XS Advanced in a simple Export to Excel functionality in PO Worklist scenario. This feature downloads all purchase-order data into an Excel spreadsheet.

Prerequisites:

You have a system with SAP HANA 2.0 and XS Advanced installed.For details on how to install XS Advanced, see here. You can also use a HANA express installation.

Setting up the Python build from sources

You must upload a Python runtime, in your XS Advanced system, that will be used to run your Python applications. To check if a Python runtime is already setup run `xs runtimes`. If you don’t see an entry for Python you need to set one up by following the below steps:

Login to the XSA system via a terminal using the root user.

  1. Building Python from sources requires several development packages to be installed on the OS. The repositories providing these packages need to be configured and are specific on different Linux distributions. Here is a list of the packages and sample command to install it on SLES:

 

Package Install on SLES
tk-devel zypper install tk-devel
tcl-devel zypper install tcl-devel
libffi-devel zypper install libffi-devel
openssl-devel zypper install openssl-devel
readline-devel zypper install readline-devel
sqlite3-devel zypper install sqlite3-devel
ncurses-devel zypper install ncurses-devel
xz-devel zypper install xz-devel

 

  1. Download the Python-3.6.x sources from https://www.python.org/downloads/ and extract them to a local folder.
  2. Run a build from the local folder.

Before executing the command, create an empty directory python_runtime where the python runtime will be installed.

./configure --prefix=<path to python_runtime folder> --exec-prefix=<path to python_runtime folder>

make -j4

make altinstall  

The build by default will install setuptools and pip.

The build results directory i.e., python_runtime looks like:

├── bin

├── include

├── lib

└── share

 

Note: If there are any missing packages reported by the command above, you need to install the devel packages missing as explained in step-1. and re-run the ./configure command.

Create Python runtime in XS Advanced

To use the built-in Python to run the applications on XS Advanced, you need to create a new runtime using the `xs create-runtime` command. Here is an example using the build directories specified in the previous section:

 xs create-runtime -p <path to python_runtime> 

 

Download/Clone SHINE

 Download or clone SHINE from here.

The python specific source code is present in the core-python folder. 

 

Vendor Python dependencies

 The overall recommendation for XSA applications, is for them to be deployed as self-contained – they need to carry all the dependencies so that the staging process does not require any network calls. The python build pack provides a mechanism for that – application can vendor their dependencies by creating a vendor folder in their root directory by executing the following steps:

  1. As we are using SAP developed python modules, to get these modules please follow the below steps:
    1. Go to SAP Support Portal
    2. Click on Support Package and Patches
    3. Expand by Alphabetical Index (A-Z)
    4. Click on Alphabet X
    5. Select XS PYTHON 1
    6. Download ZIP
    7. Extract the above ZIP to the folder sap_python_dependencies (you need to create this folder locally in some location)
  1. Execute the following command from shine/core-python directory to download the dependencies in vendor folder
 pip download -d vendor -r requirements.txt -f <sap_python_dependencies_path> 

If pip command is not recognized or if you have multiple pip versions, then you need to set the alias for the same, using the below command:

 alias pip=<path to python_runtime>/bin/pip3.6

You can check this by executing command `pip -V`.

 <python_runtime> is the directory where you have installed Python-3.6.x in above steps.

 Note: You should always make sure you are vendorizing the dependencies for the correct platform. The above steps are recommended way to vendor the dependencies specifically on Ubuntu platform. So if you are developing on anything other than Ubuntu, use the –platform flag. See pip download for more details.

Build and Deploy SHINE with python runtime on XS Advanced

Build and Deploy SHINE with Python runtime by following steps:

1.     Setup MTA Archive Builder

Follow the steps here, to download and set up the MTA Archive builder.

2.     Build and deploy the SHINE application

  1. Copy the downloaded mta.jar into the root folder of the SHINE project
  1. In the mta.yaml replace the UAA endpoint by following the steps below:a. Navigate to the resource uaab. Replace the url property of the resource uaa and controller to your respective UAA and controller end point URLs. In a port based routing system, it will be of the following format:
http(s)://<host-name>:3<instance-number>32/uaa-security​
http(s)://<host-name>:3<instance-number>30/

For example, in HANA express the UAA endpoint can be,

https://hxehost:3<instance-number>32/uaa-security

 Note: In HANAExpress VM install has default instance as 90, Binary install is a user-defined   number

3. Run the following command to build the MTAR archive:

java -jar mta.jar --build-target=XSA --mtar=shine.mtar build 

  1. After the build is done, navigate to the corresponding space where you want the application to be deployed.
xs target -s <space-name> 
  1. And then, deploy the generated mtar using the following command:
 xs deploy shine.mtar  
  1. Open the deployed shine-web application’s URL to see the running application.
  2. Click on the Purchase Order Worklist tile.
  3. Click on the Export to excel button.

The excel containing the purchase orders is downloaded via the python service.

 

 

Project Structure and Code Snippets

 Core-python module folder structure is explained below:

 requirements.txt à This file is used to specify all dependencies for the python module.

 runtime.txt à This file is used to specify the version of the python runtime.

 Procfile à This file is used to specify the command to run the python application.

 check.py à This file contains the application authorization and authentication code.

 server.py à This file contains the application logic to download the Purchase Order excel.

 

server.py

This code snippet is the rest service to download the excel which internally calls a method get_po_work_list_data(). The path is mapped using the route() decorator @app.route(‘/some/path’) in the method level.

This code snippet is responsible for querying the database and generating the excel workbook.

 

If you would like to know more about SHINE, the complete documentation can be found here.

Happy Coding!

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Mark Thienpont
      Mark Thienpont

      Great to have at least some example how to incorporate python code into an XSA framework. However, building the cloned version didn't work, as the necessary roles & authorization setup was not properly done; did I miss anything in your description for that ?

      On top : I would like to migrate a jupyter notebook content (export as python) into XSA, in order to industrialize the outcome of an EDA-process by a data scientist. So I no longer want to see the code, but just would like other user to see the nice results and visualisations the data scientist could come up with. One could it a citizen user EDA application. Is there any example how this could be performed, without too much fuzz around frontend development ? I just want to reuse whatever is possible in a notebook (e.g. via nbinteract), and not want to start javascript development, for example.