Archive

Posts Tagged ‘web’

Show Upload Progress with Python aiohttp and tqdm

March 11th, 2017 No comments

I wanted to be able to show file upload progress for my Python asyncio-based Pushbullet.com library but could not find any obvious solution using aiohttp and tqdm. Here is what I created. Essentially I intercept the calls to reading a file and update the tqdm progress bar accordingly.

The key elements look like this:

    class tqio(io.BufferedReader):
        def __init__(self, file_path):
            super().__init__(open(file_path, "rb"))
            self.t = tqdm(desc="Upload",
                          unit="bytes",
                          unit_scale=True,
                          total=os.path.getsize(file_path))

        def read(self, *args, **kwargs):
            chunk = super().read(*args, **kwargs)
            self.t.update(len(chunk))
            return chunk

Read more…

Categories: Utility Tags: , ,