I'm trying to properly build and deploy a Flask application to OpenShift.
The application uses SQLAlchemy as an ORM, and Flask-Migrate to do the database migrations.
My app resides in autoapp.py with the following content:
# -*- coding: utf-8 -*-
"""Create an application instance."""
from tcst_api.app import create_app
from tcst_api.utils import get_config_object
CONFIG = get_config_object()
application = create_app(CONFIG)
if __name__ == '__main__':
application.run()
To initialize the app and start it, I do the following locally:
export FLASK_APP=autoapp.py
flask db init
flask db migrate
flask db upgrade
flask run
On the OpenShift side, I managed to set up the FLASK_APP environment variable, and the platform also can hook the source and is able to build it.
However I don't know where can I inject the flask db ... commands into the process.
I use gunicorn to serve the app, and OpenShift manages to find.
Related
Starting my Flask app using:
flask run
Doesn't appear to work... and I get the error message:
Error: While importing 'entry', an ImportError was raised.
however if I run:
python entry.py
The app will build successfully? Why is this? Both FLASK_APP and FLASK_ENV are set correctly, here is my folder structure:
entry.py:
from application import create_app
app = create_app()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
application/init.py:
import os
from flask import Flask
from config import DevConfig, TestConfig, ProdConfig
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
from application.models import Report
def create_app(testing=False):
app = Flask(__name__)
flask_env = os.getenv("FLASK_ENV", None)
# Configure the application, depending on the environment
if testing:
app.config.from_object(TestConfig)
elif flask_env == "development":
app.config.from_object(DevConfig)
elif flask_env == "production":
app.config.from_object(ProdConfig)
else:
raise ValueError("FLASK_ENV is not set or there is an unknown environment type!")
# init plugins, if any
db.init_app(app)
if flask_env == "development":
with app.app_context():
db.create_all()
db.session.commit()
# register blueprints
register_blueprints(app)
return app
def register_blueprints(app):
from application.main import main_blueprint
app.register_blueprint(main_blueprint)
If you want to start the application through the flask executable, then you have to consider that flask will look for the app.py file containing the app application, if not (as in your case), then you will have to correctly set the value of the environment variable FLASK_APP, which will be equal to FLASK_APP=entry.py:app
On Linux, macOS:
$ export FLASK_APP=entry.py:app
$ flask run
On Windows:
$ set FLASK_APP=entry.py:app
$ flask run
Take a look here: Run The Application.
Here, however, it is explained how flask looks for the application to run
In this case (python entry.py) everything works correctly, because flask is invoked via python inside the main section, which instead is not called if entry.py is executed directly from flask, in fact flask will not enter the main section, but will look for the app.py file and the app variable inside it. (Obviously it will look for entry.py and app if you have configured the environment variable correctly)
I have a flask app deployed on Heroku right now. I wrote a script to update the database everyday and scheduled it to run everyday using Heroku Scheduler. The script runs fine but I am finding that it is not updating the database.
To deploy my app I have a wsgi.py
from my_project import create_app
app = create_app()
My Procfile
web: gunicorn wsgi:app
My script just imports a function I have in my init.py which looks like this:
def job(app):
with app.app_context():
# update database and execute job
My script:
from wsgi import app
from my_project import job
job(app)
My question is why the db is not updating. Is it because I have the app_context? So am I updating the db, but not in the correct context? Is it correct to import the app from wsgi.py? Is it the correct app that is being used in Heroku?
I am unable to run the python manage.py runserver command from Azure. My manage.py code is
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from flask_migrate import upgrade as upgrade_database
from glogic import app, db, prepare_app
prepare_app(environment='ms')
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == "__main__":
manager.run()
Where glogic is the package for the bulk of my python files. The prepare app method is called from the __init__.py file which is as follows:
from glogic.config import config_env_files
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# initialises app and db
db = SQLAlchemy()
app = Flask(__name__)
# prepares the app and db environment based on config files
def prepare_app(environment='ms', p_db=db):
app.config.from_object(config_env_files[environment])
p_db.init_app(app)
# load views by importing them
from . import views
return app
Running and hosting locally it all works but I have been unable to find the correct startup command or what to put in a startup.sh file in order to run the server on Azure.
When running locally, I can start it using the command gunicorn --bind=0.0.0.0 --timeout 600 manage:app but this isn't working on Azure
Since I moved to Google App Engine I cannot run the Flask-Migrate command python manage.py db migrate because I have exceptions regarding some GAE related imports (No module named google.appengine.ext for example).
Is there a way to run this, or an alternative, to upgrade my database on GAE?
Yes, there is a way to run it, though it's not as straightforward as you'd might like.
You need to configure your Google Cloud SQL, add yourself as an authorized user (by entering your ip address) and request to have an IPv4 address. Deal with SSL as appropriate.
Using a script:
Replacing user, password, instance_id, db_name, and path below
# migrate_prod.py
DB_MIGRATION_URI = "mysql+mysqldb://user:password#instance_id/db_name?ssl_key=path/client-key.pem&ssl_cert=path/client-cert.pem&&ssl_ca=path/server-ca.pem"
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from models import * # not needed if migration file is already generated
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = DB_MIGRATION_URI
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
Run the script as you would to migrate locally: python migrate_prod.py db upgrade, assuming your migration file is already there.
Release the IPv4, so that you're not charged for it.
I give much credit to other answers: how to connect via SSL and run alembic migrations on GAE (of which this is probably a duplicate).
I want to know the correct way to start a flask application. The docs show two different commands:
$ flask -a sample run
and
$ python3.4 sample.py
produce the same result and run the application correctly.
What is the difference between the two and which should be used to run a Flask application?
The flask command is a CLI for interacting with Flask apps. The docs describe how to use CLI commands and add custom commands. The flask run command is the preferred way to start the development server.
Never use this command to deploy publicly, use a production WSGI server such as Gunicorn, uWSGI, Waitress, or mod_wsgi.
As of Flask 2.2, use the --app option to point the command at your app. It can point to an import name or file name. It will automatically detect an app instance or an app factory called create_app. Use the --debug option to run in debug mode with the debugger and reloader.
$ flask --app sample --debug run
Prior to Flask 2.2, the FLASK_APP and FLASK_ENV=development environment variables were used instead. FLASK_APP and FLASK_DEBUG=1 can still be used in place of the CLI options above.
$ export FLASK_APP=sample
$ export FLASK_ENV=development
$ flask run
On Windows CMD, use set instead of export.
> set FLASK_APP=sample
For PowerShell, use $env:.
> $env:FLASK_APP = "sample"
The python sample.py command runs a Python file and sets __name__ == "__main__". If the main block calls app.run(), it will run the development server. If you use an app factory, you could also instantiate an app instance at this point.
if __name__ == "__main__":
app = create_app()
app.run(debug=True)
Both these commands ultimately start the Werkzeug development server, which as the name implies starts a simple HTTP server that should only be used during development. You should prefer using the flask run command over the app.run().
Latest documentation has the following example assuming you want to run hello.py(using .py file extension is optional):
Unix, Linux, macOS, etc.:
$ export FLASK_APP=hello
$ flask run
Windows:
> set FLASK_APP=hello
> flask run
you just need to run this command
python app.py
(app.py is your desire flask file)
but make sure your .py file has the following flask settings(related to port and host)
from flask import Flask, request
from flask_restful import Resource, Api
import sys
import os
app = Flask(__name__)
api = Api(app)
port = 5100
if sys.argv.__len__() > 1:
port = sys.argv[1]
print("Api running on port : {} ".format(port))
class topic_tags(Resource):
def get(self):
return {'hello': 'world world'}
api.add_resource(topic_tags, '/')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=port)
The very simples automatic way without exporting anything is using python app.py see the example here
from flask import (
Flask,
jsonify
)
# Function that create the app
def create_app(test_config=None ):
# create and configure the app
app = Flask(__name__)
# Simple route
#app.route('/')
def hello_world():
return jsonify({
"status": "success",
"message": "Hello World!"
})
return app # do not forget to return the app
APP = create_app()
if __name__ == '__main__':
# APP.run(host='0.0.0.0', port=5000, debug=True)
APP.run(debug=True)
For Linux/Unix/MacOS :-
export FLASK_APP = sample.py
flask run
For Windows :-
python sample.py
OR
set FLASK_APP = sample.py
flask run
You can also run a flask application this way while being explicit about activating the DEBUG mode.
FLASK_APP=app.py FLASK_DEBUG=true flask run