Skip to content

Instantly share code, notes, and snippets.

@alOneh
Created June 19, 2012 10:10
Show Gist options
  • Save alOneh/2953365 to your computer and use it in GitHub Desktop.
Save alOneh/2953365 to your computer and use it in GitHub Desktop.
RegexConverter for Flask app
# source http://stackoverflow.com/questions/5870188/does-flask-support-regular-expressions-in-its-url-routing
from flask import Flask
from werkzeug.routing import BaseConverter
app = Flask(__name__)
class RegexConverter(BaseConverter):
def __init__(self, url_map, *items):
super(RegexConverter, self).__init__(url_map)
self.regex = items[0]
app.url_map.converters['regex'] = RegexConverter
@app.route('/<regex("[abcABC0-9]{4,6}"):uid>-<slug>/')
def example(uid, slug):
return "uid: %s, slug: %s" % (uid, slug)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment