python render_template
First, import the necessary modules for Flask and rendering templates. This can be done with the following code:
from flask import Flask, render_template
Next, create an instance of the Flask class. This instance will be used to handle the incoming requests. You can do this by using the following code:
app = Flask(__name__)
After that, define a route for the desired URL. This is done using the @app.route
decorator. The route specifies the URL path that will trigger the associated function. For example, if you want to render a template for the root URL ("/"), you can do the following:
@app.route('/')
Then, define a function that will be called when the specified URL is accessed. This function will handle the request and return the rendered template. You can use the render_template
function for this. For example, if you want to render a template called "index.html", you can do the following:
def index():
return render_template('index.html')
Finally, run the Flask application. This is done using the app.run
method. For example, you can run the application on the local machine with the following code:
if __name__ == '__main__':
app.run()
This will start the Flask development server and make the application accessible on the specified URL.
Note: Make sure you have the necessary template file(s) in a folder named "templates" in the same directory as your Python script.