Convert html input type number to actual number from html

Question:

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
Asked By: ReinholdN

||

Answers:

In JS you can try parseInt. You can see the examples at :
https://www.geeksforgeeks.org/convert-a-string-to-an-integer-in-javascript/

Answered By: SoSen

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
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.