Python how to make simple animated loading while process is running - Stack Overflow

PHOTO EMBED

Tue Dec 21 2021 04:32:24 GMT+0000 (Coordinated Universal Time)

Saved by [deleted user]

import itertools
import threading
import time
import sys

def animate_loading(method):
    
    def animated():
        
        done = False
        #here is the animation
        def animate():
            time.sleep(0.000001)
            for c in itertools.cycle(['|', '/', '-', '\\']):
                if done:
                    break
                sys.stdout.write('\rloading ' + c)
                sys.stdout.flush()
                time.sleep(0.1)
            sys.stdout.write('\rDone!     ')
            
        t = threading.Thread(target=animate)
        t.start()

        #process method here
        method()
        done = True
    
    return animated
content_copyCOPY

https://stackoverflow.com/questions/22029562/python-how-to-make-simple-animated-loading-while-process-is-running