Register a blueprint on the application. See [Blueprints](/en/guide/best-practices/blueprints) for more information. Args: blueprint (Union[Blueprint, Iterable[Blueprint], BlueprintGroup]): Blueprint object or (list, tuple) thereof. url_prefix (Optional[str]
(
self,
blueprint: Blueprint | Iterable[Blueprint] | BlueprintGroup,
*,
url_prefix: str | None = None,
version: int | float | str | None = None,
strict_slashes: bool | None = None,
version_prefix: str | None = None,
name_prefix: str | None = None,
)
| 847 | self.websocket_enabled = enable |
| 848 | |
| 849 | def blueprint( |
| 850 | self, |
| 851 | blueprint: Blueprint | Iterable[Blueprint] | BlueprintGroup, |
| 852 | *, |
| 853 | url_prefix: str | None = None, |
| 854 | version: int | float | str | None = None, |
| 855 | strict_slashes: bool | None = None, |
| 856 | version_prefix: str | None = None, |
| 857 | name_prefix: str | None = None, |
| 858 | ) -> None: |
| 859 | """Register a blueprint on the application. |
| 860 | |
| 861 | See [Blueprints](/en/guide/best-practices/blueprints) for more information. |
| 862 | |
| 863 | Args: |
| 864 | blueprint (Union[Blueprint, Iterable[Blueprint], BlueprintGroup]): Blueprint object or (list, tuple) thereof. |
| 865 | url_prefix (Optional[str]): Prefix for all URLs bound to the blueprint. Defaults to `None`. |
| 866 | version (Optional[Union[int, float, str]]): Version prefix for URLs. Defaults to `None`. |
| 867 | strict_slashes (Optional[bool]): Enforce the trailing slashes. Defaults to `None`. |
| 868 | version_prefix (Optional[str]): Prefix for version. Defaults to `None`. |
| 869 | name_prefix (Optional[str]): Prefix for the blueprint name. Defaults to `None`. |
| 870 | |
| 871 | Example: |
| 872 | ```python |
| 873 | app = Sanic("TestApp") |
| 874 | bp = Blueprint('TestBP') |
| 875 | |
| 876 | @bp.route('/route') |
| 877 | def handler(request): |
| 878 | return text('Hello, Blueprint!') |
| 879 | |
| 880 | app.blueprint(bp, url_prefix='/blueprint') |
| 881 | ``` |
| 882 | """ # noqa: E501 |
| 883 | options: dict[str, Any] = {} |
| 884 | if url_prefix is not None: |
| 885 | options["url_prefix"] = url_prefix |
| 886 | if version is not None: |
| 887 | options["version"] = version |
| 888 | if strict_slashes is not None: |
| 889 | options["strict_slashes"] = strict_slashes |
| 890 | if version_prefix is not None: |
| 891 | options["version_prefix"] = version_prefix |
| 892 | if name_prefix is not None: |
| 893 | options["name_prefix"] = name_prefix |
| 894 | if isinstance(blueprint, (Iterable, BlueprintGroup)): |
| 895 | for item in blueprint: |
| 896 | params: dict[str, Any] = {**options} |
| 897 | if isinstance(blueprint, BlueprintGroup): |
| 898 | merge_from = [ |
| 899 | options.get("url_prefix", ""), |
| 900 | blueprint.url_prefix or "", |
| 901 | ] |
| 902 | if not isinstance(item, BlueprintGroup): |
| 903 | merge_from.append(item.url_prefix or "") |
| 904 | merged_prefix = "/".join( |
| 905 | str(u).strip("/") for u in merge_from if u |
| 906 | ).rstrip("/") |