Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent Spyder from inserting a new line after taking user input?

Spyder inserts a blank line in the console output between every call of the input() function but I don't want that (i.e. I want the input() prompts to be on contiguous lines in the console instead of separated by a blank line). Is there a way to do that? I tried using input("foo", end="") thinking it may work like the print() function but that's not the case...

Code:

fname = input("Please enter your first name: ")
lname = input("Please enter your last name: ")
print("Pleased to meet you, " + str(fname) + " " + str(lname) + "!")

Output:

Please enter your first name: Jane

Please enter your last name: Doe
Pleased to meet you, Jane Doe!

Desired output:

Please enter your first name: Jane
Please enter your last name: Doe
Pleased to meet you, Jane Doe!

Edit:

As others have pointed out in the comments section, this issue is non-reproducible, even for me, except through the use of the IPython interface within the Spyder IDE. If anyone is running IPython outside of Spyder, please run the above code and let me know whether that produces the same output. I can reproduce the undesired output through Spyder's IPython interface but not through a Terminal session so this is something specific to either IPython or Spyder.

like image 634
paanvaannd Avatar asked Oct 30 '22 06:10

paanvaannd


2 Answers

(Spyder developer here) This looks like a minor bug in our IPython console. Please report it here:

https://github.com/jupyter/qtconsole

Note: This console is not simply embedding a terminal IPython session in Spyder (that's why making comparisons to it make no sense at all).

Instead, it is a re-implementation of most of the terminal behavior but using a graphical toolkit (called Qt) and the Jupyter kernel/frontend architecture.

like image 119
Carlos Cordoba Avatar answered Nov 15 '22 05:11

Carlos Cordoba


Perhaps not exactly what you're after, but it should solve your problem.

Delete previous line in console with:

def delete_previous_line():
    CURSOR_UP_ONE = '\x1b[1A'
    ERASE_LINE = '\x1b[2K'
    print(CURSOR_UP_ONE + ERASE_LINE + CURSOR_UP_ONE)


fname = input("Please enter your first name: ")
delete_previous_line()
lname = input("Please enter your last name: ")
print("Pleased to meet you, " + str(fname) + " " + str(lname) + "!")

See remote last STDOUT.

If it doesn't work try

print(CURSOR_UP_ONE + ERASE_LINE)

instead of

print(CURSOR_UP_ONE + ERASE_LINE + CURSOR_UP_ONE)
like image 42
ChickenFeet Avatar answered Nov 15 '22 05:11

ChickenFeet