1

I want to add a progress bar that shows the progress of the executing function. The function imports data from a postgresql dataset and finally draw a bar_chart of percentages of each segments.

Here is my code:

class RfmTable:

def __init__(self,table_name,start_date,end_date):
    self.table_name = table_name
    self.start_date = start_date
    self.end_date = end_date
    self.quintiles = {}

def rfm_chart(self):        
    conn = psycopg2.connect(database=database_name, user=user_name,                           
                        password=password_info, host=host_info, port=port_info)
    cursor = conn.cursor()
    cursor.execute(...) #table information
    a = cursor.fetchall()

    # rfm analysis 
    # draw the chart
    for i, bar in enumerate(bars):
            value = bar.get_width()
            if segments_counts.index[i] in ['重要价值用户']:
                bar.set_color('firebrick')
            ax.text(value,
                    bar.get_y() + bar.get_height()/2,
                    '{:,} ({:}%)'.format(int(value),
                                       int(value*100/segments_counts.sum())),
                    va='center',
                    ha='left'
                   )
    return plt.show()

So what is a good way to add a progress bar to the function?

2 Answers 2

1

tqdm progress bar is probably the most featured progress bar for python. You can install it simply with pip install tqdm.

The following code demonstrates how easy it is to use.

from tqdm import tqdm
import time

for i in tqdm(range(100)):
    time.sleep(0.1)

This is what it looks like after running the above code example in pycharm:

enter image description here

1: Percent Complete
2: Progress Bar
3: Current Iteration out of Total Iterations
4: The Elapsed Time
5: The Estimated Remaining Time
6: Number of Iterations Processed per Second

Sign up to request clarification or add additional context in comments.

Comments

0

Using rich, we can add a progress bar that updates when given a function. Here's an example:

from rich.progress import Progress
import time
import inspect

def run(func, sleep=0):
    update = lambda progress, task: progress.update(task, advance=1)

    function = inspect.getsource(func)
    mylist = function.split('\n')
    mylist.remove(mylist[0])
    length = len(mylist)
    for num, line in enumerate(mylist):
        mylist[num] = line[8:] + "\nupdate(progress, task)\ntime.sleep(sleep)" #update the progress bar

    myexec = ''
    for line in mylist:
        myexec += line + '\n'

    with Progress() as progress:
        task = progress.add_task("Working...", total=length)
        exec(myexec)

if __name__ == '__main__':
    def myfunc():
        #chose to import these modules for progress bar example since they take a LONG time to import on my computer
        import pandas
        import openai

    run(myfunc)

I'm not going to explain fully how this works, but basically the function run() takes another function as an argument, and also how much to sleep between updating the progress bar. We then turn the function into a string using inspect, and add our extra stuff to update the progress bar after each line of the function, and run the function using exec().

While this code is a little inefficient (especially since it uses exec()), it definitely works. I recommend saving this as a file, maybe call it progress_bar? Then you can run this progress bar from anywhere in your python files, using the run() function.

So in your case, I'd assume you'd want:

from name_of_progress_bar_module import run

run(rfm_chart) #run the function, with default sleep time 0 between progress bar updating

You should probably note that rich looks best in the terminal. Otherwise...it works...but it doesn't look great.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.