7

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

8
  • What is the error you get? Commented Feb 3, 2017 at 8:14
  • What do the logs say? Commented Feb 3, 2017 at 8:23
  • @DanielRoseman -----> Python app detected $ pip install -r requirements.txt -----> Discovering process types Procfile declares types -> web -----> Compressing... Done: 38.2M -----> Launching... Released v7 tweetkaka.herokuapp.com deployed to Heroku Commented Feb 3, 2017 at 8:26
  • @AdemÖztaş Basically no error output in the terminal, but when I go the site it keeps on saying Application error, "your application cannot be served" Commented Feb 3, 2017 at 8:28
  • 1
    did you look at the log? you can check like this heroku logs -t Commented Feb 3, 2017 at 8:31

2 Answers 2

12

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.