<div dir="ltr">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...<br><br>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...<div><br></div><div><span style="font-size:12.8px">@app.route("/func/<required_param>", defaults={"opt1": None, "opt2": None, "opt3": None})</span><br style="font-size:12.8px"></div><div><span style="font-size:12.8px">@app.route("/func/<required_param>/opt1/<opt1>", </span><span style="font-size:12.8px">defaults={"opt2": None, "opt3": None})</span></div><div><span style="font-size:12.8px">@app.route("/func/<required_param>/opt1/<opt1>/opt2/<opt2>", </span><span style="font-size:12.8px">defaults={"opt3": None})</span><span style="font-size:12.8px"><br></span></div><div><br></div><div><span style="font-size:12.8px">and so on....</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">doing this for a half dozen optional path parameters sounds messy.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">If you want to parse option query parameters you should use request.args.get('param_name')</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">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:</span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">@app.route("/func/<required_param>")</span></div><div><span style="font-size:12.8px">def my_func(required_param):</span></div><div><span style="font-size:12.8px">    func_you_have(required_param,</span></div><div><span style="font-size:12.8px">                              opt1=request.args.get('opt1'),</span></div><div><span style="font-size:12.8px">                              opt2=request.args.get('opt2'),</span></div><div><span style="font-size:12.8px">                              ...)</span></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Apr 12, 2017 at 1:02 PM, Skip Montanaro <span dir="ltr"><<a href="mailto:skip.montanaro@gmail.com" target="_blank">skip.montanaro@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I'm trying to create a Flask endpoint which corresponds to an existing<br>
function which takes a single required arg and potentially a half<br>
dozen or so optional args. (I know, crazy. Still, it's what I have to<br>
work with.) What's the idiomatic approach to this?<br>
<br>
Let's simplify the problem with a silly hypothetical concrete example.<br>
Suppose I have this function:<br>
<br>
def func(param, opt1=None, opt2=None, opt3=None):<br>
    return (param, opt1, opt2, opt3)<br>
<br>
Any subset of the optN parameters might be given (that is, the<br>
presence of opt2 doesn't imply the presence of opt1). What are the<br>
best app.route() decorator calls for this function? When none are<br>
given, I clearly can have<br>
<br>
@app.route("/func/param")<br>
<br>
Do I give a defaults dictionary for the other params, like so?<br>
<br>
@app.route("/func/param", defaults={"opt1": None, "opt2": None, "opt3": None})<br>
<br>
With that signature, can I use traditional URL parameter notation to<br>
specify the optional parameters? My initial experiment didn't succeed,<br>
so I've failed so far to provide an existence proof of that solution,<br>
and my perusal of the documentation has so far not turned up any<br>
examples of "?...&...&..." notation.<br>
<br>
Thx,<br>
<br>
Skip Montanaro<br>
______________________________<wbr>_________________<br>
Flask mailing list<br>
<a href="mailto:Flask@python.org">Flask@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/flask" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/flask</a><br>
</blockquote></div><br></div>