Skip to content

FlaskGroup does not work when nested in a click.group #3263

@gustapia06

Description

@gustapia06

Expected Behavior

When using Flask CLI, I came to the case that we have a group of commands for a big program that uses click.

# main group
@click.group()
@click.pass_context
def my_big_cli_group(ctx):
    pass

# sub group
@my_big_cli_group.group()
@click.pass_context
def my_nested_group(ctx):
    pass

# command for sub group
@my_nested_group.command()
@click.pass_context
@click.option('-s', '--start', is_flag=True)
def my_command(ctx, start):
    click.echo(start)

The issue comes when nesting my flask app into the bigger group using cls=FlaskGroup and passing my create_app factory function.

# flask app sub group
@my_big_cli_group.group(cls=FlaskGroup, create_app=create_app)
@click.pass_context
def my_flask_app(ctx):
    pass

After running my setup.py pointing my entry point to 'my_big_cli = path.to:my_big_cli_group', I should expect the app to start once I do:

$ my_big_cli my_flask_app run

Actual Behavior

Instead, I get a flask.cli.NoAppException. It seems that create_app does not get passed on to Group.group instances on click.

Traceback (most recent call last):
 File “/Users/the_user/Desktop/venv/lib/python3.7/site-packages/flask/cli.py”, line 540, in list_commands
   rv.update(info.load_app().cli.list_commands(ctx))
 File “/Users/the_user/Desktop/venv/lib/python3.7/site-packages/flask/cli.py”, line 393, in load_app
   ‘Could not locate a Flask application. You did not provide ’
flask.cli.NoAppException: Could not locate a Flask application. You did not provide the “FLASK_APP” environment variable, and a “wsgi.py” or “app.py” module was not found in the current directory.

Work around

If I don't nest the flask app into a group but rather make it a new click.group, then after changing my setup.py to point to this new entry point as well, everything works as expected.

# flask app group
@click.group(cls=FlaskGroup, create_app=create_app)
@click.pass_context
def my_flask_app(ctx):
    pass

Then

$ my_flask_app run

works perfectly fine

Environment

  • Python version: 3.7.3
  • Flask version: 1.0.3
  • Werkzeug version: 0.15.4

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions