Python Forum
Looping Input form / results
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping Input form / results
#1
I am learning Python and PyMongo a bit on the fly.

Here is a basic gist of what I need but I dont know the best way to pursue it. I need Python to create an indefinite loop only escapable by pressing the Ctrl Key. The loop will consist of a slew of input functions that represent a form. the form will use the data to look up entries in PyMongo, return the results, and reload the input right after the results are posted. Here's an example..

Input Object ID: (You input 5555)
*Program locates Object ID 5555 and posts the data then asks you again to Input Object ID and will do so indefinitely until you kill it with the Ctrl key.*

That and ALSO if you were leave the field blank, for it to ask you another input such as Input Name: -- ect...

I already know how to retrieve the MongoDB query (object ID 5555) so dont need much help there, and I also know how to retrieve the items queried, just need it to bring the form back when its done.

Any help would be appreciated.
Reply
#2
Infinite loop is usually done with while True:

Capturing a specific key (as opposed to input terminated by the enter key) is tricky in standard console based Python, and varies from platform to platform.

This will work on many systems, but does not catch ctrl alone (usually a modifier key) but I'm sure there is more to termios that would allow you to address that.

import sys, termios, tty

stdinFileDesc = sys.stdin.fileno() #store stdin's file descriptor
oldStdinTtyAttr = termios.tcgetattr(stdinFileDesc) #save stdin's tty attributes so I can reset it later

try:
    print('Press any key to exit...')
    tty.setraw(stdinFileDesc) #set the input mode of stdin so that it gets added to char by char rather than line by line
    sys.stdin.read(1) #read 1 byte from stdin (indicating that a key has been pressed)
finally:
    termios.tcsetattr(stdinFileDesc, termios.TCSADRAIN, oldStdinTtyAttr) #reset stdin to its normal behavior
    print('Goodbye!')
I am trying to help you, really, even if it doesn't always seem that way
Reply
#3
(Jan-16-2018, 11:36 AM)gruntfutuk Wrote: Infinite loop is usually done with while True:

Capturing a specific key (as opposed to input terminated by the enter key) is tricky in standard console based Python, and varies from platform to platform.

This will work on many systems, but does not catch ctrl alone (usually a modifier key) but I'm sure there is more to termios that would allow you to address that.

import sys, termios, tty

stdinFileDesc = sys.stdin.fileno() #store stdin's file descriptor
oldStdinTtyAttr = termios.tcgetattr(stdinFileDesc) #save stdin's tty attributes so I can reset it later

try:
    print('Press any key to exit...')
    tty.setraw(stdinFileDesc) #set the input mode of stdin so that it gets added to char by char rather than line by line
    sys.stdin.read(1) #read 1 byte from stdin (indicating that a key has been pressed)
finally:
    termios.tcsetattr(stdinFileDesc, termios.TCSADRAIN, oldStdinTtyAttr) #reset stdin to its normal behavior
    print('Goodbye!')

Hi, thank you for the help.

I haven't yet used the sys, termios and tty modules before. I want to see if I understand this first, then I'll ask another question afterward, so for the code you posted it looks like:
stdin is a function of sys
fileno is a function of stdin
and
tcgetattr is a function of termios.

I don't know what the try: or finally: functions are or what they are typically used for but what it looks like you did to me is stored sys.stdin.fileno() to a file descriptor stdinFileDesc and then used this variable inside of the tty.setraw function. I do not yet know exactly what this function is used for either but it looks like tty.setraw adds an additional value to the descriptor stdinFileDesc whenever you press a key. Then sys.stdin.read(1) detects when a byte has been added to it, which happens when you press a key. What I don't quite understand is how it will know to break or kill the program or what can be done to kill it only by pressing ctrl (because I dont want the program to be killed while you might be typing in an input value)

another thing I was wondering about was the while true function, if I were to use that, how would I set it up to take an input and assign it to a variable and then have MongoDB print the results and automatically return to an input function and remain there until the program is broken by pressing control?
Reply
#4
You wouldn't need all this if you change the specs to use Ctrl-C instead of Ctrl
Reply
#5
(Jan-16-2018, 06:07 PM)Gribouillis Wrote: You wouldn't need all this if you change the specs to use Ctrl-C instead of Ctrl

That would work as well, what would need to be done?
Reply
#6
(Jan-16-2018, 09:39 PM)MikeAW2010 Wrote: That would work as well, what would need to be done?
With Ctrl-C to exit, the following (python 3) code suffices
if __name__ == '__main__':
    try:
        while True:
            oid = input('Input Object ID: ')
            print('Here is the data: {}{}{}'.format(oid, oid, oid))
    except KeyboardInterrupt:
        print()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 999 May-21-2023, 08:17 AM
Last Post: snippsat
  Form Results onto Page StewartLFI 3 91,314 Sep-18-2021, 05:00 AM
Last Post: ndc85430
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,245 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  How to append one function1 results to function2 results SriRajesh 5 3,179 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  Trying to send input from html form to database SQLite3 RonnyGiezen 3 71,948 Dec-21-2019, 02:09 PM
Last Post: ibreeden
  Need Help with input from Web form amitjain78 3 2,030 Sep-03-2019, 10:13 AM
Last Post: snippsat
  How to keep looping until the user input is valid ? KyawMyo 12 6,227 Jun-10-2019, 02:51 AM
Last Post: KyawMyo
  [Help] Using "Class" with User Input in an online sign up form vanicci 8 6,687 Aug-29-2018, 10:52 AM
Last Post: vanicci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020