Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

A guest post by Aaron Kavlie, a developer at Stremor working on a mobile voice platform called Plexi.

The routing system in Flask is very simple and straightforward compared to regex-based systems, such as the one employed by Django. It would seem that this ease of use comes at the cost of a lot of flexibility in defining more complex routes. There are a few nuances, however, that aren’t covered in the Flask docs that were very useful for a recent project I worked on.

Specificity Wins

Let’s say you want a variable route like the following:

But you also want hardcoded paths to work:

It turns out that this works just fine. /search/ resolves to the search view, and /[anything else]/ (assuming these are the only two views in the app) resolves to the state_index view. All other things being equal, a hardcoded route segment will win over a variable route segment. More specific trumps less specific.

Length Restrictions

I also wanted both of these to work for my project:

The entity_index view is expecting cities or zips as a value for <entity_type>, as logic and templating is mostly the same for both cases. But remember, specificity wins. So /zips/az/ would route to the zip view, just as well as /zips/85123/. That’s not what we want.

It turns out that routes accept length parameters. State abbreviations and zip codes are always fixed-length variables, so this does the trick:

The length parameter is not mentioned anywhere in the Flask docs, but you can find it (along with related parameters like minlength and maxlength) in the Werkzeug routing docs.

Order Doesn’t Matter

Unlike some other URL routing systems (Django and App Engine’s app.yaml come to mind), registration order plays no part in resolving URLs with Flask. In the specificity example, the URL /search/ (with or without the trailing slash) will route to the search view handler, whether it’s registered before or after /<entity_type>/ in the source code.

See below for Flask resources from Safari Books Online.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Instant Flask Web Development takes you beyond the quick start in the Flask documentation and shows you a bottom-up approach to organizing your Flask application. It shows you how to build a small deployable scheduling application with pointers to the various design decisions you can make when developing with Flask.
Instant Data Intensive Apps with pandas How-to starts with Pandas’ functionalities such as joining datasets, cleaning data, and other data munging tasks. It quickly moves onto building a data reporting tool, which consists of analysis in Pandas to determine what’s relevant and present that relevant data in an easy-to-consume manner, and also provides a section on Flask.

About the author

aaron Aaron Kavlie started doing web development a few years ago and is currently a developer at Stremor working on a mobile voice platform called Plexi. He mainly does backend work with Python these days, but has been known to do frontend web development in the past. He can be reached at @akavlie.

Tags: Django, Flask, Routing, specificity, tips,

Comments are closed.