How do python flask app.route() sections execute? How do python flask app.route() sections execute? flask flask

How do python flask app.route() sections execute?


You are correct, each function here is going to be triggered when you go to a specific page of the site. There is definitely some research you need to do further to gain better understanding, but here's a simple breakdown.

In Python, the "main" file that is run (meaning the file that you run with the python command: python etc.py) will have its file name internally replaced with "__main__" before it is interpreted. So based on that knowledge, we can say that if you run the file with this code as your main file, the code inside that conditional statement if __name__ == "__main__" will be executed. Therefore, app.run() is executed, and it is executed exactly once, because this if block is not inside any kind of class, function, or control structure, and a file is only interpreted once.

Next, @app.route(....) is something called a decorator. Here's a good intro to what decorators are and how to use them: https://realpython.com/blog/python/primer-on-python-decorators/. The first paragraph there really gets it head on.

By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.

I also really like the Intermediate Python chapter on decorators which I would recommend you check out. http://book.pythontips.com/en/latest/decorators.html

Basically these route functions you're defining, such as:

def index():  return "Hello world!"

are like functions you are telling the @app.route() decorator to call at some point. @app.route() is a decorator, it has been defined to call in the function you give it at some point during the execution of its internal commands. The input argument it takes is what is called a url rule, which is basically like a pattern that an incoming request's url has to match in order to trigger the function you defined for the route to take. This decorator interface is actually a convenience, see [here][1]. In other words, to make a route with a function to handle , you could also use the app.add_url_rule() function instead of a decorator. As taken from Flask's documentation:

add_url_rule(rule, endpoint=None, view_func=None, **)

Connects a URL rule. Works exactly like the route() decorator. If a view_func is provided it will be registered with the endpoint.

I've just outlined it at a very generic level, but you need to dive into the Flask documentation to see what more you can do with it.

EDIT: Just realized I didn't answer all of your questions. Typically, Flask looks for a static/ folder for serving up static assets like css and html pages that don't ever change, and a templates/ folder for templates that typically have placeholders that are to be filled in by data coming from the server. So a single file Flask app based on your code might look something like this:

- app.pystatic/templates/    - login.html

where login.html is a template to be populated with data and rendered for the /login page