-2

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
davidism
  • 121,510
  • 29
  • 395
  • 339
ReinholdN
  • 526
  • 5
  • 22

1 Answers1

1

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.

esqew
  • 42,425
  • 27
  • 92
  • 132