Emmett uses the @app.route() decorator for routing. You can include variable parts in a URL using the <type:variable_name> syntax. These variables are passed as keyword arguments to the decorated function.
Supported variable types:
int: integersfloat: floats in dot notationstr: stringsdate: date strings (YYYY-MM-DD)alpha: strings containing only literalsany: any path (including slashes)
If a URL does not match the specified type (e.g., providing a string for an int rule), Emmett returns a 404 error.
To make a part of the URL optional, wrap it in parentheses and append a question mark: "/path(/<type:var>)?". If the optional part is missing, the corresponding function parameter will be None.
@app.route('/user/<str:username>')
async def user(username):
return "Hello %s" % username
@app.route('/double/<int:number>')
async def double(number):
return "%d * 2 = %d" % (number, number*2)
@app.route("/profile(/<int:user_id>)?")
async def profile(user_id):
if user_id:
# get requested user
else:
# load current logged user profile