Passing flask textbox value to an infinite while loop

Joined
Jul 21, 2021
Messages
1
Reaction score
0
I'm running a flask server, with a textbox input and a while loop (in a different thread). I want to pass the input value from the textbox to the while loop.

This is the .py code:
Python:
from flask import Flask, render_template, request
import thread
import time

app = Flask(__name__)
def main():
   
    while True:
        time.sleep(5)
        print "var = "+str(var)

@app.route("/")
def index():
   
    templateData = {
        'var': var

    }
    return render_template('index.html', **templateData)

@app.route('/', methods=['POST'])
def post():
   
    global var
    var = 0
    var = int(request.form['var'])
    return str("var: " + str(var))
   
if __name__ == "__main__":
    thread.start_new_thread(main, ())
    thread.start_new_thread(post, ())
    app.run(host='0.0.0.0', port=8083, debug=True)
This is the .html code:
HTML:
<!DOCTYPE html>
   <head>
      <title>POST</title>
   </head>
   <body>
    <form method="POST">
        var:<br>
        <input name="var">  
        <br><br>
        Current:    {{ var  }} <br><br>
        <input type="submit">
    </form>
   </body>
</html>
I don't know what is the proper way to get the value to the while loop, because I'm getting mixed signals in the loop. If I for example write 3 in the textbox I'll get...
Code:
var = 0
var = 3
var = 0
var = 3
var = 0
var = 3
var = 0
var = 3
...in the while loop, because it's first reading var = 0 and then var = int(request.form['var'])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top