import sys import time import multiprocessing def change_char_at(s: str, ch: str, i: int, j: int=None): '''strings are immutable so i made this handy-little perform to 'mutate' strings much like => s[i] = ch # if j is just not specified s[i:j] = ch # if j is specified *observe*: detrimental indices doesn't work! ''' if not j: j = i + 1 return s[:i] + ch + s[j:] def loader(width: int=20, bar_width: int=3): ''' A easy loading bar which exhibits no info or no matter the perform ought to be run in a special thread and have to be killed. ''' s = '[' + ' ' * (width - bar_width - 1) + ']' whereas 1: for i in vary(1, width - bar_width - 1): sys.stdout.write('r' + change_char_at(s, '===', i)) sys.stdout.flush() time.sleep(0.1) for i in vary(width - bar_width - 1, 0, -1): sys.stdout.write('r' + change_char_at(s, '===', i)) sys.stdout.flush() time.sleep(0.1) def loader_with_wait(wait: int=None, width: int=20, bar_width: int=3): ''' A easy loading bar which exhibits no info or no matter. it'll give up after `wait` seconds or will *run eternally* if wait is just not specified param: wait: for the way a lot time ought to the loading bar run. ''' # Begin the loader p = multiprocessing.Course of(goal=loader, title='loader', args=(width, bar_width)) p.begin() if wait: # let the loader run for `wait` seconds time.sleep(wait) # terminate the loader() perform p.terminate() # Cleanup p.be a part of() # Begin from newline sys.stdout.write('n') if __name__ == '__main__': attempt: loader_with_wait(10) besides KeyboardInterrupt: move
I made a easy progress bar in python. The animation is cool!
it may possibly anticipate a number of seconds or it may possibly run eternally, the selection is upto you.
it may be utilized in different applications and might run in a separate thread.
Implementing the wait perform was the toughest factor to do. is there every other technique to do it?
Any recommendations will probably be useful… 🙂
see it in motion: https://asciinema.org/a/323180