468,741 Members | 1,581 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,741 developers. It's quick & easy.

Passing flask textbox value to an infinite while loop

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:

Expand|Select|Wrap|Line Numbers
  1. from flask import Flask, render_template, request
  2. import thread
  3. import time
  4.  
  5. app = Flask(__name__)
  6. def main():
  7.  
  8.     while True:
  9.         time.sleep(5)
  10.         print "var = "+str(var)
  11.  
  12. @app.route("/")
  13. def index():
  14.  
  15.     templateData = {
  16.         'var': var 
  17.  
  18.     }
  19.     return render_template('index.html', **templateData)
  20.  
  21. @app.route('/', methods=['POST'])
  22. def post():
  23.  
  24.     global var 
  25.     var = 0
  26.     var = int(request.form['var'])
  27.     return str("var: " + str(var))
  28.  
  29. if __name__ == "__main__":
  30.     thread.start_new_thread(main, ()) 
  31.     thread.start_new_thread(post, ()) 
  32.     app.run(host='0.0.0.0', port=8083, debug=True)
  33.  
This is the .html code:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2.    <head>
  3.       <title>POST</title>
  4.    </head>
  5.    <body>
  6.     <form method="POST">
  7.         var:<br>
  8.         <input name="var">   
  9.         <br><br>
  10.         Current:    {{ var  }} <br><br>
  11.         <input type="submit">
  12.     </form>
  13.    </body>
  14. </html>
  15.  
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...

Expand|Select|Wrap|Line Numbers
  1. var = 0
  2. var = 3
  3. var = 0
  4. var = 3
  5. var = 0
  6. var = 3
  7. var = 0
  8. var = 3
  9.  
...in the while loop, because it's first reading var = 0 and then var = int(request.form['var'])
1 Week Ago #1
0 1159

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Bartosz Wegrzyn | last post: by
43 posts views Thread by Gremlin | last post: by
5 posts views Thread by mailpitches | last post: by
33 posts views Thread by dmoran21 | last post: by
44 posts views Thread by James Watt | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.