Skip to content

Instantly share code, notes, and snippets.

@aasmpro
Created March 24, 2022 20:51
Show Gist options
  • Save aasmpro/ac6f33d5380b673b2fd2b38f8fa479a8 to your computer and use it in GitHub Desktop.
Save aasmpro/ac6f33d5380b673b2fd2b38f8fa479a8 to your computer and use it in GitHub Desktop.
Simple Spinner (loading animation)
from itertools import cycle
from time import sleep
def spin(data, duration=0.2, check=lambda: False):
for frame in cycle(data):
if not check():
print("\r", frame, sep="", end="", flush=True)
sleep(duration)
else:
print("\r", "[OK] task done.", sep="", flush=True)
break
def check():
global time, duration
time += duration
return time >= 30
time = 0
duration = 0.1
# spin(
# [
# "... ",
# " ... ",
# " ... ",
# " ...",
# " ... ",
# " ... ",
# ],
# duration=duration,
# check=check,
# )
# spin(
# [
# ". ",
# " . ",
# " . ",
# " . ",
# " . ",
# " .",
# ". .",
# " . .",
# " . .",
# " . .",
# " ..",
# ". ..",
# " . ..",
# " . ..",
# " ...",
# ". ...",
# " . ...",
# " ....",
# ". ....",
# " .....",
# "......",
# "..... ",
# ".... .",
# ".... ",
# "... . ",
# "... .",
# "... ",
# ".. . ",
# ".. . ",
# ".. .",
# ".. ",
# ". . ",
# ". . ",
# ". . ",
# ". .",
# ],
# duration=duration,
# check=check,
# )
# spin(
# [
# ". .",
# ": ",
# ".. ",
# " : ",
# " .. ",
# " : ",
# " .. ",
# " : ",
# " .. ",
# " : ",
# " ..",
# " :",
# ],
# duration=duration,
# check=check,
# )
# spin(
# [
# "------",
# ">-----",
# "<>----",
# "-<>---",
# "--<>--",
# "---<>-",
# "----<>",
# "-----<",
# ],
# duration=duration,
# check=check,
# )
# spin(
# [
# "||||||||",
# "]|||||||",
# "[]||||||",
# "|[]|||||",
# "||[]||||",
# "|||[]|||",
# "||||[]||",
# "|||||[]|",
# "||||||[]",
# "|||||||[",
# "||||||||",
# "|||||||[",
# "||||||[]",
# "|||||[]|",
# "||||[]||",
# "|||[]|||",
# "||[]||||",
# "|[]|||||",
# "[]||||||",
# "]|||||||",
# ],
# duration=duration,
# check=check,
# )
# spin(
# [
# "||||||||",
# "]|||||||",
# " ]||||||",
# "[ ]|||||",
# "|[ ]||||",
# "||[ ]|||",
# "|||[ ]||",
# "||||[ ]|",
# "|||||[ ]",
# "||||||[ ",
# "|||||||[",
# "||||||||",
# "|||||||[",
# "||||||[ ",
# "|||||[ ]",
# "||||[ ]|",
# "|||[ ]||",
# "||[ ]|||",
# "|[ ]||||",
# "[ ]|||||",
# "]|||||||",
# ],
# duration=duration,
# check=check,
# )
# spin(
# [
# "000000",
# "000001",
# "000010",
# "000011",
# "000100",
# "000101",
# "000110",
# "000111",
# "001000",
# "001001",
# "001010",
# "001011",
# "001100",
# "001101",
# "001110",
# "001111",
# "010000",
# "010001",
# "010010",
# "010011",
# "010100",
# "010101",
# "010110",
# "010111",
# "011000",
# "011001",
# "011010",
# "011011",
# "011100",
# "011101",
# "011110",
# "011111",
# "100000",
# "100001",
# "100010",
# "100011",
# "100100",
# "100101",
# "100110",
# "100111",
# "101000",
# "101001",
# "101010",
# "101011",
# "101100",
# "101101",
# "101110",
# "101111",
# "110000",
# "110001",
# "110010",
# "110011",
# "110100",
# "110101",
# "110110",
# "110111",
# "111000",
# "111001",
# "111010",
# "111011",
# "111100",
# "111101",
# "111110",
# "111111",
# ],
# duration=duration,
# check=check,
# )
# spin(
# [
# "000000",
# "100000",
# "110000",
# "111000",
# "111100",
# "111110",
# "111111",
# "211111",
# "221111",
# "222111",
# "222211",
# "222221",
# "222222",
# "322222",
# "332222",
# "333222",
# "333322",
# "333332",
# "333333",
# "433333",
# "443333",
# "444333",
# "444433",
# "444443",
# "444444",
# "544444",
# "554444",
# "555444",
# "555544",
# "555554",
# "555555",
# "655555",
# "665555",
# "666555",
# "666655",
# "666665",
# "666666",
# "766666",
# "776666",
# "777666",
# "777766",
# "777776",
# "777777",
# "877777",
# "887777",
# "888777",
# "888877",
# "888887",
# "888888",
# "988888",
# "998888",
# "999888",
# "999988",
# "999998",
# "999999",
# "099999",
# "009999",
# "000999",
# "000099",
# "000009",
# ],
# duration=duration,
# check=check,
# )
# spin(
# [
# "-",
# "\\",
# "|",
# "/",
# ],
# duration=duration,
# check=check,
# )
# spin(
# [
# "-------",
# "+------",
# "-+-----",
# "--+----",
# "---+---",
# "----+--",
# "-----+-",
# "------+",
# ],
# duration=duration,
# check=check,
# )
print("program completed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment