How to understand the parameters of blueprint in flask

:
|--hello.py
|--admin
    |--__init__.py
    |--admin.py
    

hello.py

from flask import Flask
from admin.admin import admin

app = Flask(__name__)
app.register_blueprint(admin, url_prefix="/admin")

admin.py

from flask import Blueprint

admin = Blueprint("666", __name__)

@admin.route("/")
def index():
    return "<h1>X</h1>"
    

_ _ init__.py
empty file

my question here is that the parameter "666" in admin = Blueprint (" 666percent, _ _ name__) is changed to any program that can run normally, so what exactly does this parameter refer to and where is it?
Thank you!

Mar.02,2021

I always thought that the name parameter in Blueprint is related to the endpoint (endpoint) used in url_for. Here's why I understand it this way.

first of all, let's take a look at the source code of flask: https://github.com/pallets/fl.
, where there is the following code:

class Blueprint(_PackageBoundObject):
    ...
    ...
    def __init__(self, name, import_name, static_folder=None,
                 static_url_path=None, template_folder=None,
                 url_prefix=None, subdomain=None, url_defaults=None,
                 root_path=None):
        _PackageBoundObject.__init__(self, import_name, template_folder,
                                     root_path=root_path)
        self.name = name
        self.url_prefix = url_prefix
        self.subdomain = subdomain
        self.static_folder = static_folder
        self.static_url_path = static_url_path
        self.deferred_functions = []
        if url_defaults is None:
            url_defaults = {}
        self.url_values_defaults = url_defaults

the above code clearly shows that the Blueprint class inherits the name parameter of _ PackageBoundObject, but the parameter defined by the class itself, so we continue to find the function of the name parameter in the source code.

looking for self.name, in this class, we can see another 8 places. In the eight class functions of this class, namely, before_request, after_request, teardown_request, context_processor, url_value_preprocessor, url_defaults, errorhandler and register_error_handle, basically all the functions are used as follows:

 self.record_once(lambda s: s.app.before_request_funcs
            .setdefault(self.name, []).append(f))

so, let's look at the function of the record_once function, which is also defined in the class .

    def record_once(self, func):
        """Works like :meth:`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.
        """
        def wrapper(state):
            if state.first_registration:
                func(state)
        return self.record(update_wrapper(wrapper, func))

through comments, it is clear that the purpose of this function is to use the name parameter as the unique identifier to distinguish the blueprint in the program.

so, back to the subject's question.
1. The reason why you can define a name casually, it feels like it doesn't matter, that's an illusion.

  • the first reason is that you define a blueprint. If you define another one, you set the parameters of name to the same, and you try to see if the program will report an error.
  • the second reason is that you don't use the url_for function, which uses your name parameter, or you call the url_for ('.index') function directly in this simplified way, that'.' Represents the current blueprint.
  • goes back to the url_for function. If you call it in html's jinja2, you have to write url_for ('name.index'), where name is the name parameter of the blueprint you defined.

2. You can try that if you do not define the name parameter, the program will report an error, because this parameter is required.

  • in the view view, we use the decorator to write, usually the function name as endpoint, if you are in two different blueprints, using the same name to define the view function, then endpoint according to the default scheme can not be unique identification , you must add the name of the blueprint, so the name of the blueprint must be unique.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e99de5-47568.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e99de5-47568.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?