Blueprint
This class provides a way to organize a group of related views, static files, and templates into a reusable and modular component within a larger application. It allows for registering CLI commands, serving static files, and opening resources relative to its root path, mirroring functionality found in the main Flask application class.
Attributes
| Attribute | Type | Description |
|---|---|---|
| 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. |
Constructor
Signature
def Blueprint(
name: str,
import_name: str,
static_folder: str | os.PathLike[str]| None = None,
static_url_path: str | None = None,
template_folder: str | os.PathLike[str]| None = None,
url_prefix: str | None = None,
subdomain: str | None = None,
url_defaults: dict[str, t.Any]| None = None,
root_path: str | None = None,
cli_group: str | None = _sentinel
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| name | str | The name of the blueprint. |
| import_name | str | The name of the blueprint's package, usually __name__. |
| static_folder | `str | os.PathLike[str] |
| static_url_path | `str | None` = None |
| template_folder | `str | os.PathLike[str] |
| url_prefix | `str | None` = None |
| subdomain | `str | None` = None |
| url_defaults | `dict[str, t.Any] | None` = None |
| root_path | `str | None` = None |
| cli_group | `str | None` = _sentinel |
Methods
get_send_file_max_age()
@classmethod
def get_send_file_max_age(
filename: str | None
) - > int | None
Used by :func:send_file to determine the max_age cache value for a given file path if it wasn't passed.
By default, this returns :data:SEND_FILE_MAX_AGE_DEFAULT from the configuration of :data:~flask.current_app. This defaults to None, which tells the browser to use conditional requests instead of a timed cache, which is usually preferable.
Note this is a duplicate of the same method in the Flask class.
Parameters
| Name | Type | Description |
|---|---|---|
| filename | `str | None` |
Returns
| Type | Description |
|---|---|
| `int | None` |
send_static_file()
@classmethod
def send_static_file(
filename: str
) - > [Response](../wrappers/response.md?sid=flask_wrappers_response)
The view function used to serve files from :attr:static_folder. A route is automatically registered for this view at :attr:static_url_path if :attr:static_folder is set.
Note this is a duplicate of the same method in the Flask class.
Parameters
| Name | Type | Description |
|---|---|---|
| filename | str | The name of the static file to be served from the static folder. |
Returns
| Type | Description |
|---|---|
[Response](../wrappers/response.md?sid=flask_wrappers_response) | A Flask Response object containing the static file. |
open_resource()
@classmethod
def open_resource(
resource: str,
mode: str = "rb",
encoding: str | None = "utf-8"
) - > t.IO[t.AnyStr]
Open a resource file relative to :attr:root_path for reading. The blueprint-relative equivalent of the app's :meth:~.Flask.open_resource method.
Parameters
| Name | Type | Description |
|---|---|---|
| resource | str | Path to the resource relative to :attr:root_path. |
| mode | str = "rb" | Open the file in this mode. Only reading is supported, valid values are "r" (or "rt") and "rb". |
| encoding | `str | None` = "utf-8" |
Returns
| Type | Description |
|---|---|
t.IO[t.AnyStr] | A file-like object for the opened resource. |