I'm pretty new to Python and Flask. I'm trying to execute a Flask app from the command line like :
python manage.py run
with 'run' being an argument I can change for different functions.
My code is this :
manage.py
import logging
from flask.cli import FlaskGroup
from server.app.main import create_app
logging.basicConfig(level=logging.DEBUG)
_logger = logging.getLogger(__name__)
app = create_app('dev')
cli = FlaskGroup(app)
@cli.command('run')
def run():
_logger.info(f'run')
return app.run(app.config.get('HOST'), app.config.get('PORT'))
if __name__ == '__main__':
_logger.info(f'main')
cli()
__init.py__
from flask import Flask
import logging
# import some config - ommitted for brevity
logging.basicConfig(level=logging.DEBUG)
_logger = logging.getLogger(__name__)
def create_app(config):
_logger.info(f'requesting config name: {config}')
app = Flask(__name__)
return app
When i run from the command line with Python it fails
python manage.py run
INFO:server.app.main:requesting config name: dev
INFO:__main__:main
Usage: manage.py run [OPTIONS]
Try 'manage.py run --help' for help.
Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.
(venv) edge@edge-dev-machine:~/dev/flaskstuff/server/app$
it works with Flask from the command line
flask --app manage.py run
INFO:server.app.main:requesting config name: dev
* Serving Flask app 'manage.py'
* Debug mode: off
INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
INFO:werkzeug:Press CTRL+C to quit
I figure I have something wrongs with the Flask CLI. Not sure what though.
So any help getting it to work with Python like :> python manage.py run bearing in mind i'll have a number of other argument as well as 'run'
Thanks