Navigation

Flask is usually used with the decorators. Decorators are simple and you
have the URL right next to the function that is called for that specific
URL. However there is a downside to this approach: it means all your code
that uses decorators has to be imported upfront or Flask will never
actually find your function.

This can be a problem if your application has to import quick. It might
have to do that on systems like Google’s App Engine or other systems. So
if you suddenly notice that your application outgrows this approach you
can fall back to a centralized URL mapping.

The system that enables having a central URL map is the
add_url_rule() function. Instead of using decorators,
you have a file that sets up the application with all URLs.

So far we only split up the views and the routing, but the module is still
loaded upfront. The trick is to actually load the view function as needed.
This can be accomplished with a helper class that behaves just like a
function but internally imports the real function on first use:

What’s important here is is that __module__ and __name__ are properly
set. This is used by Flask internally to figure out how to name the
URL rules in case you don’t provide a name for the rule yourself.

Then you can define your central place to combine the views like this:

You can further optimize this in terms of amount of keystrokes needed to
write this by having a function that calls into
add_url_rule() by prefixing a string with the project
name and a dot, and by wrapping view_func in a LazyView as needed.

defurl(import_name,url_rules=[],**options):view=LazyView('yourapplication.'+import_name)forurl_ruleinurl_rules:app.add_url_rule(url_rule,view_func=view,**options)# add a single route to the index viewurl('views.index',['/'])# add two routes to a single function endpointurl_rules=['/user/','/user/<username>']url('views.user',url_rules)

One thing to keep in mind is that before and after request handlers have
to be in a file that is imported upfront to work properly on the first
request. The same goes for any kind of remaining decorator.