Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backspace (\b) and carriage return (\r) characters are not printed correctly in the console #195

Closed
spyder-bot opened this issue Feb 16, 2015 · 15 comments

Comments

@spyder-bot
Copy link
Collaborator

From code3rea...@gmail.com on 2010-04-10T13:59:33Z

What steps will reproduce the problem?

1.type: print("Yes\bno") 2. 3. What is the expected output? What do you see instead? expected: yeno
observed: yes[checkmarkcharacter]no What version of the product are you using? On what operating system? 1.1.0beta1

Please provide any additional information below

. seen screenshot

Attachment: spyder_backspace.png

Original issue: http://code.google.com/p/spyderlib/issues/detail?id=195

@spyder-bot
Copy link
Collaborator Author

From christop...@gmail.com on 2010-09-15T14:38:19Z

Tested again on spyder 1.1.6. Using the example I get 'yesno' as output. Backspace is not printed.

@spyder-bot
Copy link
Collaborator Author

From ccordoba12 on 2011-04-15T07:00:29Z

I can confirm that this still happens in Spyder 2.0, but now I get "Yes°no"

Summary: Backspace character is printed incorrectly in the console
Status: Accepted
Labels: -Priority-Medium Priority-Low

@spyder-bot
Copy link
Collaborator Author

From ccordoba12 on 2011-05-15T08:05:07Z

I've been investigating this issue and it turns out it's not an encoding problem or anything related. To the best of my understanding, to print correctly these characters you have to be working on a proper terminal (bash or cmd.exe) and not inside a GUI.

I've tried to print them on the python interpreters I have at hand (ipython-qtconsole, wing-ide, Eric, IDLE and ipython-notebook) and I can confirm the same problem in all of them. It also occurs in environments not based on Python, for example in Eclipse: https://bugs.eclipse.org/bugs/show_bug.cgi?id=76936 This issue could be solved by emulating the terminal behavior inside Spyder. We could check if the user wants to print a string that contains these characters and preparse the input before sending it to the console, so that the result be the expected.

But for that to happen I would like to hear some good reasons of those who reported the problem, to see if it's really worth the effort to implement this feature.

Summary: Backspace (\b) and carriage return (\r) characters are not printed correctly in the console

@spyder-bot
Copy link
Collaborator Author

From ccordoba12 on 2011-05-15T08:06:23Z

issue #645 has been merged into this issue.

@spyder-bot
Copy link
Collaborator Author

From ccordoba12 on 2011-05-15T19:21:05Z

Labels: Cat-Console

@spyder-bot
Copy link
Collaborator Author

From ays...@gmail.com on 2012-10-08T08:16:45Z

Terminal-like support for \b and \r characters provides a simple way to make animations in standard out (using sys.stdout.write() ), such as a progress bar for a download (think wget). In such a use, it is not sufficient to only modify isolated strings that contains these characters, but you also need to remember where the cursor is so that a subsequent call to sys.stdout.write() starts at the intended point.

@spyder-bot
Copy link
Collaborator Author

From ccordoba12 on 2012-10-08T10:27:39Z

I think this was fixed for the IPython qtconsole some time ago. I don't think we are going to fix this for our Python console, because it seems to much trouble for what you can get.

But if someone wants to help us with this and send a patch, then we'll be happy to review it and add it to our tree.

@spyder-bot
Copy link
Collaborator Author

From bzac...@gmail.com on 2014-02-18T14:29:01Z

I think its worth the effort.
The scientific use of python often involves running slow code, and without a good progress bar it is hard to know if your code is running (or going to return in finite time).
Could you reconsider?

@spyder-bot
Copy link
Collaborator Author

From ccordoba12 on 2014-02-19T12:49:26Z

Please send us a pull request if you consider it important enough or just use one of our IPython consoles (which I know for sure have this functionality implemented).

Status: HelpNeeded

@spyder-bot
Copy link
Collaborator Author

From ccordoba12 on 2014-12-18T08:45:14Z

Status: Accepted
Labels: MS-v2.4

@spyder-bot
Copy link
Collaborator Author

From ccordoba12 on 2014-12-18T09:01:48Z

This issue was closed by revision ce3d4d0af8a5

Status: Fixed

@jnettels
Copy link

Hello there, this issue has been closed years ago, but it still exists for me. I was wondering if I should create a new issue, but I give it a try here.
Versions: Spyder 3.3.3 | Python 3.7.1 64-bit | Qt 5.9.6 | PyQt5 5.9.2 | Windows 10

Example code:

import time

for i in range(100):
    print(i, end='\r')  # OK
print()

for i in range(1000):
    print(i, end='\r')  # OK
print()

for i in range(10000):
    print(i, end='\r')  # Fail
print()

for i in range(10):
    time.sleep(0.2)
    print(i, end='\r')  # Fail

Output in external system terminal (expected output):

99
999
9999
9

Output in IPython console in Spyder:

99
999
9999280858079806
9134678

Printing progress information in my loops is important to me, and therefore I cannot use the Spyder console for most of my scripts. Why can't I find more complaints about this? Am I missing something?

Thanks to all contributers for their work & regards!

@ccordoba12
Copy link
Member

@jnettels, this was fixed for our (old and now removed) Python console. However, this is still an issue in our IPython console.

The problem was reported alredy in jupyter/qtconsole#272. If you have time, please take a look at qtconsole's code to see if you can help us to fix it (it's low priority for us right now).

@combobulate
Copy link

combobulate commented Jul 29, 2019

Don't know if it's helpful for troubleshooting, but I was able to get the desired \r behavior in the Spyder console by using \r in both the object and end arguments for print.

This produces desired overwriting of the output:

import time
 
for n in range(1, 11):
    print('\r{}'.format(n), end='\r')
    time.sleep(.5)

This incorrectly produces the numbers 1 through 10 each on a new line, without overwriting:

import time
 
for n in range(1, 11):
    print('\r{}'.format(n))
    time.sleep(.5)

This incorrectly produces the numbers 1 through 10 all on the same line, without overwriting:

import time
 
for n in range(1, 11):
    print('{}'.format(n), end='\r')
    time.sleep(.5)

@FabienFellay
Copy link

What I noticed so far:

This is actually normal that the following produces new lines (this is also the case with a regular terminal with Ipython):

import time

for n in range(1, 11):
    print('\r{}'.format(n))
    time.sleep(.5)

This is because the default end argument of the built-in print() function is set to '\n'.

Thus, the best workaround so far is the following (working identically as expected in the Spyder Ipython console and a regular terminal Ipython console):

import time

for n in range(1, 11):
    print('\r{}'.format(n), end='')
    time.sleep(.5)

It seems that \r is not taken into account by the next print() when it is placed at the end of the previous print() in the Spyder console (assuming that the print() function is used correctly and therefore that it issues no new lines). However, when \r is placed at the beginning of a print(), it is taken into account correctly in the Spyder console.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants