Issue
I have a html form which I am using a threshold for a python function. I managed to get the data as expected, but when I try to use the input for number it returns a string instead a float/number. I have been researching when I could use JS to do the conversion, still nothing really concrete found.
<form method="Post", id="myform">
<input type="number" id="threshold" name="threshold" min=".1" max="100">
<input type="submit" value="Submit">
</form>
app.route("/dp", methods=['GET', 'POST'])
def report():
v = request.form.get("threshold")
if request.method=='POST':
//code
Solution
Flask's form parser returns all data in string format regardless of how the input field itself is defined in your HTML; cast it to the datatype you need on the back-end in your Flask route:
@app.route("/dp", methods=['GET', 'POST'])
def report():
v = float(request.form.get("threshold"))
if request.method=='POST':
//code
See How do I parse a string to a float or int? for further discussion on casting a string to int
and float
datatypes.
Answered By - esqew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.