Dynamically create flask routes

I am trying to dynamically generate routes in Flask from a list. I want to dynamically generate view functions and endpoints and add them using add_url_rule .

This is what I am trying to do, but I get a "rewrite rewrite" error:

 routes = [ dict(route="/", func="index", page="index"), dict(route="/about", func="about", page="about") ] for route in routes: app.add_url_rule( route["route"], #I believe this is the actual url route["page"], # this is the name used for url_for (from the docs) route["func"] ) app.view_functions[route["func"]] = return render_template("index.html") 
+5
source share
3 answers

You have one problem with two possible solutions. Or:

  • Have route[func] directly the link to the function, not the string. In this case, you do not need to assign anything to app.view_functions .

Or:

  1. Leave the third argument to app.add_url_rule and assign the function app.view_functions[route["page"]] . Code

     return render_template("index.html") 

    not a function. Try something like

     def my_func(): return render_template("index.html") # ... app.view_functions[route["page"]] = my_func 

I would recommend the first option.

Source: documents .


Alternative solution:

Use variable parts in the URL. Something like that:

 @app.route('/<page>') def index(page): if page=='about': return render_template('about.html') # for example else: some_value = do_something_with_page(page) # for example return render_template('index.html', my_param=some_value) 
+4
source

Not too familiar with Flask, so it's possible there is a cleaner way to do this. (If someone who is knowledgeable about Flask thinks that my method is fundamentally wrong, I will be happy to delete my answer if they explain why in the comment.) Now that I have received this disclaimer, here are my thoughts:

app.route("/") is a decorator function. The @ notation is just syntactic sugar for something like index = app.route("/")(index) . Therefore, you should be able to do something like this ...

 routes = [ ("/", index), ("/about", about) ] for route, view_func in routes: view_func = app.route(route)(view_func) 

which allows you to create Flask routes from dynamically created routes and functions.

+4
source

This is how I got it to work with @ this-vidor and @PZP, the get page method requests sqlite db (but it can be any db), the generic def function loops around and in my actual code the list of dictionaries is also extracted from db. So basically I got what I need. Routes are dynamic. I can turn routes on and off in sql without going into app.py to edit them.

 defaultPage = "/" @app.route(defaultPage) def index(): page = getPage(defaultPage) return render_template("index.html", page=page) routes = [ dict(route="/", func="index", page="index"), dict(route="/about", func="about", page="about") ] def generic(): rule = request.url_rule page = getPage(rule) return render_template('index.html', page=page) for route in routes: app.add_url_rule( route["route"], #I believe this is the actual url route["page"] # this is the name used for url_for (from the docs) ) app.view_functions[route["func"]] = generic` 
0
source

All Articles