Hello, studying the book on Python, came across such code:

from flask import Flask app = Flask(__name__) @app.route('/') def hello() -> str: return 'Hello world from Flask' app.run() 

Here (in the book) it is indicated that "the Flask class needs to know the current value of the name when a new Flask object is created, so it must be passed in the argument, which we did" - I didn’t understand why.

Well and accordingly, I also did not understand how the route works. Explain, please, as clearly as possible.

    1 answer 1

    name is a special variable (module attribute) in which the module name is stored. Just about the modules - https://wombat.org.ua/AByteOfPython/modules.html @function is a decorator. Roughly speaking, a decorator is a function that changes the behavior of another function, Google has a lot of information about decorators, so I will not write examples. In the current example, @ app.route processes the hello () function before its execution @app.route def hello(): pass
    is the same as hello_new = app.route(hello) # hello_new - это измененная функция hello() . Touching the fly itself, app.route treats the hello function as an address (link) to the presentation function of our application. That is, when launching our application, to see 'Hello world from Flask', we need to refer to the hello function, which will return this string to us, and the function is called by specifying its name as the path to the application page. http: \ app_host: app_port \ hello.