Javascript to python data is not sent i am using flask and unable to convert

Question:

(comment how can i improve way to present the question as well)
I am making a project for typing speed web application using Flask. But the problem is in calculating the time it takes for user to type. I’ve tried to calc time using page response time using @app.before_request and after… but it didn’t showed time.
So i applied JS(javascript) function to calculate time(hidden input from js[embeded] to html) and return to my python code along with the input data and calculated the time difference.
BUT there a value error
File "c:—-DesktopFlaskmain.py", line 32, in typefast
in typefast
time_taken = end_time – float(start_time)
ValueError: could not convert string to float: ‘start_time’

The functional code is as follow:

{%block basic%}
<form method = 'Post' action = '/result'>
    <label for="text">Type the text below:</label><br>
    <textarea name="text" rows="10" cols="50">monster Speed</textarea>
    <input type="hidden" name="start_time" value="" id="start_time">
    <input type="submit" value="Submit">
</form>
{% endblock %}

{%block scripts%}
    <script>
        // curr time
        
**var start_time = String(new Date().getTime());
document.getElementById("start_time").value = start_time;**
    
    </script>
    {%endblock%}
@app.route('/result',methods = ['Get','Post'])
def typefast() -> 'html': 
    # request.parameter_storage_class = ImmutableOrderedMultiDict
    form_dta = request.form['start_time']
    #start_time = next(form_dta)
    start_time = start_time.strip(''')
    
    end_time = time.time()
    text = request.form['text']
    time_taken = end_time - float(start_time)
    words_typed = len(text.split())
    typing_speed = words_typed / (time_taken/60)
    return render_template('results.html',  the_title='It's Good to see your work!', typing_speed=form_dta)

The value of start time as concluded by python is > symbol.
I’ve tried to strip the string** but didn’t worked either. Other methods suggestions are also accepted for calculating time taken from input to submit. Also referred to related questions like html to flask method =Post but didn’t got the desired output.
Also i tried json format but couldn’t get it right either.
Thanks for your answer.

Asked By: Gautam A20

||

Answers:

The issue was with that the js code is not included in the main code.
The correcct procedure should be:

{%block basic%}
<form method = 'Post' action = '/result'>
        <label for="text">Type the text below:</label><br>
        <textarea name="text" rows="10" cols="50">monster Speed</textarea>
        <input type="hidden" name="start_time" value="" id="start_time">
        <input type="submit" value="Submit">
</form>
<script>
            // curr time
            
    **var start_time = String(new Date().getTime());
    document.getElementById("start_time").value = start_time;**
        
</script>
{% endblock %}
Answered By: Gautam A20
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.