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

Harrison Wright wright8191 at gmail.com
Wed Apr 12 14:22:48 EDT 2017


I'm not certain of an idiomatic way, it depends on what you want your API
design to me. Based on your question it is unclear to me if you want path
parameters or query parameters, but...

If you would like to specify optional path parameters, you need to define
an app route with those parameters. Remember that you can define multiple
routes to a single function. Such as...

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

and so on....

doing this for a half dozen optional path parameters sounds messy.

If you want to parse option query parameters you should use
request.args.get('param_name')

I would suggest creating a route function that simple serves as a wrapper
for the function that is already defined, and use optional query parameters
(or a mix of query/path parameters where it makes sense). It's hard to say
for sure without the semantics of the function and arguments. Something
like:

@app.route("/func/<required_param>")
def my_func(required_param):
    func_you_have(required_param,
                              opt1=request.args.get('opt1'),
                              opt2=request.args.get('opt2'),
                              ...)


On Wed, Apr 12, 2017 at 1:02 PM, Skip Montanaro <skip.montanaro at gmail.com>
wrote:

> 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
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20170412/bf333485/attachment.html>


More information about the Flask mailing list