petrus-jvrensburg petrus-jvrensburg - 1 year ago 190
Python Question

URL routing conflicts for static files in Flask dev server

I want to define a url rule with three variable components, like:


But I find that the development server evaluates such rules before trying to match for static files. So anything like:


will be caught by my url rule, rather than being forwarded to the built-in static file handler. Is there a way to force the development server to match for static files first?

P.S. This is only an issue if the rule has more than two variable components.

Answer Source

This is werkzeug route optimization feature. See Map.add, Map.update and Rule.match_compare_key:

def match_compare_key(self):
    """The match compare key for sorting.

    Current implementation:

    1. rules without any arguments come first for performance
    reasons only as we expect them to match faster and some
    common ones usually don't have any arguments (index pages etc.)
    2. The more complex rules come first so the second argument is the
    negative length of the number of weights.
    3. lastly we order by the actual weights.

    return bool(self.arguments), -len(self._weights), self._weights

There are self.arguments - current arguments, self._weights - path depth.

For '/<var_1>/<var_2>/<var3>/' we have (True, -3, [(1, 100), (1, 100), (1, 100)]). There are (1, 100) - default string argument with max length 100.

For '/static/<path:filename>' we have (True, -2, [(0, -6), (1, 200)]). There are (0, 1) - path non argument string length static, (1, 200) - path string argument max length 200.

So I don't find any beautiful way to set own Map implementation for Flask.url_map or set priority for map rule. Solutions:

  1. Setup Flask application as app = Flask(static_path='static', static_url_path='/more/then/your/max/variables/path/depth/static').
  2. Change @app.route('/<var_1>/<var_2>/<var3>/') to @app.route('/prefix/<var_1>/<var_2>/<var3>/').
  3. Add own converter and use as @app.route('/<no_static:var_1>/<var_2>/<var3>/').
  4. Import werkzeug.routing, create own map implementation, change werkzeug.routing.Map to own implementation, import flask.
  5. Use server as on production.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download