Skip to content

API โ€‹

This part of the documentation covers all the interfaces of Flask. For parts where Flask depends on external libraries, we document the most important right here and provide links to the canonical documentation.

Application Object โ€‹

  • class flask.Flask(import_name, static_url_path=None, static_folder='static', static_host=None, host_matching=False, subdomain_matching=False, template_folder='templates', instance_path=None, instance_relative_config=False, root_path=None) โ€‹

    The flask object implements a WSGI application and acts as the central object. It is passed the name of the module or package of the application. Once it is created it will act as a central registry for the view functions, the URL rules, template configuration and much more.

    The name of the package is used to resolve resources from inside the package or the folder the module is contained in depending on if the package parameter resolves to an actual python package (a folder with an __init__.py file inside) or a standard module (just a .py file).

    For more information about resource loading, see open_resource().

    Usually you create a Flask instance in your main module or in the __init__.py file of your package like this:

    python
    from flask import Flask
    app = Flask(__name__)
    from flask import Flask
    app = Flask(__name__)

    About the First Parameter

    The idea of the first parameter is to give Flask an idea of what belongs to your application. This name is used to find resources on the filesystem, can be used by extensions to improve debugging information and a lot more.

    So itโ€™s important what you provide there. If you are using a single module, __name__ is always the correct value. If you however are using a package, itโ€™s usually recommended to hardcode the name of your package there.

    For example if your application is defined in yourapplication/app.py you should create it with one of the two versions below:

    python
    app = Flask('yourapplication')
    app = Flask(__name__.split('.')[0])
    app = Flask('yourapplication')
    app = Flask(__name__.split('.')[0])

    Why is that? The application will work even with __name__, thanks to how resources are looked up. However it will make debugging more painful. Certain extensions can make assumptions based on the import name of your application. For example the Flask-SQLAlchemy extension will look for the code in your application that triggered an SQL query in debug mode. If the import name is not properly set up, that debugging information is lost. (For example it would only pick up SQL queries in yourapplication.app and not yourapplication.views.frontend)

    Changelog

    New in version 1.0: The host_matching and static_host parameters were added.

    New in version 1.0: The subdomain_matching parameter was added. Subdomain matching needs to be enabled manually now. Setting SERVER_NAME does not implicitly enable it.

    New in version 0.11: The root_path parameter was added.

    New in version 0.8: The instance_path and instance_relative_config parameters were added.

    New in version 0.7: The static_url_path, static_folder, and template_folder parameters were added.

    • Parameters:

      • import_name (str) โ€“ the name of the application package

      • static_url_path (str | None) โ€“ can be used to specify a different path for the static files on the web. Defaults to the name of the static_folder folder.

      • static_folder (str | os.PathLike | None) โ€“ The folder with static files that is served at static_url_path. Relative to the application root_path or an absolute path. Defaults to 'static'.

      • static_host (str | None) โ€“ the host to use when adding the static route. Defaults to None. Required when using host_matching=True with a static_folder configured.

      • host_matching (bool) โ€“ set url_map.host_matching attribute. Defaults to False.

      • subdomain_matching (bool) โ€“ consider the subdomain relative to SERVER_NAME when matching routes. Defaults to False.

      • template_folder (str | os.PathLike | None) โ€“ the folder that contains the templates that should be used by the application. Defaults to 'templates' folder in the root path of the application.

      • instance_path (str | None) โ€“ An alternative instance path for the application. By default the folder 'instance' next to the package or module is assumed to be the instance path.

      • instance_relative_config (bool) โ€“ if set to True relative filenames for loading the config are assumed to be relative to the instance path instead of the application root.

      • root_path (str | None) โ€“ The path to the root of the application files. This should only be set manually when it canโ€™t be detected automatically, such as for namespace packages.

    • aborter โ€‹

      An instance of aborter_class created by make_aborter(). This is called by flask.abort() to raise HTTP errors, and can be called directly as well.

      Changelog

      New in version 2.2: Moved from flask.abort, which calls this object.

    • aborter_class โ€‹

      alias of Aborter

    • add_template_filter(f, name=None) โ€‹

      Register a custom template filter. Works exactly like the template_filter() decorator.

      Parameters:

      • name (str | None) โ€“ the optional name of the filter, otherwise the function name will be used.

      • f (Callable[[...], Any]) โ€“

      Return type: None

    • add_template_global(f, name=None) โ€‹

      Register a custom template global function. Works exactly like the template_global() decorator.

      Changelog

      New in version 0.10.

      Parameters:

      • name (str | None) โ€“ the optional name of the global function, otherwise the function name will be used.

      • f (Callable[[...], Any]) โ€“

      Return type: None

    • add_template_test(f, name=None) โ€‹

      Register a custom template test. Works exactly like the template_test() decorator.

      Changelog

      New in version 0.10.

      Parameters:

      • name (str | None) โ€“ the optional name of the test, otherwise the function name will be used.

      • f (Callable[[...], bool]) โ€“

      Return type: None

    • add_url_rule(rule, endpoint=None, view_func=None, provide_automatic_options=None, **options) โ€‹

      Register a rule for routing incoming requests and building URLs. The route() decorator is a shortcut to call this with the view_func argument. These are equivalent:

      python
      @app.route("/")
      def index():
          ...
      @app.route("/")
      def index():
          ...
      python
      def index():
          ...
      
      app.add_url_rule("/", view_func=index)
      def index():
          ...
      
      app.add_url_rule("/", view_func=index)

      See URL Route Registrations.

      The endpoint name for the route defaults to the name of the view function if the endpoint parameter isnโ€™t passed. An error will be raised if a function has already been registered for the endpoint.

      The methods parameter defaults to ["GET"]. HEAD is always added automatically, and OPTIONS is added automatically by default.

      view_func does not necessarily need to be passed, but if the rule should participate in routing an endpoint name must be associated with a view function at some point with the endpoint() decorator.

      python
      app.add_url_rule("/", endpoint="index")
      
      @app.endpoint("index")
      def index():
          ...
      app.add_url_rule("/", endpoint="index")
      
      @app.endpoint("index")
      def index():
          ...

      If view_func has a required_methods attribute, those methods are added to the passed and automatic methods. If it has a provide_automatic_methods attribute, it is used as the default if the parameter is not passed.

      Parameters:

      • rule (str) โ€“ The URL rule string.

      • endpoint (str | None) โ€“ The endpoint name to associate with the rule and view function. Used when routing and building URLs. Defaults to view_func.__name__.

      • view_func (ft.RouteCallable | None) โ€“ The view function to associate with the endpoint name.

      • provide_automatic_options (bool | None) โ€“ Add the OPTIONS method and respond to OPTIONS requests automatically.

      • options (t.Any) โ€“ Extra options passed to the Rule object.

      Return type: None

    • after_request(f) โ€‹

      Register a function to run after each request to this object.

      The function is called with the response object, and must return a response object. This allows the functions to modify or replace the response before it is sent.

      If a function raises an exception, any remaining after_request functions will not be called. Therefore, this should not be used for actions that must execute, such as to close resources. Use teardown_request() for that.

      This is available on both app and blueprint objects. When used on an app, this executes after every request. When used on a blueprint, this executes after every request that the blueprint handles. To register with a blueprint and execute after every request, use Blueprint.after_app_request().

      Parameters:

      • f (T_after_request) โ€“

      Return type: T_after_request

    • after_request_funcs: dict[ft.AppOrBlueprintKey, list[ft.AfterRequestCallable]] โ€‹

      A data structure of functions to call at the end of each request, in the format {scope: [functions]}. The scope key is the name of a blueprint the functions are active for, or None for all requests.

      To register a function, use the after_request() decorator.

      This data structure is internal. It should not be modified directly and its format may change at any time.

    • app_context() โ€‹

      Create an AppContext. Use as a with block to push the context, which will make current_app point at this application.

      An application context is automatically pushed by RequestContext.push() when handling a request, and when running a CLI command. Use this to manually create a context outside of these situations.

      python
      with app.app_context():
          init_db()
      with app.app_context():
          init_db()

      See The Application Context.

      Changelog

      New in version 0.9.

      Return type: AppContext

    • app_ctx_globals_class โ€‹

      alias of _AppCtxGlobals

    • async_to_sync(func) โ€‹

      Return a sync function that will run the coroutine function.

      python
      result = app.async_to_sync(func)(*args, **kwargs)
      result = app.async_to_sync(func)(*args, **kwargs)

      Override this method to change how the app converts async code to be synchronously callable.

      Changelog

      New in version 2.0.

      Parameters:

      • func (Callable[[...], Coroutine]) โ€“

      Return type: Callable[[โ€ฆ], Any]

    • auto_find_instance_path() โ€‹

      Tries to locate the instance path if it was not provided to the constructor of the application class. It will basically calculate the path to a folder named instance next to your main file or the package.

      Changelog

      New in version 0.8.

      Return type: str

    • before_request(f) โ€‹

      Register a function to run before each request.

      For example, this can be used to open a database connection, or to load the logged in user from the session.

      python
      @app.before_request
      def load_user():
          if "user_id" in session:
              g.user = db.session.get(session["user_id"])
      @app.before_request
      def load_user():
          if "user_id" in session:
              g.user = db.session.get(session["user_id"])

      The function will be called without any arguments. If it returns a non-None value, the value is handled as if it was the return value from the view, and further request handling is stopped.

      This is available on both app and blueprint objects. When used on an app, this executes before every request. When used on a blueprint, this executes before every request that the blueprint handles. To register with a blueprint and execute before every request, use Blueprint.before_app_request().

      Parameters:

      • f (T_before_request) โ€“

      Return type: T_before_request

    • before_request_funcs: dict[ft.AppOrBlueprintKey, list[ft.BeforeRequestCallable]] โ€‹

      A data structure of functions to call at the beginning of each request, in the format {scope: [functions]}. The scope key is the name of a blueprint the functions are active for, or None for all requests.

      To register a function, use the before_request() decorator.

      This data structure is internal. It should not be modified directly and its format may change at any time.

    • blueprints: dict[str, Blueprint] โ€‹

      Maps registered blueprint names to blueprint objects. The dict retains the order the blueprints were registered in. Blueprints can be registered multiple times, this dict does not track how often they were attached.

      Changelog

      New in version 0.7.

    • cli โ€‹

      The Click command group for registering CLI commands for this object. The commands are available from the flask command once the application has been discovered and blueprints have been registered.

    • config โ€‹

      The configuration dictionary as Config. This behaves exactly like a regular dictionary but supports additional methods to load a config from files.

    • config_class โ€‹

      alias of Config

    • context_processor(f) โ€‹

      Registers a template context processor function. These functions run before rendering a template. The keys of the returned dict are added as variables available in the template.

      This is available on both app and blueprint objects. When used on an app, this is called for every rendered template. When used on a blueprint, this is called for templates rendered from the blueprintโ€™s views. To register with a blueprint and affect every template, use Blueprint.app_context_processor().

      Parameters:

      • f (T_template_context_processor) โ€“

      Return type: T_template_context_processor

    • create_global_jinja_loader() โ€‹

      Creates the loader for the Jinja2 environment. Can be used to override just the loader and keeping the rest unchanged. Itโ€™s discouraged to override this function. Instead one should override the jinja_loader() function instead.

      The global loader dispatches between the loaders of the application and the individual blueprints.

      Changelog

      New in version 0.7.

      Return type: DispatchingJinjaLoader

    • create_jinja_environment() โ€‹

      Create the Jinja environment based on jinja_options and the various Jinja-related methods of the app. Changing jinja_options after this will have no effect. Also adds Flask-related globals and filters to the environment.

      Changelog

      Changed in version 0.11: Environment.auto_reload set in accordance with TEMPLATES_AUTO_RELOAD configuration option.

      New in version 0.5.

      Return type: Environment

    • create_url_adapter(request) โ€‹

      Creates a URL adapter for the given request. The URL adapter is created at a point where the request context is not yet set up so the request is passed explicitly.

      Changelog

      Changed in version 1.0: SERVER_NAME no longer implicitly enables subdomain matching. Use subdomain_matching instead.

      Changed in version 0.9: This can now also be called without a request object when the URL adapter is created for the application context.

      New in version 0.6.

      Parameters:

      • request (Request | None) โ€“

      Return type: MapAdapter | None

    • property debug: bool โ€‹

      Whether debug mode is enabled. When using flask run to start the development server, an interactive debugger will be shown for unhandled exceptions, and the server will be reloaded when code changes. This maps to the DEBUG config key. It may not behave as expected if set late.

      Do not enable debug mode when deploying in production.

      Default: False

    • Default configuration parameters.

    • delete(rule, **options) โ€‹

      Shortcut for route() with methods=["DELETE"].

      Changelog

      New in version 2.0.

      Parameters:

      • rule (str) โ€“

      • options (Any) โ€“

      Return type: Callable[[T_route], T_route]

    • dispatch_request() โ€‹

      Does the request dispatching. Matches the URL and returns the return value of the view or error handler. This does not have to be a response object. In order to convert the return value to a proper response object, call make_response().

      Changelog

      Changed in version 0.7: This no longer does the exception handling, this code was moved to the new full_dispatch_request().

      Return type: ft.ResponseReturnValue

    • do_teardown_appcontext(exc=<object object>) โ€‹

      Called right before the application context is popped.

      When handling a request, the application context is popped after the request context. See do_teardown_request().

      This calls all functions decorated with teardown_appcontext(). Then the appcontext_tearing_down signal is sent.

      This is called by AppContext.pop().

      Changelog

      New in version 0.9.

      Parameters:

      • exc (BaseException | None) โ€“

      Return type: None

    • do_teardown_request(exc=<object object>) โ€‹

      Called after the request is dispatched and the response is returned, right before the request context is popped.

      This calls all functions decorated with teardown_request(), and Blueprint.teardown_request() if a blueprint handled the request. Finally, the request_tearing_down signal is sent.

      This is called by RequestContext.pop(), which may be delayed during testing to maintain access to resources.

      Parameters:

      • exc (BaseException | None) โ€“ An unhandled exception raised while dispatching the request. Detected from the current exception information if not passed. Passed to each teardown function.

      Return type: None

      Changelog

      Changed in version 0.9: Added the exc argument.

    • endpoint(endpoint) โ€‹

      Decorate a view function to register it for the given endpoint. Used if a rule is added without a view_func with add_url_rule().

      python
      app.add_url_rule("/ex", endpoint="example")
      
      @app.endpoint("example")
      def example():
          ...
      app.add_url_rule("/ex", endpoint="example")
      
      @app.endpoint("example")
      def example():
          ...

      Parameters:

      • endpoint (str) โ€“ The endpoint name to associate with the view function.

      Return type: Callable[[F], F]

    • ensure_sync(func) โ€‹

      Ensure that the function is synchronous for WSGI workers. Plain def functions are returned as-is. async def functions are wrapped to run and wait for the response.

      Override this method to change how the app runs async views.

      Changelog

      New in version 2.0.

      Parameters:

      • func (Callable) โ€“

      Return type: Callable

    • error_handler_spec: dict[ft.AppOrBlueprintKey, dict[int | None, dict[type[Exception], ft.ErrorHandlerCallable]]] โ€‹

      A data structure of registered error handlers, in the format {scope: {code: {class: handler}}}. The scope key is the name of a blueprint the handlers are active for, or None for all requests. The code key is the HTTP status code for HTTPException, or None for other exceptions. The innermost dictionary maps exception classes to handler functions.

      To register an error handler, use the errorhandler() decorator.

      This data structure is internal. It should not be modified directly and its format may change at any time.

    • errorhandler(code_or_exception) โ€‹

      Register a function to handle errors by code or exception class.

      A decorator that is used to register a function given an error code. Example:

      python
      @app.errorhandler(404)
      def page_not_found(error):
          return 'This page does not exist', 404
      @app.errorhandler(404)
      def page_not_found(error):
          return 'This page does not exist', 404

      You can also register handlers for arbitrary exceptions:

      python
      @app.errorhandler(DatabaseError)
      def special_exception_handler(error):
          return 'Database connection failed', 500
      @app.errorhandler(DatabaseError)
      def special_exception_handler(error):
          return 'Database connection failed', 500

      This is available on both app and blueprint objects. When used on an app, this can handle errors from every request. When used on a blueprint, this can handle errors from requests that the blueprint handles. To register with a blueprint and affect every request, use Blueprint.app_errorhandler().

      Changelog

      New in version 0.7: Use register_error_handler() instead of modifying error_handler_spec directly, for application wide error handlers.

      New in version 0.7: One can now additionally also register custom exception types that do not necessarily have to be a subclass of the HTTPException class.

      Parameters:

      • code_or_exception (type[Exception] | int) โ€“ the code as integer for the handler, or an arbitrary exception

      Return type: Callable[[T_error_handler], T_error_handler]

    • extensions: dict โ€‹

      a place where extensions can store application specific state. For example this is where an extension could store database engines and similar things.

      The key must match the name of the extension module. For example in case of a โ€œFlask-Fooโ€ extension in flask_foo, the key would be 'foo'.

      Changelog

      New in version 0.7.

    • full_dispatch_request() โ€‹

      Dispatches the request and on top of that performs request pre and postprocessing as well as HTTP exception catching and error handling.

      Changelog

      New in version 0.7.

      Return type: Response

    • get(rule, **options) โ€‹

      Shortcut for route() with methods=["GET"].

      Changelog

      New in version 2.0.

      Parameters:

      • rule (str) โ€“

      • options (Any) โ€“

      Return type: Callable[[T_route], T_route]

    • get_send_file_max_age(filename) โ€‹

      Used by send_file() to determine the max_age cache value for a given file path if it wasnโ€™t passed.

      By default, this returns SEND_FILE_MAX_AGE_DEFAULT from the configuration of current_app. This defaults to None, which tells the browser to use conditional requests instead of a timed cache, which is usually preferable.

      Changelog

      Changed in version 2.0: The default configuration is None instead of 12 hours.

      New in version 0.9.

      Parameters:

      • filename (str | None) โ€“

      Return type: int | None

    • property got_first_request: bool โ€‹

      This attribute is set to True if the application started handling the first request.

      Deprecated since version 2.3: Will be removed in Flask 2.4.

      Changelog

      New in version 0.8.

    • handle_exception(e) โ€‹

      Handle an exception that did not have an error handler associated with it, or that was raised from an error handler. This always causes a 500 InternalServerError.

      Always sends the got_request_exception signal.

      If PROPAGATE_EXCEPTIONS is True, such as in debug mode, the error will be re-raised so that the debugger can display it. Otherwise, the original exception is logged, and an InternalServerError is returned.

      If an error handler is registered for InternalServerError or 500, it will be used. For consistency, the handler will always receive the InternalServerError. The original unhandled exception is available as e.original_exception.

      Changelog

      Changed in version 1.1.0: Always passes the InternalServerError instance to the handler, setting original_exception to the unhandled error.

      Changed in version 1.1.0: after_request functions and other finalization is done even for the default 500 response when there is no handler.

      New in version 0.3.

      Parameters:

      • e (Exception) โ€“

      Return type: Response

    • handle_http_exception(e) โ€‹

      Handles an HTTP exception. By default this will invoke the registered error handlers and fall back to returning the exception as response.

      Changelog

      Changed in version 1.0.3: RoutingException, used internally for actions such as slash redirects during routing, is not passed to error handlers.

      Changed in version 1.0: Exceptions are looked up by code and by MRO, so HTTPException subclasses can be handled with a catch-all handler for the base HTTPException.

      New in version 0.3.

      Parameters:

      • e (HTTPException) โ€“

      Return type: HTTPException | ft.ResponseReturnValue

    • handle_url_build_error(error, endpoint, values) โ€‹

      Called by url_for() if a BuildError was raised. If this returns a value, it will be returned by url_for, otherwise the error will be re-raised.

      Each function in url_build_error_handlers is called with error, endpoint and values. If a function returns None or raises a BuildError, it is skipped. Otherwise, its return value is returned by url_for.

      Parameters:

      • error (BuildError) โ€“ The active BuildError being handled.

      • endpoint (str) โ€“ The endpoint being built.

      • values (dict[str, Any]) โ€“ The keyword arguments passed to url_for.

      Return type: str

    • handle_user_exception(e) โ€‹

      This method is called whenever an exception occurs that should be handled. A special case is HTTPException which is forwarded to the handle_http_exception() method. This function will either return a response value or reraise the exception with the same traceback.

      Changelog

      Changed in version 1.0: Key errors raised from request data like form show the bad key in debug mode rather than a generic bad request message.

      New in version 0.7.

      Parameters:

      • e (Exception) โ€“

      Return type: HTTPException | ft.ResponseReturnValue

    • property has_static_folder: bool โ€‹

      True if static_folder is set.

      Changelog

      New in version 0.5.

    • import_name โ€‹

      The name of the package or module that this object belongs to. Do not change this once it is set by the constructor.

    • inject_url_defaults(endpoint, values) โ€‹

      Injects the URL defaults for the given endpoint directly into the values dictionary passed. This is used internally and automatically called on URL building.

      Changelog

      New in version 0.7.

      Parameters:

      • endpoint (str) โ€“

      • values (dict) โ€“

      Return type: None

    • instance_path โ€‹

      Holds the path to the instance folder.

      Changelog

      New in version 0.8.

    • iter_blueprints() โ€‹

      Iterates over all blueprints by the order they were registered.

      Changelog

      New in version 0.11.

      Return type: t.ValuesView[Blueprint]

    • property jinja_env: Environment โ€‹

      The Jinja environment used to load templates.

      The environment is created the first time this property is accessed. Changing jinja_options after that will have no effect.

    • jinja_environment โ€‹

      alias of Environment

    • property jinja_loader: FileSystemLoader | None โ€‹

      The Jinja loader for this objectโ€™s templates. By default this is a class jinja2.loaders.FileSystemLoader to template_folder if it is set.

      Changelog

      New in version 0.5.

    • jinja_options: dict = {} โ€‹

      Options that are passed to the Jinja environment in create_jinja_environment(). Changing these options after the environment is created (accessing jinja_env) will have no effect.

      Changelog

      Changed in version 1.1.0: This is a dict instead of an ImmutableDict to allow easier configuration.

    • json: JSONProvider โ€‹

      Provides access to JSON methods. Functions in flask.json will call methods on this provider when the application context is active. Used for handling JSON requests and responses.

      An instance of json_provider_class. Can be customized by changing that attribute on a subclass, or by assigning to this attribute afterwards.

      The default, DefaultJSONProvider, uses Pythonโ€™s built-in json library. A different provider can use a different JSON library.

      Changelog

      New in version 2.2.

    • json_provider_class โ€‹

      alias of DefaultJSONProvider

    • log_exception(exc_info) โ€‹

      Logs an exception. This is called by handle_exception() if debugging is disabled and right before the handler is called. The default implementation logs the exception as error on the logger.

      Changelog

      New in version 0.8.

      Parameters:

      • exc_info (tuple[type, BaseException, traceback] | tuple[None, None, None]) โ€“

      Return type: None

    • property logger: Logger โ€‹

      A standard Python Logger for the app, with the same name as name.

      In debug mode, the loggerโ€™s level will be set to DEBUG.

      If there are no handlers configured, a default handler will be added. See Logging for more information.

      Changelog

      Changed in version 1.1.0: The logger takes the same name as name rather than hard-coding "flask.app".

      Changed in version 1.0.0: Behavior was simplified. The logger is always named "flask.app". The level is only set during configuration, it doesnโ€™t check app.debug each time. Only one format is used, not different ones depending on app.debug. No handlers are removed, and a handler is only added if no handlers are already configured.

      New in version 0.3.

    • make_aborter() โ€‹

      Create the object to assign to aborter. That object is called by flask.abort() to raise HTTP errors, and can be called directly as well.

      By default, this creates an instance of aborter_class, which defaults to werkzeug.exceptions.Aborter.

      Changelog

      New in version 2.2.

      Return type: Aborter

    • make_config(instance_relative=False) โ€‹

      Used to create the config attribute by the Flask constructor. The instance_relative parameter is passed in from the constructor of Flask (there named instance_relative_config) and indicates if the config should be relative to the instance path or the root path of the application.

      Changelog

      New in version 0.8.

      Parameters:

      • instance_relative (bool) โ€“

      Return type: Config

    • make_default_options_response() โ€‹

      This method is called to create the default OPTIONS response. This can be changed through subclassing to change the default behavior of OPTIONS responses.

      Changelog

      New in version 0.7.

      Return type: Response

    • make_response(rv) โ€‹

      Convert the return value from a view function to an instance of response_class.

      Parameters:

      • rv (ft.ResponseReturnValue) โ€“ the return value from the view function. The view function must return a response. Returning None, or the view ending without returning, is not allowed. The following types are allowed for view_rv:

        • str: A response object is created with the string encoded to UTF-8 as the body.

        • bytes: A response object is created with the bytes as the body.

        • dict: A dictionary that will be jsonifyโ€™d before being returned.

        • list: A list that will be jsonifyโ€™d before being returned.

        • generator or iterator: A generator that returns str or bytes to be streamed as the response.

        • tuple: Either (body, status, headers), (body, status), or (body, headers), where body is any of the other types allowed here, status is a string or an integer, and headers is a dictionary or a list of (key, value) tuples. If body is a response_class instance, status overwrites the exiting value and headers are extended.

        • response_class: The object is returned unchanged.

        • other Response class: The object is coerced to response_class.

        • callable(): The function is called as a WSGI application. The result is used to create a response object.

      Return type: Response

      Changelog

      Changed in version 2.2: A generator will be converted to a streaming response. A list will be converted to a JSON response.

      Changed in version 1.1: A dict will be converted to a JSON response.

      Changed in version 0.9: Previously a tuple was interpreted as the arguments for the response object.

    • make_shell_context() โ€‹

      Returns the shell context for an interactive shell for this application. This runs all the registered shell context processors.

      Changelog

      New in version 0.11.

      Return type: dict

    • property name: str โ€‹

      The name of the application. This is usually the import name with the difference that itโ€™s guessed from the run file if the import name is main. This name is used as a display name when Flask needs the name of the application. It can be set and overridden to change the value.

      Changelog

      New in version 0.8.

    • open_instance_resource(resource, mode='rb') โ€‹

      Opens a resource from the applicationโ€™s instance folder (instance_path). Otherwise works like open_resource(). Instance resources can also be opened for writing.

      Parameters:

      • resource (str) โ€“ the name of the resource. To access resources within subfolders use forward slashes as separator.

      • mode (str) โ€“ resource file opening mode, default is โ€˜rbโ€™.

      Return type: IO

    • open_resource(resource, mode='rb') โ€‹

      Open a resource file relative to root_path for reading.

      For example, if the file schema.sql is next to the file app.py where the Flask app is defined, it can be opened with:

      python
      with app.open_resource("schema.sql") as f:
          conn.executescript(f.read())
      with app.open_resource("schema.sql") as f:
          conn.executescript(f.read())

      Parameters:

      • resource (str) โ€“ Path to the resource relative to root_path.

      • mode (str) โ€“ Open the file in this mode. Only reading is supported, valid values are โ€œrโ€ (or โ€œrtโ€) and โ€œrbโ€.

      Return type: IO

    • patch(rule, **options) โ€‹

      Shortcut for route() with methods=["PATCH"].

      Changelog

      New in version 2.0.

      Parameters:

      • rule (str) โ€“

      • options (Any) โ€“

      Return type: Callable[[T_route], T_route]

    • permanent_session_lifetime โ€‹

      A timedelta which is used to set the expiration date of a permanent session. The default is 31 days which makes a permanent session survive for roughly one month.

      This attribute can also be configured from the config with the PERMANENT_SESSION_LIFETIME configuration key. Defaults to timedelta(days=31)

    • post(rule, **options) โ€‹

      Shortcut for route() with methods=["POST"].

      Changelog

      New in version 2.0.

      Parameters:

      • rule (str) โ€“

      • options (Any) โ€“

      Return type: Callable[[T_route], T_route]

    • preprocess_request() โ€‹

      Called before the request is dispatched. Calls url_value_preprocessors registered with the app and the current blueprint (if any). Then calls before_request_funcs registered with the app and the blueprint.

      If any before_request() handler returns a non-None value, the value is handled as if it was the return value from the view, and further request handling is stopped.

      Return type: ft.ResponseReturnValue | None

    • process_response(response) โ€‹

      Can be overridden in order to modify the response object before itโ€™s sent to the WSGI server. By default this will call all the after_request() decorated functions.

      Changelog

      Changed in version 0.5: As of Flask 0.5 the functions registered for after request execution are called in reverse order of registration.

      Parameters:

      • response (Response) โ€“ a response_class object.

      Returns: a new response object or the same, has to be an instance of response_class.

      Return type: Response

    • put(rule, **options) โ€‹

      Shortcut for route() with methods=["PUT"].

      Changelog

      New in version 2.0.

      Parameters:

      • rule (str) โ€“

      • options (Any) โ€“

      Return type: Callable[[T_route], T_route]

    • redirect(location, code=302) โ€‹

      Create a redirect response object.

      This is called by flask.redirect(), and can be called directly as well.

      Parameters:

      • location (str) โ€“ The URL to redirect to.

      • code (int) โ€“ The status code for the redirect.

      Return type: Response

      Changelog

      New in version 2.2: Moved from flask.redirect, which calls this method.

    • register_blueprint(blueprint, **options) โ€‹

      Register a Blueprint on the application. Keyword arguments passed to this method will override the defaults set on the blueprint.

      Calls the blueprintโ€™s register() method after recording the blueprint in the applicationโ€™s blueprints.

      Parameters:

      • blueprint (Blueprint) โ€“ The blueprint to register.

      • url_prefix โ€“ Blueprint routes will be prefixed with this.

      • subdomain โ€“ Blueprint routes will match on this subdomain.

      • url_defaults โ€“ Blueprint routes will use these default values for view arguments.

      • options (t.Any) โ€“ Additional keyword arguments are passed to BlueprintSetupState. They can be accessed in record() callbacks.

      Return type: None

      Changelog

      Changed in version 2.0.1: The name option can be used to change the (pre-dotted) name the blueprint is registered with. This allows the same blueprint to be registered multiple times with unique names for url_for.

      New in version 0.7.

    • register_error_handler(code_or_exception, f) โ€‹

      Alternative error attach function to the errorhandler() decorator that is more straightforward to use for non decorator usage.

      Changelog

      New in version 0.7.

      Parameters:

      • code_or_exception (type[Exception] | int) โ€“

      • f (ft.ErrorHandlerCallable) โ€“

      Return type: None

    • request_class โ€‹

      alias of Request

    • request_context(environ) โ€‹

      Create a RequestContext representing a WSGI environment. Use a with block to push the context, which will make request point at this request.

      See The Request Context.

      Typically you should not call this from your own code. A request context is automatically pushed by the wsgi_app() when handling a request. Use test_request_context() to create an environment and context instead of this method.

      Parameters:

      • environ (dict) โ€“ a WSGI environment

      Return type: RequestContext

    • response_class โ€‹

      alias of Response

    • root_path โ€‹

      Absolute path to the package on the filesystem. Used to look up resources contained in the package.

    • route(rule, **options) โ€‹

      Decorate a view function to register it with the given URL rule and options. Calls add_url_rule(), which has more details about the implementation.

      python
      @app.route("/")
      def index():
          return "Hello, World!"
      @app.route("/")
      def index():
          return "Hello, World!"

      See URL Route Registrations.

      The endpoint name for the route defaults to the name of the view function if the endpoint parameter isnโ€™t passed.

      The methods parameter defaults to ["GET"]. HEAD and OPTIONS are added automatically.

      Parameters:

      • rule (str) โ€“ The URL rule string.

      • options (Any) โ€“ Extra options passed to the Rule object.

      Return type: Callable[[T_route], T_route]

    • run(host=None, port=None, debug=None, load_dotenv=True, **options) โ€‹

      Runs the application on a local development server.

      Do not use run() in a production setting. It is not intended to meet security and performance requirements for a production server. Instead, see Deploying to Production for WSGI server recommendations.

      If the debug flag is set the server will automatically reload for code changes and show a debugger in case an exception happened.

      If you want to run the application in debug mode, but disable the code execution on the interactive debugger, you can pass use_evalex=False as parameter. This will keep the debuggerโ€™s traceback screen active, but disable code execution.

      It is not recommended to use this function for development with automatic reloading as this is badly supported. Instead you should be using the flask command line scriptโ€™s run support.

      Keep in Mind

      Flask will suppress any server error with a generic error page unless it is in debug mode. As such to enable just the interactive debugger without the code reloading, you have to invoke run() with debug=True and use_reloader=False. Setting use_debugger to True without being in debug mode wonโ€™t catch any exceptions because there wonโ€™t be any to catch.

      Parameters:

      • host (str | None) โ€“ the hostname to listen on. Set this to '0.0.0.0' to have the server available externally as well. Defaults to '127.0.0.1' or the host in the SERVER_NAME config variable if present.

      • port (int | None) โ€“ the port of the webserver. Defaults to 5000 or the port defined in the SERVER_NAME config variable if present.

      • debug (bool | None) โ€“ if given, enable or disable debug mode. See debug.

      • load_dotenv (bool) โ€“ Load the nearest .env and .flaskenv files to set environment variables. Will also change the working directory to the directory containing the first file found.

      • options (Any) โ€“ the options to be forwarded to the underlying Werkzeug server. See werkzeug.serving.run_simple() for more information.

      Return type: None

      Changelog

      Changed in version 1.0: If installed, python-dotenv will be used to load environment variables from .env and .flaskenv files.

      The FLASK_DEBUG environment variable will override debug.

      Threaded mode is enabled by default.

      Changed in version 0.10: The default port is now picked from the SERVER_NAME variable.

    • secret_key โ€‹

      If a secret key is set, cryptographic components can use this to sign cookies and other things. Set this to a complex random value when you want to use the secure cookie for instance.

      This attribute can also be configured from the config with the SECRET_KEY configuration key. Defaults to None.

    • select_jinja_autoescape(filename) โ€‹

      Returns True if autoescaping should be active for the given template name. If no template name is given, returns True.

      Changelog

      Changed in version 2.2: Autoescaping is now enabled by default for .svg files.

      New in version 0.5.

      Parameters:

      • filename (str) โ€“

      Return type: bool

    • send_static_file(filename)

      The view function used to serve files from static_folder. A route is automatically registered for this view at static_url_path if static_folder is set.

      Changelog

      New in version 0.5.

      Parameters:

      • filename (str) โ€“

      Return type: Response

    • session_interface: SessionInterface = <flask.sessions.SecureCookieSessionInterface object> โ€‹

      the session interface to use. By default an instance of SecureCookieSessionInterface is used here.

      Changelog

      New in version 0.8.

    • shell_context_processor(f) โ€‹

      Registers a shell context processor function.

      Changelog

      New in version 0.11.

      Parameters:

      • f (T_shell_context_processor) โ€“

      Return type: T_shell_context_processor

    • shell_context_processors: list[ft.ShellContextProcessorCallable] โ€‹

      A list of shell context processor functions that should be run when a shell context is created.

      Changelog

      New in version 0.11.

    • should_ignore_error(error) โ€‹

      This is called to figure out if an error should be ignored or not as far as the teardown system is concerned. If this function returns True then the teardown handlers will not be passed the error.

      Changelog

      New in version 0.10.

      Parameters:

      • error (BaseException | None) โ€“

      Return type: bool

    • property static_folder: str | None โ€‹

      The absolute path to the configured static folder. None if no static folder is set.

    • property static_url_path: str | None โ€‹

      The URL prefix that the static route will be accessible from.

      If it was not configured during init, it is derived from static_folder.

    • teardown_appcontext(f) โ€‹

      Registers a function to be called when the application context is popped. The application context is typically popped after the request context for each request, at the end of CLI commands, or after a manually pushed context ends.

      python
      with app.app_context():
          ...
      with app.app_context():
          ...

      When the with block exits (or ctx.pop() is called), the teardown functions are called just before the app context is made inactive. Since a request context typically also manages an application context it would also be called when you pop a request context.

      When a teardown function was called because of an unhandled exception it will be passed an error object. If an errorhandler() is registered, it will handle the exception and the teardown will not receive it.

      Teardown functions must avoid raising exceptions. If they execute code that might fail they must surround that code with a try/except block and log any errors.

      The return values of teardown functions are ignored.

      Changelog

      New in version 0.9.

      Parameters:

      • f (T_teardown) โ€“

      Return type: T_teardown

      • teardown_appcontext_funcs: list[ft.TeardownCallable]

      A list of functions that are called when the application context is destroyed. Since the application context is also torn down if the request ends this is the place to store code that disconnects from databases.

      Changelog

      New in version 0.9.

    • teardown_request(f) โ€‹

      Register a function to be called when the request context is popped. Typically this happens at the end of each request, but contexts may be pushed manually as well during testing.

      python
      with app.test_request_context():
          ...
      with app.test_request_context():
          ...

      When the with block exits (or ctx.pop() is called), the teardown functions are called just before the request context is made inactive.

      When a teardown function was called because of an unhandled exception it will be passed an error object. If an errorhandler() is registered, it will handle the exception and the teardown will not receive it.

      Teardown functions must avoid raising exceptions. If they execute code that might fail they must surround that code with a try/except block and log any errors.

      The return values of teardown functions are ignored.

      This is available on both app and blueprint objects. When used on an app, this executes after every request. When used on a blueprint, this executes after every request that the blueprint handles. To register with a blueprint and execute after every request, use Blueprint.teardown_app_request().

      Parameters:

      • f (T_teardown) โ€“

      Return type: T_teardown

    • teardown_request_funcs: dict[ft.AppOrBlueprintKey, list[ft.TeardownCallable]] โ€‹

      A data structure of functions to call at the end of each request even if an exception is raised, in the format {scope: [functions]}. The scope key is the name of a blueprint the functions are active for, or None for all requests.

      To register a function, use the teardown_request() decorator.

      This data structure is internal. It should not be modified directly and its format may change at any time.

    • template_context_processors: dict[ft.AppOrBlueprintKey, list[ft.TemplateContextProcessorCallable]] โ€‹

      A data structure of functions to call to pass extra context values when rendering templates, in the format {scope: [functions]}. The scope key is the name of a blueprint the functions are active for, or None for all requests.

      To register a function, use the context_processor() decorator.

      This data structure is internal. It should not be modified directly and its format may change at any time.

    • template_filter(name=None) โ€‹

      A decorator that is used to register custom template filter. You can specify a name for the filter, otherwise the function name will be used. Example:

      python
      @app.template_filter()
      def reverse(s):
          return s[::-1]
      @app.template_filter()
      def reverse(s):
          return s[::-1]

      Parameters:

      • name (str | None) โ€“ the optional name of the filter, otherwise the function name will be used.

      Return type: Callable[[T_template_filter], T_template_filter]

    • template_folder โ€‹

      The path to the templates folder, relative to root_path, to add to the template loader. None if templates should not be added.

    • template_global(name=None) โ€‹

      A decorator that is used to register a custom template global function. You can specify a name for the global function, otherwise the function name will be used. Example:

      python
      @app.template_global()
      def double(n):
          return 2 * n
      @app.template_global()
      def double(n):
          return 2 * n
      Changelog

      New in version 0.10.

      Parameters:

      • name (str | None) โ€“ the optional name of the global function, otherwise the function name will be used.

      Return type: Callable[[T_template_global], T_template_global]

    • template_test(name=None) โ€‹

      A decorator that is used to register custom template test. You can specify a name for the test, otherwise the function name will be used. Example:

      python
      @app.template_test()
      def is_prime(n):
          if n == 2:
              return True
          for i in range(2, int(math.ceil(math.sqrt(n))) + 1):
              if n % i == 0:
                  return False
          return True
      @app.template_test()
      def is_prime(n):
          if n == 2:
              return True
          for i in range(2, int(math.ceil(math.sqrt(n))) + 1):
              if n % i == 0:
                  return False
          return True
      Changelog

      New in version 0.10.

      Parameters:

      • name (str | None) โ€“ the optional name of the test, otherwise the function name will be used.

      Return type: Callable[[T_template_test], T_template_test]

    • test_cli_runner(**kwargs) โ€‹

      Create a CLI runner for testing CLI commands. See Running Commands with the CLI Runner.

      Returns an instance of test_cli_runner_class, by default FlaskCliRunner. The Flask app object is passed as the first argument.

      Changelog

      New in version 1.0.

      Parameters:

      • kwargs (t.Any) โ€“

      Return type: FlaskCliRunner

    • test_cli_runner_class: type[FlaskCliRunner] | None = None โ€‹

      The CliRunner subclass, by default FlaskCliRunner that is used by test_cli_runner(). Its __init__ method should take a Flask app object as the first argument.

      Changelog

      New in version 1.0.

    • test_client(use_cookies=True, **kwargs) โ€‹

      Creates a test client for this application. For information about unit testing head over to Testing Flask Applications.

      Note that if you are testing for assertions or exceptions in your application code, you must set app.testing = True in order for the exceptions to propagate to the test client. Otherwise, the exception will be handled by the application (not visible to the test client) and the only indication of an AssertionError or other exception will be a 500 status code response to the test client. See the testing attribute. For example:

      python
      app.testing = True
      client = app.test_client()
      app.testing = True
      client = app.test_client()

      The test client can be used in a with block to defer the closing down of the context until the end of the with block. This is useful if you want to access the context locals for testing:

      python
      with app.test_client() as c:
          rv = c.get('/?vodka=42')
          assert request.args['vodka'] == '42'
      with app.test_client() as c:
          rv = c.get('/?vodka=42')
          assert request.args['vodka'] == '42'

      Additionally, you may pass optional keyword arguments that will then be passed to the applicationโ€™s test_client_class constructor. For example:

      python
      from flask.testing import FlaskClient
      
      class CustomClient(FlaskClient):
          def __init__(self, *args, **kwargs):
              self._authentication = kwargs.pop("authentication")
              super(CustomClient,self).__init__( *args, **kwargs)
      
      app.test_client_class = CustomClient
      client = app.test_client(authentication='Basic ....')
      from flask.testing import FlaskClient
      
      class CustomClient(FlaskClient):
          def __init__(self, *args, **kwargs):
              self._authentication = kwargs.pop("authentication")
              super(CustomClient,self).__init__( *args, **kwargs)
      
      app.test_client_class = CustomClient
      client = app.test_client(authentication='Basic ....')

      See FlaskClient for more information.

      Changelog

      Changed in version 0.11: Added **kwargs to support passing additional keyword arguments to the constructor of test_client_class.

      New in version 0.7: The use_cookies parameter was added as well as the ability to override the client to be used by setting the test_client_class attribute.

      Changed in version 0.4: added support for with block usage for the client.

      Parameters:

      • use_cookies (bool) โ€“

      • kwargs (t.Any) โ€“

      Return type: FlaskClient

    • test_client_class: type[FlaskClient] | None = None โ€‹

      The test_client() method creates an instance of this test client class. Defaults to FlaskClient.

      Changelog

      New in version 0.7.

    • test_request_context(*args, **kwargs) โ€‹

      Create a RequestContext for a WSGI environment created from the given values. This is mostly useful during testing, where you may want to run a function that uses request data without dispatching a full request.

      See The Request Context.

      Use a with block to push the context, which will make request point at the request for the created environment.

      python
      with app.test_request_context(...):
          generate_report()
      with app.test_request_context(...):
          generate_report()

      When using the shell, it may be easier to push and pop the context manually to avoid indentation.

      python
      ctx = app.test_request_context(...)
      ctx.push()
      ...
      ctx.pop()
      ctx = app.test_request_context(...)
      ctx.push()
      ...
      ctx.pop()

      Takes the same arguments as Werkzeugโ€™s EnvironBuilder, with some defaults from the application. See the linked Werkzeug docs for most of the available arguments. Flask-specific behavior is listed here.

      Parameters:

      • path โ€“ URL path being requested.

      • base_url โ€“ Base URL where the app is being served, which path is relative to. If not given, built from PREFERRED_URL_SCHEME, subdomain, SERVER_NAME, and APPLICATION_ROOT.

      • subdomain โ€“ Subdomain name to append to SERVER_NAME.

      • url_scheme โ€“ Scheme to use instead of PREFERRED_URL_SCHEME.

      • data โ€“ The request body, either as a string or a dict of form keys and values.

      • json โ€“ If given, this is serialized as JSON and passed as data. Also defaults content_type to application/json.

      • args (Any) โ€“ other positional arguments passed to EnvironBuilder.

      • kwargs (Any) โ€“ other keyword arguments passed to EnvironBuilder.

      Return type: RequestContext

    • testing โ€‹

      The testing flag. Set this to True to enable the test mode of Flask extensions (and in the future probably also Flask itself). For example this might activate test helpers that have an additional runtime cost which should not be enabled by default.

      If this is enabled and PROPAGATE_EXCEPTIONS is not changed from the default itโ€™s implicitly enabled.

      This attribute can also be configured from the config with the TESTING configuration key. Defaults to False.

    • trap_http_exception(e) โ€‹

      Checks if an HTTP exception should be trapped or not. By default this will return False for all exceptions except for a bad request key error if TRAP_BAD_REQUEST_ERRORS is set to True. It also returns True if TRAP_HTTP_EXCEPTIONS is set to True.

      This is called for all HTTP exceptions raised by a view function. If it returns True for any exception the error handler for this exception is not called and it shows up as regular exception in the traceback. This is helpful for debugging implicitly raised HTTP exceptions.

      Changelog

      Changed in version 1.0: Bad request errors are not trapped by default in debug mode.

      New in version 0.8.

      Parameters:

      • e (Exception) โ€“

      Return type: bool

    • update_template_context(context) โ€‹

      Update the template context with some commonly used variables. This injects request, session, config and g into the template context as well as everything template context processors want to inject. Note that the as of Flask 0.6, the original values in the context will not be overridden if a context processor decides to return a value with the same key.

      Parameters:

      • context (dict) โ€“ the context as a dictionary that is updated in place to add extra variables.

      Return type: None

    • url_build_error_handlers: list[t.Callable[[Exception, str, dict[str, t.Any]], str]] โ€‹

      A list of functions that are called by handle_url_build_error() when url_for() raises a BuildError. Each function is called with error, endpoint and values. If a function returns None or raises a BuildError, it is skipped. Otherwise, its return value is returned by url_for.

      Changelog

      New in version 0.9.

    • url_default_functions: dict[ft.AppOrBlueprintKey, list[ft.URLDefaultCallable]] โ€‹

      A data structure of functions to call to modify the keyword arguments when generating URLs, in the format {scope: [functions]}. The scope key is the name of a blueprint the functions are active for, or None for all requests.

      To register a function, use the url_defaults() decorator.

      This data structure is internal. It should not be modified directly and its format may change at any time.

    • url_defaults(f) โ€‹

      Callback function for URL defaults for all view functions of the application. Itโ€™s called with the endpoint and values and should update the values passed in place.

      This is available on both app and blueprint objects. When used on an app, this is called for every request. When used on a blueprint, this is called for requests that the blueprint handles. To register with a blueprint and affect every request, use Blueprint.app_url_defaults().

      Parameters:

      • f (T_url_defaults) โ€“

      Return type: T_url_defaults

    • url_for(endpoint, *, _anchor=None, _method=None, _scheme=None, _external=None, **values) โ€‹

      Generate a URL to the given endpoint with the given values.

      This is called by flask.url_for(), and can be called directly as well.

      An endpoint is the name of a URL rule, usually added with @app.route(), and usually the same name as the view function. A route defined in a Blueprint will prepend the blueprintโ€™s name separated by a . to the endpoint.

      In some cases, such as email messages, you want URLs to include the scheme and domain, like https://example.com/hello. When not in an active request, URLs will be external by default, but this requires setting SERVER_NAME so Flask knows what domain to use. APPLICATION_ROOT and PREFERRED_URL_SCHEME should also be configured as needed. This config is only used when not in an active request.

      Functions can be decorated with url_defaults() to modify keyword arguments before the URL is built.

      If building fails for some reason, such as an unknown endpoint or incorrect values, the appโ€™s handle_url_build_error() method is called. If that returns a string, that is returned, otherwise a BuildError is raised.

      Parameters:

      • endpoint (str) โ€“ The endpoint name associated with the URL to generate. If this starts with a ., the current blueprint name (if any) will be used.

      • _anchor (str | None) โ€“ If given, append this as #anchor to the URL.

      • _method (str | None) โ€“ If given, generate the URL associated with this method for the endpoint.

      • _scheme (str | None) โ€“ If given, the URL will have this scheme if it is external.

      • _external (bool | None) โ€“ If given, prefer the URL to be internal (False) or require it to be external (True). External URLs include the scheme and domain. When not in an active request, URLs are external by default.

      • values (Any) โ€“ Values to use for the variable parts of the URL rule. Unknown keys are appended as query string arguments, like ?a=b&c=d.

      Return type: str

      Changelog

      New in version 2.2: Moved from flask.url_for, which calls this method.

    • url_map โ€‹

      The Map for this instance. You can use this to change the routing converters after the class was created but before any routes are connected. Example:

      python
      from werkzeug.routing import BaseConverter
      
      class ListConverter(BaseConverter):
          def to_python(self, value):
              return value.split(',')
          def to_url(self, values):
              return ','.join(super(ListConverter, self).to_url(value)
                              for value in values)
      
      app = Flask(__name__)
      app.url_map.converters['list'] = ListConverter
      from werkzeug.routing import BaseConverter
      
      class ListConverter(BaseConverter):
          def to_python(self, value):
              return value.split(',')
          def to_url(self, values):
              return ','.join(super(ListConverter, self).to_url(value)
                              for value in values)
      
      app = Flask(__name__)
      app.url_map.converters['list'] = ListConverter
    • url_map_class โ€‹

      alias of Map

    • url_rule_class โ€‹

      alias of Rule

    • url_value_preprocessor(f) โ€‹

      Register a URL value preprocessor function for all view functions in the application. These functions will be called before the before_request() functions.

      The function can modify the values captured from the matched url before they are passed to the view. For example, this can be used to pop a common language code value and place it in g rather than pass it to every view.

      The function is passed the endpoint name and values dict. The return value is ignored.

      This is available on both app and blueprint objects. When used on an app, this is called for every request. When used on a blueprint, this is called for requests that the blueprint handles. To register with a blueprint and affect every request, use Blueprint.app_url_value_preprocessor().

      Parameters:

      • f (T_url_value_preprocessor) โ€“

      Return type: T_url_value_preprocessor

    • url_value_preprocessors: dict[ft.AppOrBlueprintKey, list[ft.URLValuePreprocessorCallable]] โ€‹

      A data structure of functions to call to modify the keyword arguments passed to the view function, in the format {scope: [functions]}. The scope key is the name of a blueprint the functions are active for, or None for all requests.

      To register a function, use the url_value_preprocessor() decorator.

      This data structure is internal. It should not be modified directly and its format may change at any time.

    • view_functions: dict[str, t.Callable] โ€‹

      A dictionary mapping endpoint names to view functions.

      To register a view function, use the route() decorator.

      This data structure is internal. It should not be modified directly and its format may change at any time.

    • wsgi_app(environ, start_response) โ€‹

      The actual WSGI application. This is not implemented in __call__() so that middlewares can be applied without losing a reference to the app object. Instead of doing this:

      python
      app = MyMiddleware(app)
      app = MyMiddleware(app)

      Itโ€™s a better idea to do this instead:

      python
      app.wsgi_app = MyMiddleware(app.wsgi_app)
      app.wsgi_app = MyMiddleware(app.wsgi_app)

      Then you still have the original application object around and can continue to call methods on it.

      Changelog

      Changed in version 0.7: Teardown events for the request and app contexts are called even if an unhandled error occurs. Other events may not be called depending on when an error occurs during dispatch. See Callbacks and Errors.

      Parameters:

      • environ (dict) โ€“ A WSGI environment.

      • start_response (Callable) โ€“ A callable accepting a status code, a list of headers, and an optional exception context to start the response.

      Return type: Any

Released under the MIT License.