Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

documentation for Manager.add_option wrong? #118

Open

Description

@amagee

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?

Activity

  1. dorneanu commented on Oct 14, 2014

    @dorneanu

    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.

  2. nfvs commented on Dec 10, 2014

    @nfvs
    Contributor

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions