[Flask] Integrating Flask CLI as a subordinate Click group

Scott Colby scolby33 at gmail.com
Tue Aug 22 17:19:57 EDT 2017


Hello,

I have a package with a layout like so:

    top_package
    ├── __init__.py
    ├── __main__.py
    ├── cli.py
    └── web
        ├── __init__.py
        ├── app.py
        ├── blueprints
        ├── cli.py
        └── templates

The top_package is an application with an optional webui, which is provided
by Flask.

top_package/cli.py looks like this:

    from .web.cli import web as web_group

    @click.group()
    def main():
        """Run the package"""

    main.add_command(web_group)

    @main.command()
    def foo():
        """Foo the package"""
        click.echo('Foo')
    etc.

And top_package/web/cli.py looks like this:

    def create_app_for_cli(info):
        from .app import create_application
        return create_application()

    @click.group(cls=FlaskGroup, create_app=create_app_for_cli)
    def web():
        """Management script for webui"""

    @web.command()
    def baz():
        """Baz the webui"""
        click.echo('Baz')

    if __name__ == '__main__':
        web()

Unfortunately, this doesn't work.

cli_cmd --help shows the `web` command with the proper docstring and
cli_cmd web --help shows `run`, `shell`, and `baz`. But when I run any of
the web commands, I get this error:

    Usage: cli_cmd web baz [OPTIONS]

    Error: Could not locate Flask application. You did not provide the
FLASK_APP environment variable.

    For more information see http://flask.pocoo.org/docs/latest/quickstart/

Running the web cli with python -m top_package.web.cli works as expected,
though.

How do I get the Flask CLI to work this way without introducing any Flask
dependencies outside of the `web` subpackage?

Thank you,
Scott Colby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20170822/119be6c7/attachment.html>


More information about the Flask mailing list