2. Blueprint Objects¶
-
class
flask.
Blueprint
(name, import_name, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None)[source]¶ Represents a blueprint. A blueprint is an object that records functions that will be called with the
BlueprintSetupState
later to register functions or other things on the main application. See Modular Applications with Blueprints for more information.New in version 0.7.
-
add_app_template_filter
(f, name=None)[source]¶ Register a custom template filter, available application wide. Like
Flask.add_template_filter()
but for a blueprint. Works exactly like theapp_template_filter()
decorator.Parameters: name – the optional name of the filter, otherwise the function name will be used.
-
add_app_template_global
(f, name=None)[source]¶ Register a custom template global, available application wide. Like
Flask.add_template_global()
but for a blueprint. Works exactly like theapp_template_global()
decorator.New in version 0.10.
Parameters: name – the optional name of the global, otherwise the function name will be used.
-
add_app_template_test
(f, name=None)[source]¶ Register a custom template test, available application wide. Like
Flask.add_template_test()
but for a blueprint. Works exactly like theapp_template_test()
decorator.New in version 0.10.
Parameters: name – the optional name of the test, otherwise the function name will be used.
-
add_url_rule
(rule, endpoint=None, view_func=None, **options)[source]¶ Like
Flask.add_url_rule()
but for a blueprint. The endpoint for theurl_for()
function is prefixed with the name of the blueprint.
-
after_app_request
(f)[source]¶ Like
Flask.after_request()
but for a blueprint. Such a function is executed after each request, even if outside of the blueprint.
-
after_request
(f)[source]¶ Like
Flask.after_request()
but for a blueprint. This function is only executed after each request that is handled by a function of that blueprint.
-
app_context_processor
(f)[source]¶ Like
Flask.context_processor()
but for a blueprint. Such a function is executed each request, even if outside of the blueprint.
-
app_errorhandler
(code)[source]¶ Like
Flask.errorhandler()
but for a blueprint. This handler is used for all requests, even if outside of the blueprint.
-
app_template_filter
(name=None)[source]¶ Register a custom template filter, available application wide. Like
Flask.template_filter()
but for a blueprint.Parameters: name – the optional name of the filter, otherwise the function name will be used.
-
app_template_global
(name=None)[source]¶ Register a custom template global, available application wide. Like
Flask.template_global()
but for a blueprint.New in version 0.10.
Parameters: name – the optional name of the global, otherwise the function name will be used.
-
app_template_test
(name=None)[source]¶ Register a custom template test, available application wide. Like
Flask.template_test()
but for a blueprint.New in version 0.10.
Parameters: name – the optional name of the test, otherwise the function name will be used.
-
app_url_defaults
(f)[source]¶ Same as
url_defaults()
but application wide.
-
app_url_value_preprocessor
(f)[source]¶ Same as
url_value_preprocessor()
but application wide.
-
before_app_first_request
(f)[source]¶ Like
Flask.before_first_request()
. Such a function is executed before the first request to the application.
-
before_app_request
(f)[source]¶ Like
Flask.before_request()
. Such a function is executed before each request, even if outside of a blueprint.
-
before_request
(f)[source]¶ Like
Flask.before_request()
but for a blueprint. This function is only executed before each request that is handled by a function of that blueprint.
-
context_processor
(f)[source]¶ Like
Flask.context_processor()
but for a blueprint. This function is only executed for requests handled by a blueprint.
-
endpoint
(endpoint)[source]¶ Like
Flask.endpoint()
but for a blueprint. This does not prefix the endpoint with the blueprint name, this has to be done explicitly by the user of this method. If the endpoint is prefixed with a . it will be registered to the current blueprint, otherwise it’s an application independent endpoint.
-
errorhandler
(code_or_exception)[source]¶ Registers an error handler that becomes active for this blueprint only. Please be aware that routing does not happen local to a blueprint so an error handler for 404 usually is not handled by a blueprint unless it is caused inside a view function. Another special case is the 500 internal server error which is always looked up from the application.
Otherwise works as the
errorhandler()
decorator of theFlask
object.
-
get_send_file_max_age
(filename)¶ Provides default cache_timeout for the
send_file()
functions.By default, this function returns
SEND_FILE_MAX_AGE_DEFAULT
from the configuration ofcurrent_app
.Static file functions such as
send_from_directory()
use this function, andsend_file()
calls this function oncurrent_app
when the given cache_timeout isNone
. If a cache_timeout is given insend_file()
, that timeout is used; otherwise, this method is called.This allows subclasses to change the behavior when sending files based on the filename. For example, to set the cache timeout for .js files to 60 seconds:
class MyFlask(flask.Flask): def get_send_file_max_age(self, name): if name.lower().endswith('.js'): return 60 return flask.Flask.get_send_file_max_age(self, name)
New in version 0.9.
-
has_static_folder
¶ This is
True
if the package bound object’s container has a folder for static files.New in version 0.5.
-
jinja_loader
¶ The Jinja loader for this package bound object.
New in version 0.5.
-
make_setup_state
(app, options, first_registration=False)[source]¶ Creates an instance of
BlueprintSetupState()
object that is later passed to the register callback functions. Subclasses can override this to return a subclass of the setup state.
-
open_resource
(resource, mode='rb')¶ Opens a resource from the application’s resource folder. To see how this works, consider the following folder structure:
/myapplication.py /schema.sql /static /style.css /templates /layout.html /index.html
If you want to open the
schema.sql
file you would do the following:with app.open_resource('schema.sql') as f: contents = f.read() do_something_with(contents)
Parameters: - resource – the name of the resource. To access resources within subfolders use forward slashes as separator.
- mode – resource file opening mode, default is ‘rb’.
-
record
(func)[source]¶ Registers a function that is called when the blueprint is registered on the application. This function is called with the state as argument as returned by the
make_setup_state()
method.
-
record_once
(func)[source]¶ Works like
record()
but wraps the function in another function that will ensure the function is only called once. If the blueprint is registered a second time on the application, the function passed is not called.
-
register
(app, options, first_registration=False)[source]¶ Called by
Flask.register_blueprint()
to register a blueprint on the application. This can be overridden to customize the register behavior. Keyword arguments fromregister_blueprint()
are directly forwarded to this method in the options dictionary.
-
register_error_handler
(code_or_exception, f)[source]¶ Non-decorator version of the
errorhandler()
error attach function, akin to theregister_error_handler()
application-wide function of theFlask
object but for error handlers limited to this blueprint.New in version 0.11.
-
route
(rule, **options)[source]¶ Like
Flask.route()
but for a blueprint. The endpoint for theurl_for()
function is prefixed with the name of the blueprint.
-
send_static_file
(filename)¶ Function used internally to send static files from the static folder to the browser.
New in version 0.5.
-
static_folder
¶ The absolute path to the configured static folder.
-
teardown_app_request
(f)[source]¶ Like
Flask.teardown_request()
but for a blueprint. Such a function is executed when tearing down each request, even if outside of the blueprint.
-
teardown_request
(f)[source]¶ Like
Flask.teardown_request()
but for a blueprint. This function is only executed when tearing down requests handled by a function of that blueprint. Teardown request functions are executed when the request context is popped, even when no actual request was performed.
-