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
Product and Topic Expert
Product and Topic Expert
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
alejiandro_sensejl
Active Participant
0 Kudos
Hi fukuhara

THX, for you blog. It was a great help understanding Python deployment to Cloud Foundry.

I noticed that the app is accessible from the Internet without further login.

Is that correct or did I miss some step? Can you explain how to implement check for authenticated user / authorization role, please?

Cheers,

Alej
Fukuhara
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi,

 

Yes, you are right.

I haven't implemented any authentication checks.

I haven't check how to implement it.

 

Regards,

Yohei

Awesome tutorial for those who are learning how to develop/deploy python app to CF

Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos
I am trying to follow this and get almost all the way but during push I get this error:

 

Cell 578cfd64-547a-47eb-82d0-710b4a3fbd7a creating container for instance 6b6707da-7d3c-4305-81e5-f96bc447f13a
Cell 578cfd64-547a-47eb-82d0-710b4a3fbd7a successfully created container for instance 6b6707da-7d3c-4305-81e5-f96bc447f13a
Downloading app package...
Error staging application: App staging failed in the buildpack compile phase
FAILED

 

Any idea what is happening?

 
Fukuhara
Product and Topic Expert
Product and Topic Expert
0 Kudos
Will you share the whole log?
Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos
I kept at it and I had to change the Python version to 3.8.1 and it worked. At some point I saw a message indicating what versions were supported but not sure where I saw that.

I also added in the manifest.yaml something about the buildpack and cflinux -- I don't have it right now -- but I don't think that did anything.

Thanks for very helpful blog
former_member267851
Participant
0 Kudos
Hi Yohei,

Awesome Tutorial.

Any idea how to debug the cloud api.

Regards

Alok
Fukuhara
Product and Topic Expert
Product and Topic Expert
0 Kudos
> Any idea how to debug the cloud api.

I have no idea.

I usually output logs and see how processes work.

I seldom develop apps, so I'm not sure if some good way exists.

 

Yohei
wenxi_liu
Discoverer
0 Kudos
Hi Yohei,

Awesome Tutorial!

After  this step:
cf push <application name>


How can we  run the app to start without a HTTP request ? Thank you very much~


Best,
Wenxi
Fukuhara
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi,

 

It stars automatically with my example.

If you want to start tasks from cli, use the below command, though I don't remember if there are prerequisites.

cf run-task <cf app name> --command "python <file name>.py" --name taskfromcli

 

Regards,

Yohei
AbhijeetK
Active Participant
0 Kudos
Hi Yohei,

 

Indeed a nice blog. I am also using Python for one of the project where flask and chromadb is used.

In BAS I was trying to install chromadb but it was not getting install as it required Visual C++ build tools, so I did development in VScode and trying to push the app in CF.

But getting this error -- " 2023-08-11T15:07:59.61+0530 [APP/PROC/WEB/0] ERR Traceback (most recent call last):
2023-08-11T15:07:59.61+0530 [APP/PROC/WEB/0] ERR File "/home/vcap/app/app.py", line 5, in <module>
2023-08-11T15:07:59.61+0530 [APP/PROC/WEB/0] ERR from flask import Flask, render_template, request, jsonify
2023-08-11T15:07:59.61+0530 [APP/PROC/WEB/0] ERR ModuleNotFoundError: No module named 'flask'"

 

Not sure if you can suggest which can fix the error, it will be appreciated.

 

Regards,

Abhijeet
Fukuhara
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi,

 

I suspect if you failed to install Flask package.

Please check the below chapter.

1.3. Add Flask library to file “requirements.txt”


 

You can check if Flask is successfully installed using ssh.

Regards,

Yohei
AbhijeetK
Active Participant
0 Kudos
Thanks Yohei for your comment.

I have added flask==2.0.1 in requirements.txt file. only only this module but other modules are also there.

 

Regards,

Abhijeet
Fukuhara
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi,

 

Though I haven't check it, please try "Flask".

I think python packages are case dependant.

 

Regards,

Yohei
mishra_mayank
Explorer
0 Kudos
Hi Abhijeet

 

I'm also getting same error. Did you find any solution for this issue??
AbhijeetK
Active Participant
0 Kudos
Hi Mayank,

 

Instead of BAS I used VS code and deploy from there. it was there in BAS while testing locally as there are multiple dependencies missing in BAS.

 

Regards,

Abhijeet