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.
:internal:
"""
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:
- Setup
Flask
application as app = Flask(static_path='static', static_url_path='/more/then/your/max/variables/path/depth/static')
.
- Change
@app.route('/<var_1>/<var_2>/<var3>/')
to @app.route('/prefix/<var_1>/<var_2>/<var3>/')
.
- Add own converter and use as
@app.route('/<no_static:var_1>/<var_2>/<var3>/')
.
- Import
werkzeug.routing
, create own map implementation, change werkzeug.routing.Map
to own implementation, import flask
.
- Use server as on production.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…