flask enumerate index
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
items = ['item1', 'item2', 'item3']
return render_template('index.html', items=items)
if __name__ == '__main__':
app.run(debug=True)
Import the necessary modules from the Flask framework:
Flask
andrender_template
.Create a Flask web application instance named
app
.Define a route for the root URL
'/'
using the@app.route
decorator. This route corresponds to the default endpoint of the web application.Define a function named
index()
to handle requests to the root URL. Inside this function: a. Create a list calleditems
containing some example items. b. Userender_template
to render an HTML template named'index.html'
. Pass theitems
list to the template as a variable nameditems
.If the script is executed directly (not imported as a module), start the Flask development server with
app.run(debug=True)
. Thedebug=True
option enables debug mode, providing additional information in case of errors.