I'm currently working with the flask framework and I need to be able to create from a dictionary a web page for each element of this dictionary.
So I started with the idea of creating these pages through a for loop that will run through the dictionary:
from flask import Flask
app = Flask(__name__)
test={}
test["Louvre_Museum"]= "Rue de Rivoli, 75001 Paris"
test["Eiffel_Tower"]="Champ de Mars, 5 Avenue Anatole France, 75007 Paris"
test["Triumphal_Arch"]="Place Charles de Gaulle, 75008 Paris"
monum = []
for cle in test.keys():
monum.append(cle)
@app.route('/')
def index():
return "HOMEPAGE TEST !"
for i in range(len(monum)):
@app.route('/monum'+str(i)+'')
def monum_i():
name_monum = monum[i]
adress_monum = test[nom_monum]
return "Nom: {} --- Adresse: {}".format(name_monum, adress_monum)
if __name__ == '__main__':
app.run()
The program works correctly until it reaches the definition of the functions.
I wanted to create different functions in the loop called: "monum_0, monum_1, monum_2 etc...) which doesn't work.
For python "monum_i" is just a string and there is only one function that is created which has this name.
And this is a problem for the second pass in the loop where the program defines a function that already exists and so I have an error.
I would therefore like to know whether it is possible to define functions in an automated way in the same way as my example.