Flask Route
Just like other articles on the site, this particular article will give clues on how to perform URL Routing using Flask applications. Let's begin!
What is URL Routing?
This is a method used in linking certain information (which are web page content) to its web page URL.
There is a display of the content as soon as the endpoint is a strike and solely the task of the linked URL endpoint using the route. So, the route helps the website user to access the website and enjoy the website template and interphase.
There are various ways in which the flask route can be set up. The following are steps in which you can establish a route URL to function.
Related course: Create Web Apps with Python Flask
Flask Routing
The Use Of app.route()
When of the famous way to establish route Link is the use of the app. route() which require the use of the following syntax:
@app.route ('<endpoint>')
A typical example of a Flask application webpage with URL - localhost:5000/page a similar link will look like:
from flask import Flask
app = Flask(__name__)
@app.route('/blogs')
def blogs():
return 'Welcome to Blog site'
app.run(host='localhost', port=5000)
Caution: the same command name must be used as the endpoint name.
How to run the application is as follows:
python filename.py
There can be a URL with different endpoints URL with variable endpoint are used to develop a website that receives argument as a command from its users.
Evaluate the function:
from flask import Flask
app = Flask(__name__)
@app.route('/blogs/<int:id>')
def blogs(id):
return f"Welcome to Blog number:{id}"
app.run(host='localhost', port=5000)
Caution: The endpoint variable (id) is used to represent the argument function while the non-variable endpoint (blogs) will serve as the function name.
Presently, it possible you think the webpage will display different output based on the variable endpoint.
The Use of Add_Url_Route() Attribute: The above command is commonly used to route a externally command with the avoidance to the use of decorator. The structure are arranged as follow: app.add_url_route('
from flask import Flask
app = Flask(__name__)
def blogs():
return f"Welcome to Blog Site"
app.add_url_rule('/blogs','blogs', blogs)
app.run(host='localhost', port=5000)
The displayed output isn't going to change here either. Likewise, the variable endpoint for syntax is represented below:
app.add_url_rule('<url_rule_with_variable>','<endpoint_name>', <view_function>)
Here, the syntax for variable endpoint files are:
from flask import Flask
app = Flask(__name__)
def blogs(id):
return f"Welcome to Blog number:{id}"
app.add_url_rule('/blogs/<int:id>','blogs',blogs)
app.run(host='localhost', port=5000)
Launch the application and take note of the displayed output: Also, the displayed output still maintains the same function as before.
Conclusion
Guys, we've come to the end of this tutorial class. It will be nice if you can try the above illustration. That will help you understand this course better. Stay glued to this website as we promise to give a solution to all your coding problems. Enjoy coding!
Related course: Create Web Apps with Python Flask