'Waiting' animation in command prompt (Python)
Asked Answered
B

6

24

I have a Python script which takes a long time to run. I'd quite like to have the command line output to have a little 'waiting' animation, much like the swirly circle we get in browsers for AJAX requests. Something like an output of a '\', then this is replaced by a '|', then '/', then '-', '|', etc, like the text is going round in circles. I am not sure how to replace the previous printed text in Python.

Bookout answered 12/8, 2011 at 10:56 Comment(0)
R
39

Use \r and print-without-newline (that is, suffix with a comma):

animation = "|/-\\"
idx = 0
while thing_not_complete():
    print(animation[idx % len(animation)], end="\r")
    idx += 1
    time.sleep(0.1)

For Python 2, use this print syntax:

print animation[idx % len(animation)] + "\r",
Rounders answered 12/8, 2011 at 11:2 Comment(5)
Works great with me (when changing 'thing_not_complete()' to 'True'), excellent little animation!Bookout
Here is something pre baked.Whitten
What would change in Python 3 in the print line?Norry
@Norry Amended answer.Rounders
Is it possible to make the "\r" it work in the python IDLE. In the IDLE (3.7 on windows) all characters are just printed one after another...Narcosynthesis
U
12

Just another pretty variant

import time

bar = [
    " [=     ]",
    " [ =    ]",
    " [  =   ]",
    " [   =  ]",
    " [    = ]",
    " [     =]",
    " [    = ]",
    " [   =  ]",
    " [  =   ]",
    " [ =    ]",
]
i = 0

while True:
    print(bar[i % len(bar)], end="\r")
    time.sleep(.2)
    i += 1
Unhappy answered 25/10, 2019 at 9:6 Comment(0)
F
6

A loading bar useful for if you're installing something.

animation = [
"[        ]",
"[=       ]",
"[===     ]",
"[====    ]",
"[=====   ]",
"[======  ]",
"[======= ]",
"[========]",
"[ =======]",
"[  ======]",
"[   =====]",
"[    ====]",
"[     ===]",
"[      ==]",
"[       =]",
"[        ]",
"[        ]"
]

notcomplete = True

i = 0

while notcomplete:
    print(animation[i % len(animation)], end='\r')
    time.sleep(.1)
    i += 1

if you want to make it last a couple seconds do

if i == 17*10:
    break

after the

i += 1
Felonry answered 1/10, 2020 at 22:29 Comment(0)
S
1

I think for best practices you can put a if condition at the end of the loop to avoid overflow with 'idx' variable in the case where the waiting funtion last longer than expexted:

import time

animation_sequence = "|/-\\"
idx = 0
while True:
    print(animation_sequence[idx % len(animation_sequence)], end="\r")
    idx += 1
    time.sleep(0.1)

    if idx == len(animation_sequence):
        idx = 0

    # Verify the change in idx variable
    print(f'   idx: {idx}', end='\r')
Sentience answered 30/1, 2022 at 21:27 Comment(1)
or simply instead of idx +=1 and the overflow test : (idx = idx +1)%4Vaporous
I
0

I liked the aesthetic of @Warren and built upon it to allow a personalization.

import time

# User Specification
bar = '========'
width = 8

bar_length = len( bar )
total_width = width + 2 * bar_length

t0 = time.time()
idx = 0

while time.time()-t0<30:
    string = f'{idx * " " + bar: <{total_width }}'
    substring = string[bar_length:bar_length+width]
    print(f'[{substring}]', end='\r')
    idx = (idx + 1) % total_width 
    time.sleep(0.1)

This approach saves the need to specify each 'frame' of the loading bar and allows you to replace bar with anything you want to see flying across the screen.

Illhumored answered 16/11, 2023 at 19:25 Comment(0)
C
-1

Python's built-in curses package contains utilities for controlling what is printed to a terminal screen.

Chambray answered 12/8, 2011 at 11:1 Comment(1)
curses has nothing to do with this post. he was just asking about a animation loop, ofc you can make in curses but that is not the answer for this post.Jessikajessup

© 2022 - 2024 — McMap. All rights reserved.