flask.cli DispatchingApp Example Code

DispatchingApp is a class within the flask.cli module of the Flask project. DispatchingApp is a special application that dispatches to a Flask app if it is imported by name in a background thread.

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

Example 1 from indico

indico (project website, documentation and sandbox demo) is a Flask-based web app for event management. The code is open sourced under the MIT license.

indico / indico / cli / devserver.py

# devserver.py

from __future__ import print_function, unicode_literals

import os

from flask.cli import DispatchingApp
from werkzeug.debug import DebuggedApplication
from werkzeug.exceptions import NotFound
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.middleware.proxy_fix import ProxyFix
from werkzeug.serving import WSGIRequestHandler, run_simple
from werkzeug.urls import url_parse

    import pywatchman
except ImportError:
    pywatchman = None

def run_cmd(info, **kwargs):
    if kwargs['reloader_type'] == 'watchman':
        if pywatchman is None:
            print('watchman is not available - you need to `pip install pywatchman`')
    run_server(info, **kwargs)

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

    import logging
    werkzeug_logger = logging.getLogger('werkzeug')
    werkzeug_logger.propagate = False

    app = _make_wsgi_app(info, url, evalex_whitelist, proxy)
    run_simple(host, port, app,
               reloader_type=reloader_type, use_reloader=(reloader_type != 'none'),
               use_debugger=False, use_evalex=False, threaded=True, ssl_context=ssl_ctx,
               extra_files=extra_files, request_handler=QuietWSGIRequestHandler if quiet else None)

def _reset_state():
    from indico.core.celery import celery
    celery.flask_app = None

def _make_wsgi_app(info, url, evalex_whitelist, proxy):
    def _load_app():
        return info.load_app()

    url_data = url_parse(url)
    app = DispatchingApp(_load_app)
    app = DebuggedIndico(app, evalex_whitelist)
    app = _make_indico_dispatcher(app, url_data.path)
    if proxy:
        app = ProxyFix(app, x_for=1, x_proto=1, x_host=1)
    QuietWSGIRequestHandler.INDICO_URL_PREFIX = url_data.path.rstrip('/')
    return app

def _make_indico_dispatcher(wsgi_app, path):
    path = path.rstrip('/')
    if not path:
        return wsgi_app
        return DispatcherMiddleware(NotFound(), {
            path: wsgi_app

class DebuggedIndico(DebuggedApplication):
    def __init__(self, *args, **kwargs):
        self._evalex_whitelist = None
        self._request_ip = None
        super(DebuggedIndico, self).__init__(*args, **kwargs)

## ... source file continues with no further DispatchingApp 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