[Flask] Integrating Flask CLI as a subordinate Click group

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


I have a package with a layout like so:

    ├── __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

    def main():
        """Run the package"""


    def foo():
        """Foo the package"""

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"""

    def baz():
        """Baz the webui"""

    if __name__ == '__main__':

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,

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