Search code examples
pythonpython-3.xtqdm

Using tqdm with subprocess


I'm trying to add a progress bar feature to an open-source project I contribute to. This software multi-threads commands passed to it, in an easy to use way. At the moment, I am trying to add a progress bar functionality to the application, however am unable to get the progress bar to stick to the bottom and update.

We are using the following code to process commands:

subprocess.call(task, shell=True)

I currently use tqdm.update() to update the progress of the bar, however that keeps printing on a new line, making the terminal look horrible. Image displaying non-sticky progress bar

To see exactly how we have used the tqdm package, please see the link below: https://github.com/codingo/Interlace/blob/master/Interlace/lib/threader.py

Finally, our aim for this application is having a progress bar stuck to the bottom of the terminal, so that it doesn't affect the output and looks reasonably clean. Any help would be appreciated!


Solution

  • Patched the issue by changing the following:

        @staticmethod
        def run_task(task):
            subprocess.call(task, shell=True)
    

    into:

        @staticmethod
        def run_task(task, t):
            s = subprocess.Popen(task, shell=True, stdout=subprocess.PIPE)
            t.write(s.stdout.readline().decode("utf-8"))
    

    This basically allows us to pipe all of the output of our task into stdout and then decode the bytes using UTF-8 and print it out using tqdm.write()

    Worked like a charm!