Basic Routing in Flask


Source Code

from flask import Flask
 
app = Flask(__name__)
 
@app.route('/') 
def index():
    return '<h1>Tutor joes</h1><p>Learn More Be Smart</p>'
 
@app.route('/about')
def about():
    return '<h3>Tutor Joes Computer Education Salem</h3>' 
 
@app.route('/contact')
def contact():
    return '<h3>Contact Us</h3>' 
 
if __name__ == '__main__':
    app.run(debug=True)
 

In Flask, you can create multiple routes for different URLs by using the @app.route() decorator for each route. Here's an example of how you can create multiple routes for "index" and "about" in Flask:

The index() function is associated with the root URL ("/") and returns an HTML response with a heading <h1>Tutor joes</h1> and a paragraph <p>Learn More Be Smart</p>.


index output

The about() function is associated with the "/about" URL and returns an HTML response with a heading <h3>Tutor Joes Computer Education Salem</h3>.


about output

The contact() function is associated with the "/contact" URL and returns an HTML response with a heading <h3>Contact Us</h3>.


contact output

When you run this Flask application and access the root URL ("/"), "/about" URL, or "/contact" URL in a web browser or through an HTTP request, Flask will call the respective functions (index(), about(), or contact()) and return the HTML responses as defined in those functions.

The if __name__ == '__main__': block ensures that the Flask application runs only when the script is executed directly, and not when it is imported as a module in another script. The debug=True argument in the app.run() function enables the debug mode in Flask, which provides additional debugging information in case of errors. However, it's recommended to disable the debug mode in a production environment for security reasons. You can remove the debug=True argument when deploying your Flask application for production use.