#π I dont understand flasks in python
24 messages Β· Page 1 of 1 (latest)
@jovial aurora
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
The app.route decorator registers the say_hello function in the app's mapping which says what functions to run for what URLs.
When someone fetches the URL http://yoursite/hello the flask app then knows to call the hello function to obtain the text to return in the web page.
what i dont understand, is how the say_hello function runs when someone visits the site
is it because its below the @app.route("/hello")
Not really
but someome showed me this
Which kinda makes sense assuming its actually the same
It is. A decorator is a function which gets applied to another function. Sometimes it does something with the function, like flask does here, then returns the originl function. Sometimes it defines a new function which will wrap the original function, and returns the wrapper function.
thats what the decoration is
So in this case, the code:
@app.route("/hello")
def say_hello():
calls app.route("/hello")(say_hello)
hich seems round about, but sane.
It does this to record say_hello against a table of URLs, in this case for the /hello URL.
The app.route decorator will return say_hello unchanged. But it will have recorded it in the registry of functions-which-serve-URLs.
@app.route("/hello")
def index():
return "hello"
# same as
def index():
return "hello"
app.route("\hello")(index) ```
so could you describe a decorator, as do this thing, but do this other thing first? or it like for example modifing somthing
in what other way, or concept, could @app.route, etc, be explained , like is they a simpler like more basic python version of this? or is this just as it is
the "@" simbol will basically apply that
It's more like this:
def app_route(url_path, func):
flask.url_mapping[url_path] = func
return func
Ignoring the complexity, this is what's happening - the say_hello function and the URL /hello are being passed to a function (the decorator) which records the function in a table. Then returns the original function.
So when the @ is used, which means:
say_hello = app.route("/hello")(say_hello)
it's defining say_hello as itself. But during the decoration it gets recorded in flask's table.
i give up rn, im going to come back to thsi later, my brain feels fogged, thank you tho, you can explain anything else if there is
having a good rest helps everyone
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.