11

Deployment of Python Application on BTP for API Integration & Creation

 1 year ago
source link: https://blogs.sap.com/2022/10/03/deployment-of-python-application-on-btp-for-api-integration-creation/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
October 3, 2022 4 minute read

Deployment of Python Application on BTP for API Integration & Creation

Overview

SAP BTP capabilities can be used to expose the data as an API or connect it to a database via python application deployed on the platform. The complete process includes to offer easy app deployment and ETL via known connection details through python codes, allowing the data to be refined as per the usage for further modelling in HANA or DWC.

The Prerequisites

  1. BTP Sub-Account
  2. Authorization and Trust Management Service in BTP
  3. Job Scheduling Service in BTP for data integration
  4. Business Application Studio or any Python ide with cloud foundry application installed in the project file

Python Application Project Setup

  1. Manifest.yml – This file is first read by BTP and creates the application based on the given services and the same is bind.

        

Fig%201%20Sample%20of%20Manifest.yml

Fig 1 Sample of Manifest.yml

  1. Runtime.txt – States the runtime environment and its version to use.

Fig%202%20Sample%20of%20Runtime.txt

Fig 2 Sample of Runtime.txt

  1. Requirements.txt – Used to specify all the python library that is imported in the python file so that the application upon deploying can be installed in the BTP app environment.

Fig%203%20Sample%20of%20Requirements.txt

Fig 3 Sample of Requirements.txt

Data Consumption

Data can be consumed from an open API connection along with right authentication through python code, and from any database tables through standard connectors such as mysql or hdbcli which can be accessible in the python code.

  1. Consumption from API

The API data can be accessed via python scripting where the API is authenticated and then it can be used to send request to get data from the API endpoint.

Fig%204%20Sample%20of%20API%20call%20from%20Ariba

Fig 4 Sample of API call from Ariba

  1. Consumption from Table

Standard connectors such as mysql or hdbcli connector can be used in the python code to connect to database for read or write operations. The sensitive credentials such as username and password can be stored in the user provided variable section of the BTP app.

  • Code to access user provided variables:

Fig%205%20Sample%20of%20DWC%20Connection%20code%20snippet

Fig 5 Sample of DWC Connection code snippet

  • Setting up user provided variables in BTP:

Fig%206%3A%20Sample%20of%20DWC%20Connection%20variables%20stored%20in%20SAP%20BTP

Fig 6: Sample of DWC Connection variables stored in SAP BTP

Deployment of Python Application

Deployment to BTP can be done through Cloud Foundry CLI 7.4.0, which is by default available in business application studio (BAS).

In case of any other IDE the CF.EXE file must be available in same folder as manifest.yml and the command prompt of system should also open on same project file.

Step 1: Login to your cloud foundry system using command

cf login

Step 2:

Application deployment done in 2 ways depending on the need.

  1. ETL load

For the data load, the application is expected to shut down so that the individual python file can be triggered as and when using job scheduler task.

cf push --task

The task attribute in command represents to shut down the app after deployment.

Job Scheduling: In the Job scheduler dashboard create a task. For the same app different ETL files can be added as they are triggered based on python files.

Fig%207%3A%20Sample%20of%20task%20creation%20in%20Job%20Scheduler

Fig 7: Sample of task creation in Job Scheduler

  1. API Creation

For creating the API, flask library can be used in the python code.

The following command is used to deploy the application in BTP.

cf push app_name

In this case, the app continues to be in run state and the data is available in application route URL in SAP BTP. For the same app different API can be created based on the routes set in the flask code.

Fig%208%3A%20Sample%20of%20created%20API%20available%20in%20SAP%20BTP

Fig 8: Sample of created API available in SAP BTP

  1. Docker Image for AI-ML

In case of AI-ML codes where the libraries that are used are very RAM effective and would take the BTPs disk usage completely resulting into failure of deployment, Docker image can be created to such use cases and then deploy the app with docker image to run the code smoothly in BTP.

cf push app_name --docker-image docker_image_path --docker-username user_name

BTP platform provides easy and simple way to utilize the capabilities of python in SAP Environment, with only basic subscriptions and needs of ETL and API creations.

Conclusion

After reading the blog you will have learnt how to deploy the python application in SAP BTP platform for various required purposes such as API creations and ETL. Do let me know in comments if the blog was helpful!

Hope you have enjoyed the blog!

Reference

Developing Python in the Cloud Foundry Environment

Create a Python Application via Cloud Foundry Command Line Interface


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK