flask-script icon indicating copy to clipboard operation
flask-script copied to clipboard

documentation for Manager.add_option wrong?

Open amagee opened this issue 9 years ago • 2 comments

There's a code snippet in the docs as follows:

def create_my_app(config=None):
    app = Flask(__name__)
    if config:
        app.config.from_pyfile(config)

    return app

manager = Manager(create_my_app)
manager.add_option("-c", "--config", dest="config", required=False)
@manager.command
def mycommand(app):
    app.do_something()

The app argument to mycommand seems wrong; if I try to follow that pattern I get an error:

usage: manage.py runserver [-?] app
manage.py runserver: error: the following arguments are required: app

The example code code at https://github.com/smurfix/flask-script/blob/master/examples/manage.py works differently and it seems correct; if I remove the app argument from mycommand and use flask.current_app instead, everything works. Is this a mistake in the documentation or am I missing something?

amagee avatar Oct 07 '14 10:10 amagee

I'm issuing the same thing. @amagee Thanks for the hint.

Meanwhile I was able to solve it this way:

[manage.py]

from flask.ext.script import Manager
from werkzeug.serving import run_simple
from app import app, register_blueprints

# Factory app
def create_app(config=None):
    if config:
        app.config.from_object("config." + config)
    else:
        app.config.from_object("config.default")
    return app

# Create app 
manager = Manager(create_app)
manager.add_option('-c', '--config', help='Configuration')

@manager.command
def run():
    # Register blueprints
    register_blueprints(app)

    # Run WSGI application
    run_simple(
        app.config['HOST'], 
        app.config['PORT'], 
        app, 
        use_reloader=app.config['RELOAD'],
        use_debugger=app.config['DEBUG']
    )

[app/__init__.py]
# Define the WSGI application object
# The app will be configured later.
app = Flask(__name__)

def register_blueprints(app):
    # Add (fake) admin blueprint 
    from app.admin.views import admin

    # Add the main app
    from app.main.views import apps
    app.register_blueprint(apps)

Afterwards I can run:

# python manage.py -c default run

Edit: Have a look at https://github.com/dorneanu/crudappify/tree/master/apps/orgapp.

dorneanu avatar Oct 14 '14 07:10 dorneanu

@amagee that code would never work, since mycommand(app) is expecting a Flask object to be passed as a parameter in the shell, which is obviously impossible. You can only pass strings, which is all you see being passed in the example. Optional arguments (like url=None) need to be passed in the shell like this: --url=test.

In your example, you either have to remove app from the parameter and instantiate it somewhere in the module instead of in a method, or declare app as global.

nfvs avatar Dec 10 '14 08:12 nfvs