Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm new to flask and python. For example, if you have the following code...

@app.route('/')
def index():
 return "Hello world!"

@app.route('/accounts')
def accounts():
    return some_data

@app.route('/login')
def login():
    return render_template(login.html)

if __name__ == "__main__":
    app.run()

In what order will these execute? From what I understand if you are on the /accounts page of the website, that function will run. If you are on the /login page, that function will run. What role does the parameter for the route method play? Secondly, how many times will the last two lines of code be run (will the app.run() be called once or every time the app.route() is called)? Additionally, what folder should an app be put in within a filestructure for a website?

like image 472
J.E.C. Avatar asked Feb 28 '17 21:02

J.E.C.


People also ask

How does app route work in Flask?

App Routing means mapping the URLs to a specific function that will handle the logic for that URL. Modern web frameworks use more meaningful URLs to help users remember the URLs and make navigation simpler. Example: In our application, the URL (“/”) is associated with the root URL.

What does app route () do?

App routing is used to map the specific URL with the associated function that is intended to perform some task. It is used to access some particular page like Flask Tutorial in the web application.

How does Flask route decorator work?

For Flask, you define the routes using decorator functions. When you navigate to a URL that matches the decorator, it will execute the decorated function. In this example, if I navigate to "/" on my webserver, the hello function will execute.


1 Answers

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.py
static/
templates/
    - login.html

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

like image 78
Ananth Rao Avatar answered Oct 20 '22 19:10

Ananth Rao