[Flask] A bit confused about route decorator and optional parameters

Skip Montanaro skip.montanaro at gmail.com
Wed Apr 12 14:02:58 EDT 2017


I'm trying to create a Flask endpoint which corresponds to an existing
function which takes a single required arg and potentially a half
dozen or so optional args. (I know, crazy. Still, it's what I have to
work with.) What's the idiomatic approach to this?

Let's simplify the problem with a silly hypothetical concrete example.
Suppose I have this function:

def func(param, opt1=None, opt2=None, opt3=None):
    return (param, opt1, opt2, opt3)

Any subset of the optN parameters might be given (that is, the
presence of opt2 doesn't imply the presence of opt1). What are the
best app.route() decorator calls for this function? When none are
given, I clearly can have

@app.route("/func/param")

Do I give a defaults dictionary for the other params, like so?

@app.route("/func/param", defaults={"opt1": None, "opt2": None, "opt3": None})

With that signature, can I use traditional URL parameter notation to
specify the optional parameters? My initial experiment didn't succeed,
so I've failed so far to provide an existence proof of that solution,
and my perusal of the documentation has so far not turned up any
examples of "?...&...&..." notation.

Thx,

Skip Montanaro


More information about the Flask mailing list