Issue
I want to print only two lines in my cycles, I started with the following:
import time
for i in range(10):
time.sleep(0.5)
for j in range(10):
time.sleep(0.5)
print('Index1:', i)
print('Index2:', j)
I tried different combinations of \r
, \n
, flush=True
etc. at the beginning and end of print
, but wasn't successful((
What I'd like to see is as in the GIF of accepted answer here
I tried those code, but it didn't help, maybe because my OS is Windows or I use jupyter notebook
, I have no idea((
Solution
In Jupyter Notebook, you can clear current cell output using IPython.display.clear_output(wait=True)
In terminal, similiar behaviour can be achieved using ANSI escape codes, as in the following snippet:
import time
n = 15
for i in range(n):
print(f"{i}s passed")
print(f"{n-i}s remaining")
time.sleep(1)
print("\u001b[2A", end="") #ANSI escape code for cursor up 2 lines
Answered By - matszwecja
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.