YouTip LogoYouTip

Flask Blueprint Api

Blueprint is used to split an application into reusable modules. Each blueprint can have its own routes, templates, static files, and hooks.\\n\\nCreation method:\\n\\nbp = Blueprint("auth", __name__)\\n\\n* * *\\n\\n## Constructor Parameters\\n\\n| Parameter | Type | Default | Description |\\n| --- | --- | --- | --- |\\n| name | str | Required | Blueprint name, used as the endpoint prefix in url_for |\\n| import_name | str | Required | The module name where the blueprint is located, used to locate resources. Usually passes __name__ |\\n| static_folder | str | None | The static file folder for the blueprint |\\n| static_url_path | str | None | The URL path exposed for static files |\\n| template_folder | str | None | The template folder for the blueprint |\\n| url_prefix | str | None | A unified prefix for all routes in the blueprint, e.g., "/auth" |\\n| subdomain | str | None | The subdomain that the blueprint routes will match |\\n| url_defaults | dict | None | Default URL parameters for the blueprint routes |\\n| root_path | str | None | The root path of the blueprint, usually automatically discovered |\\n| cli_group | str | None | CLI command group name. Defaults to the blueprint name |\\n\\n* * *\\n\\n## Core Attributes\\n\\n| Attribute | Type | Description |\\n| --- | --- | --- |\\n| name | str | Blueprint name, used as the endpoint prefix after registration |\\n| cli | AppGroup | The blueprint's dedicated CLI command group, custom commands can be added |\\n| has_static_folder | bool | Whether a static folder is configured |\\n\\n* * *\\n\\n## Routing Methods\\n\\nThe usage of a blueprint is consistent with the Flask application, simply replace app with bp:\\n\\n| Method | Description |\\n| --- | --- |\\n| route(rule, **options) | Decorator, binds a URL to a view function |\\n| get / post / put / delete / patch(rule, **options) | HTTP method shortcut decorators |\\n| add_url_rule(rule, endpoint, view_func, **options) | Programmatically add a URL rule |\\n\\n* * *\\n\\n## Hook Decorators\\n\\nBlueprint hooks only affect the routes within that blueprint:\\n\\n| Method | Description |\\n| --- | --- |\\n| before_request | Executed before all requests within the blueprint |\\n| after_request | Executed after a request response within the blueprint |\\n| teardown_request | Executed when the request context is destroyed within the blueprint |\\n| errorhandler(code_or_exception) | Error handler within the blueprint |\\n| url_value_preprocessor | URL value preprocessor within the blueprint |\\n| url_defaults | Default URL parameters within the blueprint |\\n| template_filter / template_test / template_global | Blueprint template filter/test/global function |\\n\\n* * *\\n\\n## Blueprint-Specific Methods\\n\\n| Method | Description |\\n| --- | --- |\\n| register(app, options) | Registers the blueprint to the Flask application. Called internally by app.register_blueprint() |\\n| record(func) | Registers a callback function that is called when the blueprint is registered to the application |\\n| record_once(func) | Same as record, but only executes once when the blueprint is registered multiple times |\\n| make_setup_state(app, options) | Creates a blueprint registration state object |\\n| send_static_file(filename) | Sends a file from the blueprint's static folder |\\n| open_resource(resource, mode="rb") | Opens a resource file under the blueprint's root path |\\n\\n* * *\\n\\n## Options Parameter When Registering a Blueprint\\n\\nCan be passed when calling app.register_blueprint(bp, **options):\\n\\n| Parameter | Description |\\n| --- | --- |\\n| url_prefix | Overrides the blueprint's URL prefix |\\n| subdomain | Overrides the blueprint's subdomain |\\n| url_defaults | Overrides the blueprint's URL defaults |\\n| name | Renames the blueprint (the same blueprint can be registered multiple times, each with a different name) |\\n\\n* * *\\n\\n## Code Example\\n\\n## Instance\\n\\nfrom flask import Blueprint, render_template\\n\\n# Create Blueprint\\n\\n bp = Blueprint("blog", __name__,\\n\\nurl_prefix="/blog",\\n\\ntemplate_folder="templates",\\n\\nstatic_folder="static")\\n\\n# Register routes\\n\\n@bp.route("/")\\n\\ndef index():\\n\\nreturn render_template("blog/index.html")\\n\\n@bp.route("/")\\n\\ndef show(post_id):\\n\\nreturn f"Show post {post_id}"\\n\\n# Hooks within Blueprint\\n\\n@bp.before_request\\n\\ndef check_login():\\n\\n# Only for /blog/* Path Takes Effect\\n\\npass\\n\\n# in app Register in\\n\\n# app.register_blueprint(bp)
← Flask Response ApiFlask Request Lifecycle β†’