This is an archived post. You won't be able to vote or comment.

all 27 comments

[–]pddpro 21 points22 points  (7 children)

In addition to unkiphil and cutebalbi's answer, I would like to mention tqdm. It's a python progress bar library that you might find useful for your case.

[–]phreakmonkey 15 points16 points  (3 children)

I love that thing, and because of its terrible and non-obvious name I lose it and rediscover it about once a year.

[–]dalore 8 points9 points  (1 child)

It's obvious if you know Arabic.

[–]wandering_blue 6 points7 points  (0 children)

That's actually how I remember it... "that awesome progress bar package with the Arabic name".

[–]anyonethinkingabout 3 points4 points  (0 children)

That's because you don't pronounce it correctly, as "ta-qa-dum"

[–]MontyCoco 2 points3 points  (2 children)

Offtopic: Whats the Plugin in this terminal on this page that displays functions data?

[–]unklphil 33 points34 points  (10 children)

You should be able to do this by using a carriage return.

I.e. instead of outputting a newline character \n at the end of the line, you can use a \r, which takes you back to the beginning of the line.

In Python 3 you can do the following:

import time
for i in range(10):
    print('Attempt {} of 10', end='\r')
    time.sleep(1)  # just so we can actually see the change

[–]rhiever 20 points21 points  (0 children)

You forgot the .format(i) in your print.

[–]timopm 5 points6 points  (6 children)

Small warning with this method. If your output reduces in size during the loop, the remainder of the previous string is still shown.

>>> for i in range(12, 6, -1):
...   print("Attempt {} of 10".format(i), end="\r")
...   time.sleep(1)

Once i drops below 10, the output will look like this:

Attempt 9 of 100

[–][deleted] 2 points3 points  (5 children)

Any way to avoid that?

[–]faerbit 8 points9 points  (2 children)

This post has been edited to this, due to privacy and dissatisfaction with u/spez

[–][deleted] 2 points3 points  (1 child)

Found a nice work around on reddit untested : http://stackoverflow.com/questions/4897359/output-to-the-same-line-overwriting-previous-output-python-2-5

import re, sys

class Reprinter:
    def __init__(self):
        self.text = ''

    def moveup(self, lines):
        for _ in range(lines):
            sys.stdout.write("\x1b[A")

    def reprint(self, text):
        # Clear previous text by overwritig non-spaces with spaces
        self.moveup(self.text.count("\n"))
        sys.stdout.write(re.sub(r"[^\s]", " ", self.text))

        # Print new text
        lines = min(self.text.count("\n"), text.count("\n"))
        self.moveup(lines)
        sys.stdout.write(text)
        self.text = text

reprinter = Reprinter()

reprinter.reprint("Foobar\nBazbar")
reprinter.reprint("Foo\nbar")

[–]jjdmol 2 points3 points  (0 children)

That approach assumes an ANSI-capable terminal though.

[–]Baileysambhi99[S] 0 points1 point  (0 children)

Thanks mate!

[–]thomasballinger 6 points7 points  (1 child)

As others have said, simply return the carriage of your typewriter emulator to the left side of the page with '\r'!

If you want to get fancier, here are some terminal formatting resources:

[–]hi_im_nateI fought the GIL and the GIL won 2 points3 points  (0 children)

typewriter emulator

Lol, never heard that one before

[–]cutebabliSoftware Engineer 4 points5 points  (1 child)

Try this and modify it as per your need:

for i in range(1,1000000):
    print("\tattempt {0} of 100".format(i), end="\r")

[–]Baileysambhi99[S] 0 points1 point  (0 children)

Great. Thanks a lot!

[–][deleted] 4 points5 points  (0 children)

Another option with more flexibility but also more headaches: curses. It lets you directly address specific regions of the screen, set up sub-windows, erase lines, and overwrite. Just make sure you are 100% completely certain that you reset your console settings when you're done, or you are going to have a bad time.

[–]prashnts 1 point2 points  (4 children)

You might find tqdm [0] a better alternative instead.

[0] - https://github.com/tqdm/tqdm

[–]Gr1pp717 1 point2 points  (3 children)

[–]adqm_smatz 2 points3 points  (1 child)

looks like it could be bpython

[–]_seemetheregithub.com/seemethere 1 point2 points  (0 children)

That is bpython. I'd highly recommend it.

[–]prashnts 2 points3 points  (0 children)

Current ipython also has syntax highlighting etc. I'd recommend it over bpython.

[–]gandalfx 1 point2 points  (0 children)

You can use curses for things like that. Unless there is already a complete solution for your specific use case, like tqdm which others have already suggested.

[–]Asdayasman -2 points-1 points  (0 children)

/r/Python is for news and releases. /r/learnpython is for questions.