#πŸ”’ I dont understand flasks in python

24 messages Β· Page 1 of 1 (latest)

jovial aurora
#

Can someone explain this code. I dont understand, how the app.route, triggers the function say_hello, how are they related? or linked?

glossy bobcatBOT
#

@jovial aurora

Python help channel opened

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.

winter arch
#

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.

jovial aurora
#

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")

winter arch
#

Yes.

#

Do you know what a decorator is?

jovial aurora
#

Not really

#

but someome showed me this

#

Which kinda makes sense assuming its actually the same

winter arch
#

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.

placid socket
#

thats what the decoration is

winter arch
#

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.

placid socket
#
@app.route("/hello")
def index():
  return "hello"

# same as
def index():
  return "hello"
app.route("\hello")(index) ```
jovial aurora
jovial aurora
placid socket
#

the "@" simbol will basically apply that

winter arch
#

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.

jovial aurora
#

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

placid socket
#

having a good rest helps everyone

glossy bobcatBOT
#
Python help channel closed for inactivity

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.