1

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.

3 Answers 3

2

No, this doesn't work and it's not at all the recommended way to do things in a backend.

A better way to do this is to use a single monum function and use a parameter to define the content of the webpage you want to display.

For example:

from flask import request

...

@app.route('/monum')
def monum():
    name_monum = request.args.get('name_monum')
    adress_monum = test[nom_monum]
    return "Nom: {} --- Adresse: {}".format(name_monum, adress_monum)

(And remember to handle errors)

Sign up to request clarification or add additional context in comments.

Comments

0

This doesn't directly answer your question, but you might consider using a variable in the url instead.

@app.route('/monum/<page>')
def index(page):
  if page in monum:
      return "Nom: {} --- Adresse: {}".format(page, monum[page])
  # raise a 404 or something

Comments

0

I can't comment so I am adding my reply as an answer;

Are you calling the function anywhere? Is "@app.route('/monum'+str(i)+'')" supposed to do it? If so, should it not be "@app.route('/monum_'+str(i)+'')"? Otherwise, it might be easier to apply a "Nom: {} --- Adresse: {}".format(monum[i], test[monum[i]]) into a for loop so you don't create a lot of functions that you'd use only once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.