473,289 Members | 1,963 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,289 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 4213

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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.