public
Last active

Flask-Script: demo passing in configuration file.

  • Download Gist
README.md
Markdown

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 runserver -c development.cfg

gives "Hello, developer!".

app.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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
development.cfg
1
HELLO = 'developer'
manage.py
Python
1 2 3 4 5 6 7 8 9 10
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()
production.cfg
1
HELLO = 'world'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.