Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask heroku application error

So I do have a flask app, I can run it in my computer locally and deployed it to heroku successfully but when I do heroku open the site keeps on saying Application Error.

This is in my app.py:

import random
import os

from flask import Flask
from gen_histogram import histogram
from sample import generate_probability, generate_word

app = Flask(__name__)

dict_histogram = histogram('tom_sawyer.txt')

tes_list = ["one", "fish", "two", "fish", "red", "fish", "blue", "fish"]


def try_random():
   return random.choice(tes_list)


@app.route('/')
def hello_world():
   return try_random()


if __name__ == '__main__':
   port = int(os.environ.get("PORT", 5000))
   app.run(debug=True, port=port)

Procfile:

web: gunicorn app:app

Notes:

I have everything setup including virtualenv and requirements.txt

like image 907
Buka Cakrawala Avatar asked Feb 03 '17 08:02

Buka Cakrawala


People also ask

Why my Heroku app is showing application error?

There are some errors which only occur when the app is rebooting so you will need to restart the app to see these log messages appear. For most apps, we also recommend enabling one of the free logging addons from https://elements.heroku.com/addons#logging to make sure that your historical log data is being saved.

How do I run a flask app on Heroku?

Deploying Flask App on HerokuSTEP 1 : Create a virtual environment with pipenv and install Flask and Gunicorn . STEP 2 : Create a “Procfile” and write the following code. STEP 3 : Create “runtime.

Does Heroku work with flask?

In this tutorial, you'll create a Python Flask example application and deploy it using Heroku, making it publicly available on the web. Heroku removes much of the infrastructure burden related to building and running web applications, allowing you to focus on creating an awesome app.


2 Answers

I think a likely possibility is that that gunicorn is not using the correct port. Heroku assigns a port for the application. I'm not entirely sure if that port gets assigned randomly or if it has a default. But if this is what is causing the problem, changing the Procfile to this should fix it:

web: gunicorn -b :$PORT app:app

This way catches whatever port assignment Heroku does. Or if you choose to set an environment variable for PORT it will also use that. gunicorn defaults to port 8000, so setting the PORT environment variable to 8000 on Heroku should also work.

  • Related: Deploying Flask app to Heroku

I'm pretty sure that the app.run does not effect the gunicorn server in any way. I think that gunicorn just finds the application instance in the module specified by app:app (module:appinstance), and loads the views.

  • ref: http://docs.gunicorn.org/en/stable/settings.html#bind
like image 181
wgwz Avatar answered Oct 17 '22 00:10

wgwz


I created a file called Procfile in my root folder containing

web: gunicorn -b :$PORT app:app

Im my app.py, i have

import os
...
port = int(os.environ.get('PORT', 5000))
...
app.run(host='0.0.0.0', port=port, debug=True)

Heroku dyno will set it's own PORT env and the app will use that rather than the default 5000 i use locally.

Working example at https://github.com/Sean-Bradley/Seans-Python3-Flask-Rest-Boilerplate

like image 39
Sean Bradley Avatar answered Oct 17 '22 01:10

Sean Bradley