Skip to content

Instantly share code, notes, and snippets.

@rudrathegreat
Last active February 15, 2024 17:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rudrathegreat/b11daed176c8119dcedbb6b06c953590 to your computer and use it in GitHub Desktop.
Save rudrathegreat/b11daed176c8119dcedbb6b06c953590 to your computer and use it in GitHub Desktop.
A Simple Loading Animation For the Command Line Using Python 3
"""
This program is designed to create
and animate a simple loading animation.
"""
from sys import stdout as terminal
from time import sleep
from itertools import cycle
from threading import Thread
done = False
def animate():
for c in itertools.cycle(['|', '/', '-', '\\']):
if done:
break
terminal.write('\rloading ' + c)
terminal.flush()
sleep(0.1)
terminal.write('\rDone! ')
terminal.flush()
t = Thread(target=animate)
t.start()
sleep(5)
done = True
@lporanta
Copy link

lporanta commented May 4, 2022

Line 14 should be for c in cycle(['|', '/', '-', '\\']): without the "itertools". After that this will work just fine.

@rudrathegreat
Copy link
Author

either way, it works fine. but you're way is much cleaner

@k-blo
Copy link

k-blo commented May 26, 2022

Thanks for sharing. some CLIs like mine will also print a % after "Done!". I added + "\n" to get rid of it. So terminal.write('\rDone! '+ "\n" )

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