Skip to content

Instantly share code, notes, and snippets.

@smurfix
Forked from rduplain/README.md
Last active November 2, 2017 13:04
Show Gist options
  • Save smurfix/9307618 to your computer and use it in GitHub Desktop.
Save smurfix/9307618 to your computer and use it in GitHub Desktop.

This demonstrates that you can configure a Flask application through Flask-Script, without having to create a Flask instance or deal with circular dependencies. Note that Flask-Script's Manager accepts a factory function in place of a Flask app object.

Running:

python manage.py runserver

gives "Hello, world!" on http://localhost:5000/, while running:

python manage.py -c development.cfg runserver

gives "Hello, developer!".

import os
from flask import Flask
def create_app(config=None):
app = Flask(__name__)
if config is None:
config = os.path.join(app.root_path, 'production.cfg')
app.config.from_pyfile(config)
@app.route('/')
def index():
return 'Hello, %(name)s!' % {'name': app.config['HELLO']}
return app
HELLO = 'developer'
from flask.ext.script import Manager
from app import create_app
manager = Manager(create_app)
manager.add_option('-c', '--config', dest='config', required=False)
if __name__ == '__main__':
manager.run()
HELLO = 'world'
@frankV
Copy link

frankV commented Aug 1, 2014

I copied this into my project but can not seem to actually pass a value into create_app.

I tried printing the value of whats passed to -c and it returns None

Has this changed?

@dorneanu
Copy link

I don't get it. How can I access app inside my functions?

@thecrackofdawn
Copy link

well, after manager.run() is called the manager.app will be replaced with flask app instance returned by create_app. So you can access app with manager.app

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