1

I've got the following piece of code:

def progressbar(count, total, status=""):
    bar_len = 40
    filled_len = int(round(bar_len * count / float(total)))

    percents = round(100.1 * count / float(total), 1)
    bar = "X" * filled_len + "-" * (bar_len - filled_len)

    print("[{}] {}{} ...{}".format(bar, percents, "%", status),
          end="\r", flush=True)

And for calling the progress bar:

total = 100
i = 0
while i < total:
    i += 1
    progressbar(i, total, status="Creating stuff")
    time.sleep(1)

Where total is a number of iterations. When I run this code I get the progress bar running on multiple lines instead of just one. Any advice?

  • it write the progress of the bar on a new line as it is rewriting it everytime, are you wanting the initial bar the be overwritten when the bar updates? – James Lingham May 16 '18 at 20:04
  • I've always liked [this](http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html) blog. You can use the [colorama](https://pypi.org/project/colorama/#description) module on Windows. – pstatix May 16 '18 at 20:15
  • I understand, seems the flush=true isnt working. what version of python are you using?> – James Lingham May 16 '18 at 20:15
  • Yes. I'm looking for an progress bar that is overwritten while updating... It just writes every new line when updating. – lfsantosgeo May 17 '18 at 00:07
  • I'm using Python version 3.6.5. – lfsantosgeo May 17 '18 at 00:11

2 Answers2

0

I don't think you have any issue in your code.

print("[{}] {}{} ...{}".format(bar, percents, "%", status),
          **end="\r"**, flush=True)

the end="\n" argument in print() method should print the progress bar i one line.

if you run this code in terminal it should work fine.

I have previously seen an issue with PyCharm IDE where end="\r" doesn't work. It is probably a bug in the terminal emulator in the IDE.

Harrison
  • 5,095
  • 7
  • 40
  • 60
Rajesh Pantula
  • 10,061
  • 9
  • 43
  • 52
  • PyCharm doesnt support the `\r` ending. – pstatix May 16 '18 at 20:17
  • I'm not sure his issue is the progress bar not being on the new line, I think he is looking to clear the previously written progress bar when he writes the new update. i.e 1% bar will disappear and show 2% instead. – James Lingham May 16 '18 at 20:17
  • true, but it doesn't appear to me, not for me anyway – James Lingham May 16 '18 at 21:45
  • Hello! Thank you for the answer! Changing the \r to \n in the print() doesn't work for me. The console keeps getting every new line while the progress bar is updating... As @JamesLingham said, I'm looking for a progress bar, simple enough, that when written it overwrites itself. – lfsantosgeo May 17 '18 at 00:16
0

Thanks for everyone that commented on my topic trying to help. I got the solution on another StackOverflow Question: Python 3 progress bar shows nothing

So I tested two ways that were successful. The first one, to print using sys.stdout:

sys.stdout.write("\r[{}] {}{} ...{}".format(bar, percents, "%", status))
sys.stdout.flush()

And the second, using the print() but with the \r in front of the line:

print("\r[{}] {}{} ...{}".format(bar, percents, "%", status),
      end="", flush=True)

Both works perfectly. The first one needs the import sys.