Python Forum
How can I add a progress bar for my software?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I add a progress bar for my software?
#9
I see, so with tqdm am I not able to create a single progress bar for each "for" loop in my code? today, I wrote this progress bar, it's a really simple function but, it works. let me know what do you think:

import os
import math
from datetime import datetime, timezone

# it can clear the terminal screen:
def clear(): 
    # for windows 
    if os.name == 'nt': 
        _ = os.system('cls') 
    # for mac and linux(here, os.name is 'posix') 
    else: 
        _ = os.system('clear')

# it can create a progress bar in the terminal:
def progress_bar(header, current_time, number):
    remains = 60 - number
    percentage = str(math.ceil(100*number//60))
    progress_bar = "\nLoading: [" + "#"*number + " "*remains + "]"
    clear()
    print(header, progress_bar, percentage + "% -", current_time + ",", datetime.utcnow().strftime('%H:%M:%S'))
to use the "progress_bar" function you have to call it many times in the script. for example:

from datetime import datetime, timezone
current_time = datetime.utcnow().strftime('%H:%M:%S')

menu = "-------------------\nEXAMPLE\n-------------------\ntext..\n"

progress_bar(menu, current_time, 0)
# code ..
# code ..
progress_bar(menu, current_time, 4)
# code ..
# code ..
progress_bar(menu, current_time, 8)
# code ..
# code ..
# .......
progress_bar(menu, current_time, 60)
I used this progress bar in my script and it works fine. you can see it in the attachments. unfortunately the percentuage shown by my progress bar is not the real percentuage, it depends by how many times did you use the function in the script and where, but it's ok, it doesn't matter. the main goal is to see the progress about my software.

thanks to this progress bar I could see where my script is slow. probably i will open another topic to understand better how to improve that pice of code, and if it's possible also how to use more than one CPU with python.

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
RE: How can I add a progress bar for my software? - by aquerci - Nov-16-2019, 04:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Progress bar bnadir55 1 1,781 Apr-11-2022, 01:52 PM
Last Post: deanhystad
  Progress Indicator for Xmodem 0.4.6 KenHorse 1 1,931 Jan-30-2021, 07:12 PM
Last Post: bowlofred
  wget progress bar anasrocks 1 4,690 Jun-06-2019, 03:12 PM
Last Post: heiner55
  Progress Finished Question malonn 32 17,229 May-23-2018, 02:43 AM
Last Post: malonn
  how to progress with py jakegold98 1 2,596 Dec-05-2017, 02:58 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020