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.
I see two options for you.
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.
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.
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
Related
After reading following docs and examples about deploying python app to evennode I've tried to do it with Flask application, but didn't succeed
https://www.evennode.com/docs/git-deployment
https://github.com/evennode/python-getting-started
Here is my main.py module's code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello, World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000)
Also I'have created conf.py file with configuration for gunicorn:
workers = 4
bind = "0.0.0.0:8000"
Then I'm running the application with gunicorn --config=conf.py main:app and all works well on my local machine. To run it on evennode I populated requirements.txt and committed above files. Then run following commands:
git remote add evennode git#git.evennode.com:your_app_here
git push evennode master
The output looks next way and I don't know what to do with it:
ssh: connect to host git.evennode.com port 8000: No route to host
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I have added my public ssh key to evennode app settings as well, so that's can't be an issue
Any help is appreciated
I have followed the google tutorial below and am able to run this on my google app engine no problem.
https://cloud.google.com/appengine/docs/standard/python3/building-app/writing-web-service
I only run into problems when I try to incorporate a kafka client in the main.py script. The main script is below and when all reference to kafka is commented out this script runs fine again in the app engine.
Can Kafka be used in an app engine? I have tried using both the internal and external kafka IP addresses on GCP with no luck... When I run "gcloud app deploy" with requirements.txt app.yaml and main.py in the same directory (as per tutorial), or even "python3 main.py" (for testing) the script hangs, there is no error message it just doesn't start the server. Again if Kafka references removed it runs fine. Kafka is running fine in the GCP project - can pub and sub from ssh terminals fine.
I have been able to make this Kafka setup work when run on a Google Compute Engine with the Flask app run directly from the VM - (running directly from the VM not using gcloud terminal). Doing this seems to allow kafka to use the internal IP address and avoids any problems with external IP addresses and cross network difficulties that Kafka doesn't seem to like.
Please help!! (This is my first ever SOF question - so be kind and be very descriptive:) )
from flask import Flask, render_template, Response
from pykafka import KafkaClient
from pykafka.common import OffsetType
clientKafka = KafkaClient(hosts='10.128.0.3:9092')
app = Flask(__name__)
topick = clientKafka.topics['gps_new']
producer = topick.get_sync_producer()
#app.route('/')
def index():
return(render_template('index.html'))
#app.route('/topic/gps_new/')
def get_messages():\
def events():
for i in
clientKafka.topics["gps_new"].get_simple_consumer(auto_offset_reset=OffsetType.LATEST,reset_offset_on_start=True):
yield 'data:{0}\n\n'.format(i.value.decode())
return Response(events(), mimetype='text/event-stream')
if __name__ == '__main__':
# This is used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
# Flask's development server will automatically serve static files in
# the "static" directory. See:
# http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed,
# App Engine itself will serve those files as configured in app.yaml.
app.run(host='127.0.0.1', port=8080, debug=True)
How can I run the full Flask Tutorial app in the Wingware IDE?
I've been using Flask under Wing Pro 7.2 for some time, and can get control because I start Flask by doing app.run() in Wing.
I conceived a wish to trace through the official working version of the completed tutorial, obtained by
git clone https://github.com/pallets/flask
This works fine (using 'flask run'), and I now have the complete source. But there's no app.run() anywhere. I tried putting one in init.py:
def create_app(test_config=None):
#...
db.init_app(app)
return app
RUN = True
if RUN:
app= create_app()
app.run()
and flask starts up, but throws an error on request 'localhost:5000/', which normally fires up a database form.
Is there a starting point in the Python code somewhere?
Or, is it possible to attach Wing to a running flask, and tell it about the source files? There is a bit in the Wing manual about attaching, but it seems to demand information about the target that we lack.
I managed to start the tutorial by creating a file main.py in the same directory as the flaskr package, with this contents:
import flaskr
app = flaskr.create_app()
app.debug = False
app.run(use_reloader=True)
Then I set this as the main debug file in Wing.
To make debugging work correctly, you may also need to set the Python Executable in Project Properties (from the Project menu) to the Python command line or activated env you want to use.
Also, it is important to set Debug/Execute > Debug Child Processes in Project Properties to Always Debug Child Processes. Otherwise the process actually running the app code is not debugged.
This works but results in a SQL error because the table 'post' does not exist if you did not already run the following first to initialize the database:
$ export FLASK_APP=flaskr
$ export FLASK_ENV=development
$ flask init-db
Once I did that, everything worked for me.
I'm trying to develop my first "large" app with Flask on Heroku and I'm attempting to combine the basic tutorial here: https://devcenter.heroku.com/articles/python with the instructions here: http://flask.pocoo.org/docs/patterns/packages/#larger-applications. It works locally with "foreman start" but when I push to Heroku I get an error that the wrong port is in use:
Starting process with command python run.py
2012-12-04T23:45:18+00:00 app[web.1]: * Running on
http://127.0.0.1:5000/ 2012-12-04T23:45:18+00:00 app[web.1]: *
Restarting with reloader 2012-12-04T23:45:23+00:00 heroku[web.1]:
Error R11 (Bad bind) -> Process bound to port 5000, should be 33507
(see environment variable PORT)
I'm new to all this, but it looks like it's trying to run "locally" on Heroku. I've tried all sorts of combinations, but can't get it to work. My very simple code right now is (the app is called "pml"):
directory: /pml
Procfile:
web: python run.py
run.py:
from pml import app
app.run(debug=True)
directory: /pml/pml/
__init__.py
from flask import Flask
app = Flask(__name__)
import pml.views
view.py
from pml import app
#app.route('/')
def index():
return 'Hello World!'
I haven't used Heroku, but to me, it looks like they have a reserved port for Flask, specifically 33507. It looks like it will try to use an environment variable, which I am not sure how to set in Heroku. The good news is you can tell Flask which port to use.
try this:
app.run(debug=True, port=33507)
and it looks like adding the PORT to the env in heroku is done like this:
heroku config:add PORT=33507
You should only have to do one of these. I would try the first as it, to me, is the straight forward way to fix the issue.
EDIT
After reading the article from your post, I see where the issue comes in.
port = int(os.environ.get('PORT', 5000))
That line says, get the value of PORT from the environment if it is set, otherwise use 5000. I am not sure why they wouldn't allow it to run from 5000 if that's what is in their docs, but I would try this change:
port = int(os.environ.get('PORT', 33507))
I'm trying to develop my first "large" app with Flask on Heroku and I'm attempting to combine the basic tutorial here: https://devcenter.heroku.com/articles/python with the instructions here: http://flask.pocoo.org/docs/patterns/packages/#larger-applications. It works locally with "foreman start" but when I push to Heroku I get an error that the wrong port is in use:
Starting process with command python run.py
2012-12-04T23:45:18+00:00 app[web.1]: * Running on
http://127.0.0.1:5000/ 2012-12-04T23:45:18+00:00 app[web.1]: *
Restarting with reloader 2012-12-04T23:45:23+00:00 heroku[web.1]:
Error R11 (Bad bind) -> Process bound to port 5000, should be 33507
(see environment variable PORT)
I'm new to all this, but it looks like it's trying to run "locally" on Heroku. I've tried all sorts of combinations, but can't get it to work. My very simple code right now is (the app is called "pml"):
directory: /pml
Procfile:
web: python run.py
run.py:
from pml import app
app.run(debug=True)
directory: /pml/pml/
__init__.py
from flask import Flask
app = Flask(__name__)
import pml.views
view.py
from pml import app
#app.route('/')
def index():
return 'Hello World!'
I haven't used Heroku, but to me, it looks like they have a reserved port for Flask, specifically 33507. It looks like it will try to use an environment variable, which I am not sure how to set in Heroku. The good news is you can tell Flask which port to use.
try this:
app.run(debug=True, port=33507)
and it looks like adding the PORT to the env in heroku is done like this:
heroku config:add PORT=33507
You should only have to do one of these. I would try the first as it, to me, is the straight forward way to fix the issue.
EDIT
After reading the article from your post, I see where the issue comes in.
port = int(os.environ.get('PORT', 5000))
That line says, get the value of PORT from the environment if it is set, otherwise use 5000. I am not sure why they wouldn't allow it to run from 5000 if that's what is in their docs, but I would try this change:
port = int(os.environ.get('PORT', 33507))