473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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'])
Jul 21 '21 #1
0 4220

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

Similar topics

2
by: Bartosz Wegrzyn | last post by:
I use onblue event to validate fields in my form. I do this onblur="return isname()" and so so ... I have one form with 20 fields. My problem is that when the focus is for example on the...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
1
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
33
by: dmoran21 | last post by:
Hi all, I am a mathematician and I'm trying to write a program to try out a formula that I've derived. However, it seems that I've got an infinite loop and I don't quite understand why. I was...
19
by: Richard | last post by:
Hi All, I copied a script example from http://www.irt.org/script/640.htm into a local .html file. I opened that file first in HTML-kit, which hung (in an infinite loop, I think) when I...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
by: ahmadbaseet | last post by:
Hello all I am new to VB.NET. I am trying to create dynamically objects and giving them the event handles. Something like this Dim NewTab As New TabPage Dim NewTextBox As New TextBox Dim...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.