1

I'm trying to push a program containing of 2 apps to Cloudfoundry - a Flask server to run a Python API and a Polymer app for frontend. Currently I'm using the following structure:

manifest.yml:

---
applications:
  - name: flask_min
    path: ./flask_min
    buildpack: https://github.com/cloudfoundry/python-buildpack 
    memory: 512M
  - name: pacing_app
    memory: 512M
    buildpack: nodejs_buildpack
    command: node server/app.js
    path: ./pacing_app/build/es5-basic

And then in the folder ./flask_min I have a Procfile:

web: python3 app.py

and app.py has a Flask server (plus the decorator for CORS for local testing that I left out from here for brevity):

app = Flask(__name__)

port = int(os.getenv("PORT", 7733))

@app.route('/hello', methods=['GET', 'OPTIONS'])
@crossdomain(origin='*')
def hello():
    return "Hello"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=port)

I can cf push it, and the instances show up running. However, when I visit the url of the flask_min app, I don't get the "Hello" it's supposed to print out, I get an HTTP ERROR 503.

As for the pacing_app, I am using the Predix Webapp Starter, except that I removed the elements in seed-app.html and just replaced it with the API call:

<template>
  <iron-ajax url="http://localhost:7733/hello" handle-as="text" last-response="{{data}}" auto></iron-ajax>        
    <p>{{data}}</p>
</template>

Locally this is working, however, on Predix it is not. First question is: How can I link to the Flask API once it's on Predix?

Also, even though the whole thing is working locally, the Polymer frontend also won't load when on Predix. I also get

Failed to load resource: the server responded with a status of 503 (Service Unavailable)

Even though the cf push seems successful, the same IS running locally when I do a gulp in the pacing-app folder and run thy python server "by hand" locally.

Do you know what I'm doing wrong? How can I set up a Polymer frontend with Predix components that uses a Python API also running on Predix?

I'm not set on using two separate apps, I just don't know how to do this with one app. I would prefer to have the Polymer app run on the NodeJS server instead of serving it from Flask because of performance and the Python/Flask server is important because I intend to run some SKLearn code in the background.

lte__
  • 7,175
  • 25
  • 74
  • 131

2 Answers2

0

I see two options for you.

  1. Use the predix-webapp-starter with NodeJS as your front end server. Use a separate Python microservice as your back end server. This approach might be better, especially if your backend SKLearn processes take a long time.
  2. Run everything in a single Python/Flask server. You could look at the Predix Digital Volcano App as an example. This approach might be easier if you have more experience with Python.
gstroup
  • 1,064
  • 8
  • 16
0

The combination of polymer and flask requires a bit of 'adjusting' for the flask server to serve the correct polymer folder... Starting from a simple flask server as below:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return app.send_static_file('index.html')

@app.route('/<path:the_path>')
def all_other_routes(the_path):
    return app.send_static_file(the_path)

if __name__ == '__main__':
    app.run(debug=True)

and by using the polymer starter kit in a new folder:

mkdir polymer && cd polymer && polymer init polymer-3-starter-kit && polymer build && cd ..

Then, you either have to change the default flask serving folder (static) or you can simply create a symbolic link to your polymer build folder. I prefer to do the second, by issuing the following command on the root directory (alongside your server file):

ln -s ./polymer/build/es6-bundled ./static
lhd
  • 436
  • 4
  • 16