3

From what I've seen progress bars mostly work by wrapping an iterable or something the like. I'm trying to get a progress bar from a callback function from another module.

The module is doing calculating on data and you can define a callback that uses three arguments: (file being worked on, current piece, total pieces)

Just for fun I did this:

def callback(filename, pc, num_pieces):
    print(f'{filename} - {pc} - {num_pieces}')

As expected, you get thousands of prints with increasing piece number: The file name also changes during the progress, but it's not important for my purposes.

How would you define and use the callback to create a progress bar for a CLI application?

1
  • 1
    can you just call tqdm.update after each invocation of the callback? Commented Mar 31, 2020 at 17:00

1 Answer 1

2

I have done something similar with TQDM.

If you have a classical callback, change it into update_to function, it would look somewhat like mine:

class DownloadProgressBar(tqdm):
    def update_to(self, current, total):
        self.total = total
        self.update(current - self.n)

And register the callback regularly with your DownloadProgressBar.

with DownloadProgressBar(unit='B', unit_scale=True) as t:
            client.download_media(media, filepath , progress_callback=t.update_to)

For more information look up at their Official docs.

( Keep in mind my example is showing a callback on a single file )

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

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.