I am building a website that will accept user input in 7 fields, one of them is a list of stocks (can be up to 100). One of the fields the user should input is a target of annual rate of return.
The issue I am having is that if the target of annual rate of return failed (it is larger than the max return) I am flashing the user with a message, and redirecting to the form, but I lose all pre entered input feilds the user submitted.
What I would like to do is to keep all the input the user submitted and flash the message under the "target of annual rate of return" field in the form.
What I have so far is on HTML:
<h3> Please enter target annual rate of return</h3>
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="return" placeholder="rate of return" type="number">%
</div>
<button onClick="this.form.submit(); this.disabled=true; this.value='Sending…'; " class="btn btn-primary" type="submit">Build</button>
on the app.py I have:
ef = EfficientFrontier(mu, S, weight_bounds=(None, None))
ef.add_objective(objective_functions.L2_reg)
try:
ef.efficient_return(target_return=(float(request.form.get("return"))/100), market_neutral=True)
except pypfopt.exceptions.OptimizationError:
flash('Target annual rate of return is too high for this portfolio, please consider using a lower rate')
return redirect("/build")
so hopefully I will have the flash appearing and the form input intact when except "pypfopt.exceptions.OptimizationError:" is triggered.
Thanks!