flask return html

Certainly! Here's an example of how to use Flask to return HTML:

  1. Import Flask: Import the Flask class from the flask module.

python from flask import Flask

  1. Create an instance: Create an instance of the Flask class.

python app = Flask(__name__)

  1. Define a route: Use the @app.route() decorator to define a route. This associates a URL with a function that will be called when the URL is visited.

python @app.route('/') def index(): return 'Hello, this is the homepage!'

  1. Create an HTML route: Define another route for returning HTML content.

python @app.route('/html') def html_page(): return ''' <html> <head> <title>Flask HTML Example</title> </head> <body> <h1>Hello, this is an HTML page!</h1> </body> </html> '''

  1. Run the application: Use app.run() to start the Flask development server.

python if __name__ == '__main__': app.run(debug=True)

Explanation: This code sets up a basic Flask application with two routes. The first route, '/', is the root route and returns a simple text message when visited. The second route, '/html', returns an HTML page when visited, created as a multi-line string in Python. When the script is executed, the Flask development server starts, allowing you to visit these routes in a web browser.