Technical Articles
Deploying Flask + Python web application and Linking Node web application
【Introduction】
In this blogs, I will explain about the way of deploying a web application to SAP Cloud Platform. Especially, I will explain about Flask + Python web application.
After deploying the web application, I will connect node to flask app.
【IT Architechture】
I will show IT Architecture outline below.
Flask is a lightweight web application framework.
Node web app connects to flask + python web application on Cloud Foundry environment.
【Topic/table of Content】
- Preparation
- PyCharm installation
- Flask + Python Web Application Creation
- Python Implementation and Configuration
- Launching Server
- Flask + Python Web Application Deployment
- Node.js and Flask Web Application Data Linkage
【Environment】
You need to prepare Cloud Foundry environments and HANA as a Service.
Cloud Environment :Cloud Foundry environment.
Database :HANA as a Service(CF)
PC/Service :1 PC (※ In my case, I used WindowsPC)
【Version】
WindowsPC Edition :Windows 10 Enterprise 2016 LTSB
CPU :Intel(R)Core(TM)i5-7300U CPU @ 2.60GHz 2.71GHz
Memory(RAM) :8.00GB
HANA version(CF) :4.10.5
【1. Preparation】
At first, you need to install Python 3.x.
C:\Users\shimizuyt>python -V
Python 3.7.0
【2. PyCharm instration】
You can set up PyCharm following the step below.
- Access the official web site
https://www.jetbrains.com/pycharm/ - Click the download button on pycharm
- Click the download button on community edition
- Run 「pycharm-community-2019.1.3.exe」 file
【3. Flask + Python Web Application Creation】
Next step, you create python project on your directory
- Run PyCharm
- Create your project
- Move your project root directory
- Open your terminal and install Flask
【4. Python Implementation and Configuration】
You make a python code and configure on your project.
- Create 「app.py」 file
- Implement your method and host/port
import os from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello Yutaro Shimizu!" if __name__ == "__main__": osPort = os.getenv("PORT") if osPort == None: port = 5000 else: port = int(osPort) app.run(host='0.0.0.0', port=port)
- Create 「manifest.yml」 and define your application name
--- applications: - name: flaskwebapp host: flaskwebapp memory: 512M disk_quota: 1028M timeout: 60 buildpack: python_buildpack
- Create 「Procfile」
web: python app.py
- Create 「requirements.txt」
Flask
- Create 「runtime.txt」 and specify Python version
python-3.7.3
【5. Launching Server】
You run your application server and check the web app on your browser.
- Type 「python app.py」
- Open your browser
- Access 「localhost:5000」
You can see your web application.
【6. Flask + Python Web Application Deployment】
You deploy flask application to SAP Cloud Platform.
- Type login command「cf login -a https://api.cf.eu10.hana.ondemand.com」
- Type your email and password
- Deploy your web application to a specified space
- Access [https://flaskwebapp.cfapps.eu10.hana.ondemand.com/]
You can see your web application.
【7. Node.js and Flask Web Application Data Linkage】
Node.js try to call a flask web application. Any kind of Node.js is acceptable.
- Add some kind of method on flask web application
import os from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello Yutaro Shimizu!" @app.route("/customer") def findAllCustomer(): return "[{'ID':1,'CUSTOMER_NAME':'AAA Company','CUSTOMER_ADDRESS':'Tokyo','CUSTOMER_TEL':'xxx-xxxx-xxxx'},{'ID':2,'CUSTOMER_NAME':'BBB Company','CUSTOMER_ADDRESS':'Tokyo','CUSTOMER_TEL':'xxx-xxxx-xxxx'},{'ID':3,'CUSTOMER_NAME':'CCC Company','CUSTOMER_ADDRESS':'Tokyo','CUSTOMER_TEL':'xxx-xxxx-xxxx'}]" if __name__ == "__main__": osPort = os.getenv("PORT") if osPort == None: port = 5000 else: port = int(osPort) app.run(host='0.0.0.0', port=port)
- Create Node.js application and open app.js
- Implement Request method on app.js
/** * Find all customers */ app.use("/api/datalink/customer", function (req, res, next) { var options = { method: 'GET', url: 'https://flaskwebapp.cfapps.eu10.hana.ondemand.com/customer', headers: { 'cache-control': 'no-cache', Connection: 'keep-alive', 'accept-encoding': 'gzip, deflate', Host: 'flaskwebapp.cfapps.eu10.hana.ondemand.com', 'Cache-Control': 'no-cache', Accept: '*/*', 'User-Agent': 'PostmanRuntime/7.15.0' } }; return request(options, function (error, response, body) { if (error) throw new Error(error); res.json(body); }); });
- Type login command「cf login -a https://api.cf.eu10.hana.ondemand.com」(※ This is the same way of Flask + Python web application)
- Type your email and password
- Type your org
- Push your Node.js application
- Open your browser
- Follow the link below
「https://nodewebapp.cfapps.eu10.hana.ondemand.com/api/datalink/customer」
【Summary】
To sum up, you can establish an application server with flask and deploy the web application.
It is possible to connect flask web application from node.js.
Thanks Yutaro Shimizu for the blog. One question though When you have already exposed the endpoints via your python application why do you need to use Nodejs, any frontend application can already consume it. I did not understand the context is it for demonstration that Node can consume flask python API's?
Hello Yutaro Shimizu,
Thanks for the article. I am able to write and run the script in the localhost. I am able to even deploy to the cloud. But I get the error "502 Bad Gateway: Registered endpoint failed to handle the request." when I run it.
Could you please help me ? Did you face this problem ?
Regards,
Sushanth