Issue
New to programming and currently working on a web scraper. I'm trying to monitor my progress and keep my terminal clean as I go using clear_output() from IPython.display, but it doesn't appear to be clearing my output in VSCode. Here's an example:
from time import sleep
from IPython.display import clear_output
x = 0
while x < 5:
x += 1
sleep(.2)
print(x)
clear_output(wait=True)
I'd expect it to clear the previous value of x in place of the new value, but it doesn't. Once the program finishes, my output changes from:
>>>1
>>>2
>>>3
>>>4
>>>5
to:
>>>1
>>>2[2K
>>>3[2K
>>>4[2K
>>>5[2K
Guessing this is something from IPython, but I'm not sure what it means or how to fix it. Thanks for any help!
Solution
I'm not exactly sure if this is what you're asking but you could try using the sys library:
from time import sleep
from sys import stdout
for i in range(0, 1000):
stdout.write("\b") # \b for backspace, you may need \r depends on system
sleep(.9)
stdout.write("\b\b\b>>>" + str(i))
stdout.flush()
for me the output is:
>>>1
then the 1 is replaced by a 2
>>>2
2 replaced by 3 etc.
I threw in a sleep so you can see what happens, as stated in the comment you may need to change the \b to a \r or some combination of both
Answered By - BpY
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.