Skip to Content
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】

  1. Preparation
  2. PyCharm installation
  3. Flask + Python Web Application Creation
  4. Python Implementation and Configuration
  5. Launching Server
  6. Flask + Python Web Application Deployment
  7. 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.

  1. Access the official web site
    https://www.jetbrains.com/pycharm/
  2. Click the download button on pycharm
  3. Click the download button on community edition
  4. Run 「pycharm-community-2019.1.3.exe」 file

 


【3. Flask + Python Web Application Creation】

Next step, you create python project on your directory

  1. Run PyCharm
  2. Create your project

  3. Move your project root directory
  4. Open your terminal and install Flask

 


【4. Python Implementation and Configuration】

You make a python code and configure on your project.

  1. Create 「app.py」 file
  2. 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)
    ​
  3. Create 「manifest.yml」 and define your application name
    ---
    applications:
    - name: flaskwebapp
      host: flaskwebapp
      memory: 512M
      disk_quota: 1028M
      timeout: 60
      buildpack: python_buildpack​
  4. Create 「Procfile」
    web: python app.py​
  5. Create 「requirements.txt」
    Flask​
  6. 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.

  1. Type 「python app.py」

  2. Open your browser
  3. Access 「localhost:5000」

You can see your web application.

 


【6. Flask + Python Web Application Deployment】

You deploy flask application to SAP Cloud Platform.

  1. Type login command「cf login -a https://api.cf.eu10.hana.ondemand.com」
  2. Type your email and password
  3. Deploy your web application to a specified space

  4. 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.

  1. 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)
    
  2. Create Node.js application and open app.js
  3. 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);
      });
    });
    ​
  4. Type login command「cf login -a https://api.cf.eu10.hana.ondemand.com」(※ This is the same way of Flask + Python web application)
  5. Type your email and password
  6. Type your org
  7. Push your Node.js application
  8. Open your browser
  9. 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.

2 Comments
You must be Logged on to comment or reply to a post.
  • 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