4

Using flask-script's add_option method I'm trying to pass the name of a config file into my create_app() so I can configure from_pyfile() -- Flask Instance Folders

I used this gist to get me started.

manage.py

from fbone import create_app

app = create_app()
manager = Manager(app)
manager.add_option('-c', '--config', dest='config', required=False)

app.py

def create_app(config=None, app_name=None, blueprints=None):
    """Create a Flask app."""

    print config

This is just a snippet of my create_app function but I'm starting the app like this:

$ python manage.py -c config/heroku.cfg runserver
None
/env/lib/python2.7/site-packages/flask_script/__init__.py:153: UserWarning: Options will be ignored.
* Running on http://127.0.0.1:5000/
* Restarting with reloader
None

As you can see, instead of printing config/heroku.cfg it prints None I think this is because of the UserWarning from flask script but I can't find out why that's happening.

Ashoka Lella
  • 6,631
  • 1
  • 30
  • 39
frankV
  • 5,353
  • 8
  • 33
  • 46
  • OK. According to the docs "For this to work, the manager must be initialized with a factory function rather than a Flask instance. Otherwise any options you set will be ignored." but I'm already doing this. – frankV Aug 02 '14 at 14:31

1 Answers1

4

It turns out you are creating the flask object by calling create_app() (with the parens).

If you do

app=create_app

or

Manager(create_app)

then you should be able to use add_option()

SW_user2953243
  • 334
  • 1
  • 12
  • it defaults to `None` for logical reasons. The `add_option` method should pass the argument following the flag to the `dest` defined, in this case that's `config`. If i'm mistaken, please expand your answer. – frankV Aug 02 '14 at 14:55
  • Unless there is some relevant code missing...I'm simply following the flow of code. You're printing `config` when it is defaulted to None. `add_option` is called AFTER the print statement is executed in `create_app()`. – SW_user2953243 Aug 02 '14 at 15:16
  • `add_option` is not "called" after `create_app`. `create_app` is the application factory passed into manage.py. Flask-Script, (or the `Manager` object) has a set of default commands, like `runserver` which you can `add_option`s to, and it will then instantiate the app (using the factory in this case) and pass those options as the arguments. – frankV Aug 02 '14 at 15:21
  • app = create_app() `create the app with no params, printing None for config` manager = Manager(app) `wrap manager around the app previously created` manager.add_option('-c', '--config', dest='config', required=False) `add_option called after create_app...` – SW_user2953243 Aug 02 '14 at 15:25
  • I think I see what you're saying. You're talking about the 2nd time None is printed. – SW_user2953243 Aug 02 '14 at 15:28
  • can you provide your complete source for the create_app() method? – SW_user2953243 Aug 02 '14 at 15:32
  • I will but it's not relevant. – frankV Aug 02 '14 at 15:36
  • I thought perhaps it was not returning a FLASK object. But it turns out you need to call Manager(create_app) instead of Manager(app). If you pass an instance to Manager() it won't allow options to be used. You have to instead pass the create_app method itself. It's documented in the __init.py__ source. It also correctly reflects what's in the example code you linked to in your question. – SW_user2953243 Aug 02 '14 at 15:39
  • Did you revise your code to call Manager(create_app)? – SW_user2953243 Aug 02 '14 at 15:42
  • 1
    had the same problem, OP. The weird thing is that the documentation doesn't point it out very explicitly, even though all the previous examples were like your original code – corvid Aug 02 '14 at 18:15