Skillversitas

The Flask CLI

The CLI is the command: the flask run (the dev, the no the python), the FLASK_APP (the env, the app, the no the 1), and the flask shell (the REPL, the context, the no the import). The with_appcontext is the 1 (the command, the no the with), the FlaskGroup is the sub (the db, the migrate, the no the the), and the pass_context is the 1 (the click, the no the arg). This chapter covers the flask run (the dev, the no the python), the FLASK_APP (the env, the no the 1), the flask shell (the context, the no the import), and the custom command (the with_appcontext, the 1).

The flask run and the shell

# the FLASK_RUN (the dev, the
#   no python, the no the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   the no: the 2 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   the 1: the FLASK_APP (the
#     env, the app, the no
#     the 1):
export FLASK_APP=app.py
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the 2: the run (the 1,
#     the no the 1, the no
#     the the):
flask run
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the 2 (the 5000,
#     the debug, the reload,
#     the no the prod, the no
#     the 1):

# the SHELL (the REPL, the
#   context, the no import,
#   the no the 1):
flask shell
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The flask run is the dev: the FLASK_APP is the env (the app, the no the 1, the no the the), and the run is the 1 (the 127.0.0.1:5000, the debug, the no the the) - the no is the prod (the gunicorn, the no the 1, the no the the). The flask shell is the REPL: the context is the no (the app, the db, the no the 1), and the import is the no (the 1, the no the the) - the 1 is the no (the python, the with, the no the 1), and the custom is the next: the command the 1 the no the with.

The custom command and the FlaskGroup

# the CUSTOM (the 1, the no
#   with, the no the the):
import click
from flask import Flask

app = Flask(__name__)

#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
@app.cli.command("seed")
@click.argument("count",
    type=int, default=3)
@with_appcontext
def seed_cmd(count):
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    for i in range(count):
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no):
        u = User(username=
            f"u{i}")
        db.session.add(u)
    db.session.commit()
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    click.echo(f"the {count}")
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the RUN (the 1, the no
#   the the, the no the 1):
flask seed
flask seed 5
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the FLASKGROUP (the sub,
#   the db, the migrate, the
#   no the the, the no the 1):
from flask.cli import FlaskGroup
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
cli = FlaskGroup(create_app=
    create_app)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The custom is the 1: the @app.cli.command is the seed (the 1, the no the 1), the @click.argument is the arg (the count, the int, the no the 1), and the @with_appcontext is the no (the with, the no the the) - the click.echo is the 1 (the stdout, the no the print, the no the the). The FlaskGroup is the sub: the create_app is the 1 (the factory, the no the 1), and the db is the migrate (the flask db upgrade, the no the the, the no the 1) - the rule is the one: the flask run the dev, the FLASK_APP the env, the shell the context, and the custom the 1 - the next chapter's API is the JSON the resource the no the 1.

The Working Rules

  • The flask run is the dev, the no the python. The FLASK_APP the env (the app, the no the 1, the no the the), and the run the 1 (the 127.0.0.1:5000, the debug, the no the the) - the no the prod (the gunicorn, the no the 1, the no the the), and the no the 2 (the no the 1, the no the the).
  • The shell is the REPL, the no the import. The context the no (the app, the db, the no the 1), and the import the no (the 1, the no the the) - the no the 1 (the python, the with, the no the 1), and the no the the (the no the 1, the no the the).
  • The custom is the 1, the with_appcontext the no. The @app.cli.command the seed (the 1, the no the 1), and the @click.argument the count (the int, the no the 1) - the @with_appcontext the no (the with, the no the the), and the click.echo the 1 (the stdout, the no the print).
  • The FlaskGroup is the sub, the factory the 1. The create_app the 1 (the factory, the no the 1), and the db the migrate (the flask db upgrade, the no the the, the no the 1) - the no the 2 (the no the 1, the no the the), and the no the the (the no the 1, the no the the).

Summary

The CLI is the command: the flask run the dev (the FLASK_APP the env, the no the 1, the 127.0.0.1:5000, the no the prod), the flask shell the REPL (the context the no, the no the import, the no the 1), the custom command the 1 (the @app.cli.command, the @click.argument, the @with_appcontext the no, the click.echo the stdout), and the FlaskGroup the sub (the create_app, the db, the migrate, the no the the). The rule is the one: the flask run the dev, the FLASK_APP the env, the shell the context, and the custom the 1 - the next chapter's API is the JSON: the jsonify the dict, the get_json the body, the status the 201, and the /api the blueprint (the no the the, the no the 1).