1

I'm using a custom manage.py script for my flask app, which is created with a factory pattern.

This script needs to both be able to run the server and run my tests. I use an external tool to run my tests, so I do not need to create an app to run the tests. How can I only create the app when running certain commands?

My script currently looks like:

import subprocess
import sys

from flask import current_app
from flask.cli import FlaskGroup
import click

from quizApp import create_app


@click.pass_context
def get_app(ctx, _):
    """Create an app with the correct config.
    """
    return create_app(ctx.find_root().params["config"])


@click.option("-c", "--config", default="development",
              help="Name of config to use for the app")
@click.group(cls=FlaskGroup, create_app=get_app)
def cli(**_):
    """Define the top level group.
    """
    pass


@cli.command("test")
def test():
    """Set the app config to testing and run pytest, passing along command
    line args.
    """
    # This looks stupid, but see
    # https://github.com/pytest-dev/pytest/issues/1357
    sys.exit(subprocess.call(['py.test',
                              '--cov=quizApp',
                              '--flake8',
                              '--pylint',
                              './']))


if __name__ == '__main__':
    cli()

I tried creating a second group, but it seemed that only one group can "run" at a time, so I'm not exactly sure how to solve this.

2 Answers 2

2

Use the with_appcontext parameter to your additional commands:

@click.group(cls=FlaskGroup, create_app=get_app)
def cli(**_):
    pass

@cli.command(with_appcontext=false)
def extra():
    pass
Sign up to request clarification or add additional context in comments.

Comments

0

you need to add:

cli.add_command(test)

before:

if __name__ == '__main__':
    cli()

And I am not sure if it is the only left thing you need to do. I am also study this and feel confused. But if you want to add a command in the FlaskGroup, you must do that. I feel confused when I want to use Factory and CLI in the flask project. Maybe at last you need to try Flask-Script extension, at least it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.