Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update only two lines in print inside jupyter notebbok

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

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

Does this code snippet help you?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading