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:
This is the .html code:
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...
...in the while loop, because it's first reading var = 0 and then var = int(request.form['var'])
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)
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>
Code:
var = 0
var = 3
var = 0
var = 3
var = 0
var = 3
var = 0
var = 3