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
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With