473,666 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing flask textbox value to an infinite while loop

1 New Member
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.for m['var'])
Jul 21 '21 #1
0 4248

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

Similar topics

2
13045
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 first field of my form the "name" field and I click somewher or press tab than I loose focus and my isname() function is executed. Everything is fine but the focus was change for next field
43
5575
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 fallacy built on the assumption of mythical infinite all powerfull machines. In reality we deal with finite machines that are capable of two states in a loop, they either terminate, or repeat themselves. In the mythical halting problem scenario...
5
4931
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
5209
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 peer. I have no problem connecting the peers and streaming Network Streams. When the incoming data is finished recieving, I act upon it. This works great as long as all of the code is inside my form. I want to build the networking code into a...
33
3044
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 hoping someone could point me in the right direction. Code: #include <stdio.h> #include <math.h>
19
2343
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 previewed the example. On the off chance that example exposed a weakness in HTML-Kit, I ran the example in IE7, which also hung. The example seems to start an infinite loop which the user should be
44
4320
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
2132
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 NewTab2 As New TabPage Dim b As New Button AddHandler b.Click, AddressOf b1
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.