Flask
TheFlask class implements a WSGI application and acts as the central object. It is passed the name of the module or package of the application.
Constructor
The name of the application package. Usually
__name__.Can be used to specify a different path for the static files on the web. Defaults to the name of the
static_folder folder.The folder with static files that is served at
static_url_path. Relative to the application root_path or an absolute path.The host to use when adding the static route. Required when using
host_matching=True with a static_folder configured.Set
url_map.host_matching attribute.Consider the subdomain relative to
SERVER_NAME when matching routes.The folder that contains the templates that should be used by the application.
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.If set to
True relative filenames for loading the config are assumed to be relative to the instance path instead of the application root.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.
Example
Routing Methods
route()
Decorate a view function to register it with the given URL rule and options.The URL rule string.
A list of HTTP methods this rule should handle.
HEAD and OPTIONS are added automatically.The endpoint name for the route. Defaults to the name of the view function.
Example
add_url_rule()
Register a rule for routing incoming requests and building URLs.The URL rule string.
The endpoint name to associate with the rule and view function. Defaults to
view_func.__name__.The view function to associate with the endpoint name.
Add the
OPTIONS method and respond to OPTIONS requests automatically.Example
HTTP Method Shortcuts
Flask provides shortcuts for common HTTP methods:Example
Request Handling Hooks
before_request()
Register a function to run before each request.None value, the value is handled as if it was the return value from the view, and further request handling is stopped.
Example
after_request()
Register a function to run after each request.The response object that will be sent to the client.
Example
teardown_request()
Register a function to be called when the request context is popped.An unhandled exception that occurred during the request, if any.
Example
teardown_appcontext()
Register a function to be called when the application context is destroyed.Error Handling
errorhandler()
Register a function to handle errors by code or exception class.The HTTP status code as an integer or an exception class.
Example
register_error_handler()
Non-decorator alternative toerrorhandler().
Template Context
context_processor()
Register a template context processor function.Example
template_filter()
Register a custom template filter.template_global()
Register a custom template global function.template_test()
Register a custom template test.URL Building
url_for()
Generate a URL to the given endpoint with the given values.The endpoint name associated with the URL to generate.
If given, append this as
#anchor to the URL.If given, generate the URL associated with this method for the endpoint.
If given, the URL will have this scheme if it is external.
If given, prefer the URL to be internal (False) or require it to be external (True).
**values
Values to use for the variable parts of the URL rule. Unknown keys are appended as query string arguments.
url_defaults()
Register a function to modify keyword arguments when generating URLs.url_value_preprocessor()
Register a URL value preprocessor function.Running the Application
run()
Runs the application on a local development server.The hostname to listen on. Set to
'0.0.0.0' to have the server available externally.The port of the webserver.
If given, enable or disable debug mode.
Load the nearest
.env and .flaskenv files to set environment variables.Example
Testing
test_client()
Creates a test client for this application.Whether to enable cookies in the test client.
FlaskClient instance.
Example
test_cli_runner()
Create a CLI runner for testing CLI commands.FlaskCliRunner instance.
Other Utility Methods
make_response()
Convert the return value from a view function to an instance ofresponse_class.
open_resource()
Open a resource file relative toroot_path for reading.
Path to the resource relative to
root_path.Open the file in this mode. Only reading is supported:
"r" (or "rt") and "rb".Open the file with this encoding when opening in text mode.
