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.
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.
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.
- 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|
- Download the Python-3.6.x sources from https://www.python.org/downloads/ and extract them to a local folder.
- 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:
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 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:
- As we are using SAP developed python modules, to get these modules please follow the below steps:
- Go to SAP Support Portal
- Click on Support Package and Patches
- Expand by Alphabetical Index (A-Z)
- Click on Alphabet X
- Select XS PYTHON 1
- Download ZIP
- Extract the above ZIP to the folder sap_python_dependencies (you need to create this folder locally in some location)
- 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
- Copy the downloaded mta.jar into the root folder of the SHINE project
- 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:
For example, in HANA express the UAA endpoint can be,
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
- After the build is done, navigate to the corresponding space where you want the application to be deployed.
xs target -s <space-name>
- And then, deploy the generated mtar using the following command:
xs deploy shine.mtar
- Open the deployed shine-web application’s URL to see the running application.
- Click on the Purchase Order Worklist tile.
- 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.
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.