Each time we visit a URL in a browser, it makes a request to the web server, which processes the request and returns a response back to the browser. In our Flask app, we can create endpoints to handle the various requests. Requests from different URLs can be directed to different endpoints in a process called routing.
To build a route, we need to first define a function, known as a view function, that contains the code for processing the request and generating a response. The response could be something as simple as a string of text. Then, we can use the
route() decorator to bind a URL to the view function such that the function will be triggered when the URL is visited:
@app.route('/') def home(): return 'Hello, World!'
route() decorator takes the URL path as parameter, or the part of the URL that follows the domain name. All URL paths must start with a leading slash. In the above example, if we visit http://localhost:5000/ in the browser,
Hello, World! will be displayed on the webpage.
Multiple URLs can also be bound to the same view function:
@app.route('/') @app.route('/home') def home(): return 'Hello, World!'
Define a function called
home() that returns
route() decorator to bind the URL path
'/' to the view function.
Run app.py and view your page at http://localhost:5000/ in the browser. What do you see?
Bind a second URL path
'/home' to the
Navigate to http://localhost:5000/home in the browser.
Let’s create another route! Define a function called
reporter() that returns
'Reporter Bio' and is bound to the path
Navigate to http://localhost:5000/reporter in the browser.