0

I'm trying to make a, "fake" loading bar as just a small task. I'm new to coding and this seems to work, but it seems a lot of code. I assume could be done in like 2 lines by someone with more skill than me. I would love to see how this could be refactored into a more efficient way. Any help would be greatly appreciated!

loading_bar = "LOADING\n[==========]"

print(loading_bar[0:10])
time.sleep(.300)
os.system('cls')
print(loading_bar[0:11])
time.sleep(.300)
os.system('cls')
print(loading_bar[0:12])
time.sleep(.300)
os.system('cls')
print(loading_bar[0:13])
time.sleep(.300)
os.system('cls')
print(loading_bar[0:14])
time.sleep(.300)
os.system('cls')
print(loading_bar[0:15])
time.sleep(.300)
os.system('cls')
print(loading_bar[0:16])
time.sleep(.300)
os.system('cls')
print(loading_bar[0:17])
time.sleep(.300)
os.system('cls')
print(loading_bar[0:18])
time.sleep(.300)
os.system('cls')
print(loading_bar)

I'm sorry if this isn't in the right place. I'm new to StackOverflow as well.

hoefling
  • 59,418
  • 12
  • 147
  • 194
  • Just use a loop for the whole middle part. – jsmolka Mar 01 '18 at 12:43
  • 1
    hint: `for i in range(10,19):` – Chris_Rands Mar 01 '18 at 12:45
  • 1
    Besides the answers, avoid `os.system('cls')` like the plague. It is the code equivalent of having your frontdoor lock broken and calling a locksmith each time you are leaving or arriving home (instead of asking him to fix it) . (You create an external process just to erase the screen, and it is a system dependent command). – jsbueno Mar 01 '18 at 14:04

2 Answers2

1

I would do it this way-

import sys
import time
loading= "LOADING\n"
bar = "[==========]"
print(loading)
for c in bar:
    time.sleep(0.3)
    sys.stdout.write(c)
    sys.stdout.flush()

Explanation: The for c in bar loop means "for each character c in the string bar". And before printing each character there's the delay just like in your code. Then I've used sys.stdout.write instead of print to avoid printing newline. sys.stdout.flush() means to immediately print the output onto terminal which otherwise would stay in a buffer. You can imagine buffer to be like a internal variable which to which we can keep on appending using print or sys.stdout.write. More info on it here

If you need help understanding anything else feel free to comment :)

Udayraj Deshmukh
  • 1,814
  • 1
  • 14
  • 22
1

There is a neat library for displaying progress in terminal, named tqdm. Install it with

$ pip install tqdm

Example script:

import time
from tqdm import tqdm

seconds = 10

for i in tqdm(range(seconds)):
    time.sleep(1)  # sleep one second in each iteration

Run the script:

$ python spam.py
100%|██████████████████████████████| 10/10 [00:10<00:00,  1.00s/it]

tqdm is highly customizable, check out the documentation available on its PyPI page. Example with prepending custom message before the progress bar:

import time
from tqdm import tqdm

for i in tqdm(range(10), desc='LOADING'):
    time.sleep(1)

Output:

$ python spam.py
LOADING: 100%|█████████████████████| 10/10 [00:10<00:00,  1.00s/it]
hoefling
  • 59,418
  • 12
  • 147
  • 194