I was deploying a flask applications to Azure App Services with the following (example) file structure.
flask-app
├── application.py
└── web
└── __init__.py
Inside __init__.py I declared app = Flask(__name__) (and of course do other things such as setup the configuration and add the controllers). Then, inside application.py, I just did from web import app.
When attempting to deploying this to Azure App Services, I would receive the following error when App Services would try to start the application: "Failed to find attribute 'app' in 'application'".
In order to fix this, I modified application.py to do the following:
from web import app
app = app
And then the app was able to be deployed. Of course, I'm somewhat skeptical that this is a proper fix or if what was happening was a bug, but I thought I would share this in case it helped others in the future.
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 have a python flask API in azure app services. When deploying to it from vs code, I get a "successful deployment" message, but the app still shows the default initial web page provided as a template from Microsoft for new app services.
The current file structure looks something like this:
├───README.md
├───docs
├───data
└───src
├───main.py
└───other_files.py
I changed the file structure to look like the following:
├───README.md
├───docs
├───data
├───app.py
└───src
└───other_files.py
After deploying it like this, the application was able to start normally instead of displaying the boilerplate webpage from Microsoft. What do I need to do to have the app.py inside the src directory?
Can it actually be done?
I was able to solve it on my own by providing a custom startup command in the configuration of the app service. It can be done from the azure portal, in the "Configuration" section, or with the azure CLI.
az webapp config set --resource-group <resource-group-name> --name <app-name> --startup-file "<custom-command>"
And the custom command was:
gunicorn --bind=0.0.0.0 --timeout 600 --chdir src main:app
More details of this here: https://learn.microsoft.com/en-us/azure/app-service/configure-language-python#customize-startup-command
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
I am new to flask and GAE. I am trying to deploy a simple flask app to GAE. I am using https://github.com/kamalgill/flask-appengine-template/ as the template for deploying.
When I run
dev_appserver.py src/
I get the following error -
Debugged import:
- 'application' found in '/home/murtaza/workspace/flask/sim-sim/src/application/__init__.py'.
- 'application.settings' not found.
Below is the code from the application.settings file -
"""
Initialize Flask app
"""
from flask import Flask
app = Flask('application')
app.config.from_object('application.settings')
import urls
What is the utility of application.settings, is it a GAE or flask config file, or a custom file for the above template that can be ignored ?
Any other templates / approaches for deploying flask on GAE? Or sample flask project on GAE?
This is a flask config. Take a look here: http://flask.pocoo.org/docs/config/#configuring-from-files
I haven't tried this, but it looks like a decent start: https://github.com/kamalgill/flask-appengine-template/
There are also a bunch of other boilerplate templates if you google for it.
An application that uses Flask framework, can start the built-in werkzeug development server simply by calling app.run() :
from flask import Flask
app = Flask(__name__)
app.run()
Consider an empty Django project "myproject" created as follows: django-admin.py startproject myproject. Is there a non-hackish way to start Django's development server for myproject as it's done in Flask? Thank you.
You can use management.call_command() to emulate what manage.py does:
from django.core import management
management.call_command('runserver')
See https://docs.djangoproject.com/en/dev/ref/django-admin/