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
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
As for heroku, it can handle requirement.txt or setup.py
c.f. https://devcenter.heroku.com/articles/python-pip#local-file-backed-distributions
If your Python application contains a setup.py file but excludes a requirements.txt file, python setup.py develop will be used to install your package and resolve your dependencies.
If you already have a requirements file, but would like to utilize this feature, you can add the following to your requirements file:
-e .
And about run command, i think you cat put Procfile like
web: FLASK_APP=app flask run
or
web: FLASK_APP=app python -m flask run
Related
I am following the instructions here to deploy an app in Google App Engine. Everything works correctly.
Nevertheless, Google, by default, looks for the main folder (where app = Flask(__name__) is defined) in main.py. How could I redefine this? I would like to define this main folder as app.py.
Rename main.py to app.py
Add entrypoint: gunicorn -b :$PORT app:app to your app.yaml file. This is where you are telling Google to find the app object in a file called app
Add gunicorn to your requirements.txt file
Notes:
i. Because you're changing from main.py to app.py, you need to specify an entrypoint. GAE documentation says
If your app meets the following requirements, App Engine will start
your app with the gunicorn web server if you don't specify the
entrypoint field:
The root of your app directory contains a main.py file with a WSGI-compatible object called app.
Your app does not contain Pipfile or Pipfile.lock files.
ii. If you add an entrypoint, then you need to include gunicorn in your requirements.txt file
iii. I just tested the above configuration (the answer I gave) on a dev environment (Python 3.9 environment on Macbook using dev_appserver.py) and it works
I'm working on a project which used to be run on Linux for test. It's an App Engine flex project, run with gunicorn. Gunicorn does not work on Windows if I understood well, so I've been adviced to use waitress.
I also use virtualenv in my project.
So when I'm in my virtualenv, I run waitress-serve main:app (the gunicorn cmd was gunicorn -b :8080 main:app). I get an error: It had these arguments:
1. No module named flask.
I use flask. I can see the flask folder in my virtualenv folder. And when I run python then from flask import Flask I have no error.
Is there compat issue between waitress and virtualenv ? Or I'm doing something else wrong ? (already tried to delete virtualenv folder and install all the things again)
Python modules are case sensitive
Try Flask not flask.
I don't understand why if I want to run Flask application I need
(venv) $ export FLASK_APP=microblog.py
(venv) $ flask run
But if I want to run Django application I only
(venv) $ python manage.py runserver
without export DJANGO_APP=microblog.py
Why? Why I need the export app in the first case, but in the second case I don't need?
Firstly, Django and Flask are different frameworks. There's no reason why the commands to start them should be the same.
You need to export FLASK_APP to tell flask which app to run.
Doing export FLASK_APP=microblog.py sets an environment variable FLASK_APP. The flask application can then read this variable from the environment and use it to run the application.
In Python you can access environment variables from os.environ, or use the os.getenv method:
import os
flask_app = os.getenv('FLASK_APP')
If you use the django-admin command, you need to export DJANGO_SETTINGS_MODULE in a similar way:
$ export DJANGO_SETTINGS_MODULE=yourproject.settings
$ django-admin runserver
However with Django, you usually use runserver with manage.py instead of django-admin. The manage.py is specific to your project and sets the DJANGO_SETTINGS_MODULE environment variable if it hasn't been set already:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourproject.settings")
Therefore you don't need to export DJANGO_SETTINGS_MODULE when using manage.py.
From a source code prospective, seems that the FLASK_APP variabile is used for understand which flask app run in a 'multi-flask-app' environment.
Is necessary only if the app_name from the source is not present.
The FLASK_APP env variable is used only into the find_best_app method of the cli.py file of the flask framework.
Simply in your project, you don't need something like export FLASK_APP=microblog.py unless you want environment variables
from flask import jsonify,Flask
app = Flask(__name__)
#app.route("/<Your Route>/<string:<Your Param>>")
def main(<Your Param>):
//DO LOGIC HERE
data =[{'TestData1' : "" ,<YOUR OUTPUT>}]
return jsonify(data), 200
app.run(debug=False,host="0.0.0.0",port=<PORT YOU WANT TO HOST>)
Problem: When you write the command flask run in the console, how does flask know which file to run?
Solution: Thats why we use export FLASK_APP=microblog.py
It is setting the FLASK_APP (an internal flask variable) value to microblog.py
It tells flask to use microblog.py as start-up file for the application when you run flask via flask run command.
If you decide not to do so, then when you run flask run, then there is no way for flask to know which file to run. Now you could run the application using python filename.py instead of flask run
So python microblog.py in your case.
This is the Error logs I find in the heroku when deploying the python pyramid application. I have followed the each and every steps the python pyramid documentation has. Where have I exactly missed not able to figure out.
I doubt if my way of creating run file is incorrect. I have created a run.py
file and added the following code into it.
#!/bin/bash
set -e
python setup.py develop
python runapp.py
You cannot host a web app like that. You need a proper server, for example gunicorn - see the Pyramid docs on how to run with gunicorn, that is what needs to go in your Procfile. You don't need a run.py.
Try this:
Profile
web: ./run
run
#!/bin/bash
set -e
python setup.py develop
python runapp.py
runapp.py
#Heroku Startup
import os
from paste.deploy import loadapp
from waitress import serve
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app = loadapp('config:production.ini', relative_to='.')
serve(app, host='0.0.0.0', port=port)
requirements.txt
pyramid
pyramid_chameleon
pyramid_debugtoolbar
waitress
<add other dependencies here>
runtime.txt
python-3.3.0 #or whatever version you are running. Take this out
This is the error I get checking the logs on HEROKU.
The documentation of pyramid says "Create run with the following command."
So I create a file named as run.py and saved below codes. I dont know if this is the right way to create a run if not help me with that.
Here is the line of code in run.py
#!/bin/bash
set -e
python setup.py develop
python runapp.py
Here is the runapp.py
import os
from paste.deploy import loadapp
from paste import httpserver
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app = loadapp('config:development.ini', relative_to='.')
httpserver.serve(app, host='0.0.0.0', port=port)
Finally here is the procfile
$ echo "web: ./run" > Procfile
The Pyramid Community Cookbook has a deployment recipe for Heroku, assuming that is what you meant by "the documentation of Pyramid" (it's not official documentation, just a collection of recipes from the community).
In Step 1, make sure that the four files you create are done so locally in the root of your project directory.
requirements.txt
The file you named run.py should be named run.
Check that the file named Procfile was created and contains not the command to generate Procfile, but just the result of the command, specifically:
web: ./run
runapp.py