Flask start-up command on azure for 'manage.py runserver' - python

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

Related

Having trouble getting Flask app running on cPanel using passenger_wsgi.py

I'm trying to get a Flask "point of sale" application working on cPanel with no success.
Here is the directory structure on cPanel File Manager
I have my app = Flask(name) in init.py
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_security import Security,SQLAlchemyUserDatastore
# from app.models import Role,User
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db, render_as_batch=True)
login = LoginManager(app)
from app import routes,models
I have tried using the line this line of code in my passenger_wsgi.py
from app import app as application
and in my pointofsale.py i used:
application = app
I have successfully created the python app and installed requirements.txt, when I try to load the link to the website but I cant reach the site.
Managed to figure out the issue, I had two files importing the application instance -: pointofsale.py and passenger_wsgi.py.
I deleted pointofsale.py, and added the following code to passenger_wsgi.py file
from app import app
application = app

Flask Heroku Correct Procfile

I'm trying to use Heroku to push my Flask app to the web. I think I have everything down, except the Procfile. Here is what my file directory looks like:
My main.py is:
from application import app
My init.py is:
from flask import Flask
from config import Config
from flask_mongoengine import MongoEngine
app = Flask(__name__)
app.config.from_object(Config)
db = MongoEngine()
db.init_app(app)
from application import routes
What is the correct Procfile command here?
Thanks

How to setup the startup command for a flask azure web app

I've created a simple flask web app and managed to publish it with CI/CD from azure devops to an azure web app. The pipelines are working except the startup of my app.
If i look at the log files in the webapp this comes up -
logfile default_docker.log:
2020-01-08T13:04:17.017575225Z Documentation: http://aka.ms/webapp-linux
2020-01-08T13:04:17.017579025Z Python 3.7.5
2020-01-08T13:04:17.017582725Z Note: Any data outside '/home' is not persisted
2020-01-08T13:04:17.093756525Z Starting OpenBSD Secure Shell server: sshd.
2020-01-08T13:04:17.109540649Z Site's appCommandLine: gunicorn --bind = 0.0.0.0 --timeout 600 app: application
2020-01-08T13:04:17.110379356Z Launching oryx with: -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite -bindPort 8000 -userStartupCommand 'gunicorn --bind = 0.0.0.0 --timeout 600 app: application'
2020-01-08T13:04:17.114336587Z Oryx Version: 0.2.20191105.2, Commit: 67e159d71419415435cb5d10c05a0f0758ee8809, ReleaseTagName: 20191105.2
2020-01-08T13:04:17.116548105Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2020-01-08T13:04:17.118951024Z Build Operation ID: |DGaRVt5jG5c=.2a144509_
2020-01-08T13:04:17.554659456Z Writing output script to '/opt/startup/startup.sh'
2020-01-08T13:04:17.784203265Z Found virtual environment .tar.gz archive.
2020-01-08T13:04:17.784884970Z Removing existing virtual environment directory /antenv...
2020-01-08T13:04:17.788272497Z Extracting to directory /antenv...
2020-01-08T13:04:32.810295030Z Using packages from virtual environment antenv located at /antenv.
2020-01-08T13:04:32.817794689Z Updated PYTHONPATH to ':/antenv/lib/python3.7/site-packages'
2020-01-08T13:04:36.780635398Z usage: gunicorn [OPTIONS] [APP_MODULE]
2020-01-08T13:04:36.780670499Z gunicorn: error: unrecognized arguments: app: application
my simplefied app treeview looks like this -
test_app/
venv/
application/
templates/
__init__.py
routes.py
errors.py
models.py
forms.py
app.py
i've tried different startup commands in azure portal "general setting" but to no solution
gunicorn --bind=0.0.0.0 --timeout 600 app:application
EDIT : added app.py and init.py
app.py:
from application import app, db
from application.models import User, Post
#app.shell_context_processor
def make_shell_context():
return {'db': db, 'User': User, 'Post': Post, 'Classrooms' : Classrooms, 'ClassSession' : ClassSession, 'Teacher' : Teacher}
init.py
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from logging.handlers import RotatingFileHandler
import os
from flask_bootstrap import Bootstrap
import logging
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'
bootstrap = Bootstrap(app)
from application import routes, models, errors
if __name__ == '__main__':
# * --- DEBUG MODE: --- *
app.run(host='127.0.0.1', port=5000, debug=True)
can anybody point me a direction to where i can solve this stupid problem.
thx a lot!!
The gunicorn command isn't actually pointing to the WSGI app object. Try this instead:
gunicorn --bind=0.0.0.0 --timeout 600 application:app

Flask shell failed to to find Flask application or factory in module "flask"

I am learning 'flask_web_development' in chapter 7 which encapsulate an app to a factory
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
bootstrap.init_app(app)
mail.init_app(app)
moment.init_app(app)
db.init_app(app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
It report error when run flask shell
In [21]: !flask shell
Usage: flask shell [OPTIONS] [IPYTHON_ARGS]...
Error: Failed to find Flask application or factory in module "flask". Use "FLASK_APP=flask:name to specify one.
flask run works properly
In [19]: !export FLASK_APP=flask.py; export FLASK_DEBUG=1
In [20]: !flask run &
In [21]: * Serving Flask app "flask.py" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 301-086-427
flask shell function well in the proceeding chapters which I set it to ipython with `flask-shell-ipython'
The details
In [26]: !export FLASK_APP=flasky.py
In [27]: !flask shell
Usage: flask shell [OPTIONS] [IPYTHON_ARGS]...
Error: Failed to find Flask application or factory in module "flask". Use "FLASK_APP=flask:name to specify one.
and the flasky.py
In [28]: !cat flasky.py
import os
import click
from flask_migrate import Migrate
from app import create_app, db
from app.models import User, Role
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
migrate = Migrate(app, db)
#app.shell_context_processor
def make_shell_context():
return dict(db=db, User=User, Role=Role)
#app.cli.command()
#click.argument('test_names', nargs=-1)
def test(test_names):
"""Run the unit tests."""
import unittest
if test_names:
tests = unittest.TestLoader().loadTestsFromNames(test_names)
else:
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
Before 'flask run &' you are exporting 'FLASK_APP=flask.py'
Before 'flask shell' you are exporting 'FLASK_APP=flasky.py'
There is a difference in both the names.

How to add custom build/deploy commands on OpenShift

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.

Categories