Showing task progress from a Django management command
Asked Answered
H

1

6

I have some Django management commands that call methods in other classes to fetch data from APIs. These tasks can take a long time to complete, and I'd like to show progress in the console in a concise manner.

I could use print() to output a single line like "Fetched 22 of 3000" that writes over itself, using something like:

print('Fetched %d of %d' % (n, total) + ' '*30, end='\r')

But using print() seems a bit nasty, and it gets output to the console when tests are run. So it seems better to use logging, but I can't see a way using that to display a single, constantly updated, "progress" line in the console.

Is there a nice way to do this?

Heterophyllous answered 7/5, 2016 at 16:3 Comment(1)
If you use self.stdout.write and self.stderr.write instead of print, you can pass os.devnull to call_command for stdout/stderr kwargs in your tests and it won't output to the console.Prager
V
7

Maybe tqdm is a helpful python package for you.

Verbenia answered 7/5, 2016 at 16:14 Comment(2)
I hadn't seen that before, and you may be right. One thing though - I'm not sure how to disable its console output while running unit tests. Any ideas?Heterophyllous
@PhilGyford you can set the disable=True argument.Phina

© 2022 - 2024 — McMap. All rights reserved.