¡Caminando hacia el éxito!

Aprende en Comunidad

Avalados por :

Deploying a Python Flask REST API on SAP Cloud Foundry: Step-by-Step Guide

  • Creado 01/03/2024
  • Modificado 01/03/2024
  • 121 Vistas
0
Cargando...
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
                
                
Pedro Pascal
Se unió el 07/03/2018
Pinterest
Telegram
Linkedin
Whatsapp

Sin respuestas

No hay respuestas para mostrar No hay respuestas para mostrar Se el primero en responder

contacto@primeinstitute.com

(+51) 1641 9379
(+57) 1489 6964

© 2024 Copyright. Todos los derechos reservados.

Desarrollado por Prime Institute

¡Hola! Soy Diana, asesora académica de Prime Institute, indícame en que curso estas interesado, saludos!
Hola ¿Puedo ayudarte?