flask.cli with_appcontext Example Code

with_appcontext is a Flask decorator in the flask.cli module that wraps a callback to guarantee it will be called with a script's application context. Any callbacks registered with app.cli are wrapped with this function by default.

AppGroup, DispatchingApp, FlaskGroup, ScriptInfo, and pass_script_info are several other callables with code examples from the same flask.cli package.

Example 1 from FlaskBB

FlaskBB (project website) is a Flask-based forum web application. The web app allows users to chat in an open message board or send private messages in plain text or Markdown.

FlaskBB is provided as open source under this license.

FlaskBB / flaskbb / cli / main.py

# main.py
import binascii
import logging
import os
import sys
import time
import traceback
from datetime import datetime

import click
import click_log
from celery.bin.celery import CeleryCommand
from flask import current_app
from flask.cli import FlaskGroup, ScriptInfo, with_appcontext
from flask_alembic import alembic_click
from jinja2 import Environment, FileSystemLoader
from sqlalchemy_utils.functions import database_exists
from werkzeug.utils import import_string

from flaskbb import create_app
from flaskbb.cli.utils import (EmailType, FlaskBBCLIError, get_version,
                               prompt_config_path, prompt_save_user,
from flaskbb.extensions import alembic, celery, db, whooshee
from flaskbb.utils.populate import (create_default_groups,
                                    create_default_settings, create_latest_db,
                                    create_test_data, create_welcome_forum,
                                    insert_bulk_data, run_plugin_migrations,
from flaskbb.utils.translations import compile_translations

logger = logging.getLogger(__name__)

class FlaskBBGroup(FlaskGroup):
    def __init__(self, *args, **kwargs):

## ... source file abbreviated to get to with_appcontext examples ...

                   "'instance' next to the package or module is assumed to "
                   "be the instance path.")
@click.option("--version", expose_value=False, callback=get_version,
              is_flag=True, is_eager=True, help="Show the FlaskBB version.")
def flaskbb(ctx):
    if ctx.invoked_subcommand is None:

flaskbb.add_command(alembic_click, "db")

@click.option("--welcome", "-w", default=True, is_flag=True,
              help="Disable the welcome forum.")
@click.option("--force", "-f", default=False, is_flag=True,
              help="Doesn't ask for confirmation.")
@click.option("--username", "-u", help="The username of the user.")
@click.option("--email", "-e", type=EmailType(),
              help="The email address of the user.")
@click.option("--password", "-p", help="The password of the user.")
@click.option("--no-plugins", "-n", default=False, is_flag=True,
              help="Don't run the migrations for the default plugins.")
def install(welcome, force, username, email, password, no_plugins):
    click.secho("[+] Installing FlaskBB...", fg="cyan")
    if database_exists(db.engine.url):
        if force or click.confirm(click.style(
            "Existing database found. Do you want to delete the old one and "
            "create a new one?", fg="magenta")


    click.secho("[+] Creating default settings...", fg="cyan")

    click.secho("[+] Creating admin user...", fg="cyan")
    prompt_save_user(username, email, password, "admin")

    if welcome:
        click.secho("[+] Creating welcome forum...", fg="cyan")

## ... source file abbreviated to get to with_appcontext examples ...

    if fixture or all_latest:
            settings = import_string(
            settings = settings.fixture
        except ImportError:
            raise FlaskBBCLIError("{} fixture is not available"
                                  .format(fixture), fg="red")

        click.secho("[+] Updating fixtures...", fg="cyan")
        count = update_settings_from_fixture(
            fixture=settings, overwrite_group=force, overwrite_setting=force
        click.secho("[+] {settings} settings in {groups} setting groups "
                    "updated.".format(groups=len(count), settings=sum(
                        len(settings) for settings in count.values())
                    ), fg="green")

@flaskbb.command("celery", add_help_option=False,
                 context_settings={"ignore_unknown_options": True,
                                   "allow_extra_args": True})
def start_celery(ctx):
        ["flaskbb celery"] + ctx.args

@flaskbb.command("shell", short_help="Runs a shell in the app context.")
def shell_command():
    import code
    banner = "Python %s on %s\nInstance Path: %s" % (
    ctx = {"db": db}

    startup = os.environ.get("PYTHONSTARTUP")
    if startup and os.path.isfile(startup):
        with open(startup, "r") as f:
            eval(compile(f.read(), startup, "exec"), ctx)


        import IPython
        IPython.embed(banner1=banner, user_ns=ctx)
    except ImportError:
        code.interact(banner=banner, local=ctx)

@flaskbb.command("urls", short_help="Show routes for the app.")
@click.option("--route", "-r", "order_by", flag_value="rule", default=True,
              help="Order by route")
@click.option("--endpoint", "-e", "order_by", flag_value="endpoint",
              help="Order by endpoint")
@click.option("--methods", "-m", "order_by", flag_value="methods",
              help="Order by methods")
def list_urls(order_by):
    from flask import current_app

    rules = sorted(
        key=lambda rule: getattr(rule, order_by)

    max_rule_len = max(len(rule.rule) for rule in rules)
    max_rule_len = max(max_rule_len, len("Route"))

    max_endpoint_len = max(len(rule.endpoint) for rule in rules)
    max_endpoint_len = max(max_endpoint_len, len("Endpoint"))

    max_method_len = max(len(", ".join(rule.methods)) for rule in rules)
    max_method_len = max(max_method_len, len("Methods"))

    column_header_len = max_rule_len + max_endpoint_len + max_method_len + 4
    column_template = "{:<%s}  {:<%s}  {:<%s}" % (
        max_rule_len, max_endpoint_len, max_method_len

    click.secho(column_template.format("Route", "Endpoint", "Methods"),
                fg="blue", bold=True)

## ... source file continues with no further with_appcontext examples...

Sponsored By

Sentry logo

Software errors are inevitable. Chaos is not. Try Sentry for free.

AssemblyAI logo

The most accurate speech-to-text API. Built for Python developers.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via Twitter & Facebook.

Matt Makai 2012-2020