-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update app factory docs for Flask's CLI #2027
Comments
So would best practice be to create a runner/instantiation script? (For using the development server) |
Yeah. Also I'm not sure why "multiple instances" is mentioned as usecase, since we already use locks in Flask to ensure that one app object can be used with more than one server in one process. |
I create a
from flask import Flask
from flask.cli import FlaskGroup
def create_app(info=None):
app = Flask('my_app')
...
return app
cli = FlaskGroup(create_app=create_app)
setup(
...,
entry_points={
'console_scripts': [
'my_app=my_app:cli.main',
],
},
)
You can of course adapt this to use a separate file for the cli so it can be called without installing too, but I'd rather encourage the install pattern. |
@davidism how do you make use of the kwarg "info"? |
I don't, but it has to be there to make Click happy. I think it can be used to pass data around within Click. |
I think the factory function should be coded in a way that it is straightforward to call directly. I do not like the idea of adding the I honestly haven't found a way to use an app factory with Click that matches what you can do with Flask-Script. This is how I typically code my app factory functions: def create_app(config_name=None):
if config_name is None:
config_name = os.environ.get('FLASK_CONFIG', 'development')
app = Flask(__name__)
...
return app With this structure, Flask-Script can be given the app factory and it will call it without arguments, which runs the The closest I have achieved with Click requires the use of a |
I hadn't considered optional arguments even though @mbr pointed that out repeatedly. On that basis we might actually include a basic way to use factories. On 19 September 2016 03:24:52 CEST, Miguel Grinberg notifications@github.com wrote:
Sent from my Android device with K-9 Mail. Please excuse my brevity. |
@davidism what kind of package structure do you use to add commands to the |
Cross posting from the .env discussion: One option for making this play nicer with the factory pattern is to allow making a call in the |
I would like to be able to do the same with the flask cli:
(Not necessarily the config loading, but definitely invoking the app factory) |
After reading this thread and mixing @davidism and @miguelgrinberg ideas, I have been using the following structure which allows to:
That way, I don't have to ever set
def create_app(config_name=None):
if not config_name:
config_name = os.environ.get('FLASK_CONFIG', 'development')
app = Flask(__name__)
# …
return app
def create_cli_app(info):
return create_app()
@click.group(cls=FlaskGroup, create_app=create_cli_app)
@click.option('--debug', is_flag=True, default=False)
def cli(debug):
if debug:
os.environ['FLASK_DEBUG'] = '1'
@cli.command()
def initdb():
"""Initialize the database."""
db.create_all()
# …
if __name__ == '__main__':
cli()
setup(
# …
entry_points={
'console_scripts': [
'app=app.commands:cli',
],
},
) |
see #1536
The text was updated successfully, but these errors were encountered: