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: 
Fukuhara
Advisor
Advisor
Hi All,

I am writing this blog to describe some easy steps to deploy a Python Flask based REST API application on the SAP cloud foundry environment.

Python is very useful to deal with many requirements, since it has so many powerful libraries. In my case, I recently used it to connect web application and TensorFlow serving for object detection. The python application resizes image file and converts data format.

For Flask logging, please see another article "Logging from Python Flask application deployed on Cloud Foundry".

Environment


Local PC



  • Windows 10 Professional

  • Python 3.6.6  on Anaconda

  • Flask 1.0.2

  • cf CLI 6.37.0


Cloud Foundry



  • Python Build pack 1.6.20

  • CF Trial (Europe - Frankfurt)


Prerequisites



  • your space is created on Cloud Foundry environment

  • cf CLI is installed on Local PC(see the official page for the installation)


Procedure


1. Prepare for python application


The python application is on my Github repository, so just clone the repository is also OK here.

1.1. Application directory


Create an application directory on local PC. I created the one named "cloudfoundry-python-flask-sample".


1.2. Flask application(hello.py)


The application is quite simple, because it just return "Hello World".  It works also on local window PC.
from flask import Flask
import os

app = Flask(__name__)
# Port number is required to fetch from env variable
# http://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#PORT

cf_port = os.getenv("PORT")

# Only get method by default
@app.route('/')
def hello():
return 'Hello World'

if __name__ == '__main__':
if cf_port is None:
app.run(host='0.0.0.0', port=5000, debug=True)
else:
app.run(host='0.0.0.0', port=int(cf_port), debug=True)

1.3. Add Flask library to file "requirements.txt"


Add Flask library to file "requirements.txt".  If you want other libraries, just add them.
Flask

1.4. Python runtime version(runtime.txt)


Just write python runtime version. The version should be listed here.  As of 2018/12/12 I used python Buildpack 1.6.20, which was default on SAP Cloud Platform CF.

 
python-3.6.6

To check cf buildpack version, just use "cf buildpacks" command.

1.5. Manifest(manifest.yml)


This is so simple application that it does not need much resources.  Please make sure don't allocate much resources.
---
applications:
- memory: 128MB
disk_quota: 256MB
random-route: true

1.6. Commands run the application(Procfile)


Here just write down a command, which runs hello.py application.
web: python hello.py

2. Deploy the application to cloud foundry


2.1. Check cloud foundry API endpoint


See the API endpoint from SAP Cloud Platform Cockpit.


2.2. Login Cloud Foundry using cf CLI


Run command prompt and login to the cloud foundry.
cf api https://api.cf.eu10.hana.ondemand.com
cf login


2.3. Deploy the application


Change current directory and deploy the application.
cf push <application name>




3. Check the result


3.1. Check the result


See the application status via SAP Cloud Platform Cockpit.  There is an application Routes, so just click the link.



Here I can see "Hello World"!


Conclusion


The advantage to use Cloud Foundry is that we can concentrate on development.  For me it is so helpful that there is few time to setup environment like OS, python.
16 Comments