I am trying to deploy my flask app. Usually I would have an app.py
and put all code in it.
app.py
templates/
|
|--index.html
for my really small projects. But then I have a slightly larger app and follow the larger app guide by flask. So I have this:
setup.py
app/
__init__.py
views.py
models.py
forms.py
templates/
| ---index.html
I now have all my routes and views in views.py
and running the app in __init__.py
:
from flask import Flask
app = Flask(__name__)
import app.views # Name in setup.py
if __name__ == "__main__":
app.run()
(This is just an example)
So now I follow the guide by running it with pip install -e .
and running with:
>set FLASK_APP=app(name I set in setup.py)
flask run
and it works. Except I do not know how to run it with one command. Since there is no one file to run I can not use gunicorn or anything like that. I am not sure how to go about executing this app. How would I run pip install .
on the cloud server heroku?
My problem is because I have to import the app
from __init__.py
and views using import blog.[insert import] (models, views etc.) Any help is appreciated. Thank you.
EDIT: I do not want to use blueprints though. That might be too much. My app is medium, not small but not large either
Although Flask has a built-in web server, as we all know, it's not suitable for production and needs to be put behind a real web server able to communicate with Flask through a WSGI protocol.
While lightweight and easy to use, Flask's built-in server is not suitable for production as it doesn't scale well and by default serves only one request at a time.
You absolutely can use Gunicorn to run this project. Gunicorn is not limited to a single file, it imports Python modules just the same as flask run
can. Gunicorn just needs to know the module to import, an the WSGI object to call within that module.
When you use FLASK_APP
, all that flask run
does is look for module.app
, module.application
or instances of the Flask()
class. It also supports a create_app()
or make_app()
app factory, but you are not using such a factory.
Gunicorn won't search, if you only give it a module, it'll expect the name application
to be the WSGI callable. In your case, you are using app
so all you have to do is explicitly tell it what name to use:
gunicorn app:app
The part before the :
is the module to import (app
in your case), the part after the colon is the callable object (also named app
in your module).
If you have set FLASK_APP
as a Heroku config var and want to re-use that, you can reference that on the command line for gunicorn
:
gunicorn $FLASK_APP:app
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