[Solved]-Dynamic one-line output in Django management command

12๐Ÿ‘

โœ…

tl;dr

stdout output is buffered by default, so flush manually:

import time

from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = "My shiny new management command."

    def handle(self, *args, **options):
        self.stdout.write("Writing progress test!")
        for i in range(100):
            self.stdout.write("%{}".format(i), ending='\r')
            self.stdout.flush()
            time.sleep(0.01)

Reference

I followed this issue that someone opened a few years back, and this SO thread; see both for longer details and other solutions, such as the python -u option.

๐Ÿ‘คtutuDajuju

Leave a comment