
Progress Bars in Python - sammycdubs
https://samwilkinson.io/article/2018/12/17/progress-bars-python/
======
user2426679
I've always just used `sys.stdout.write("\r%i/%i" %(c, n));
sys.stdout.flush()`

That is exactly what this library does: "tqdm does not require any
dependencies (not even curses!), just Python and an environment supporting
carriage return \r and line feed \n control characters." \--
[https://github.com/tqdm/tqdm](https://github.com/tqdm/tqdm)

Seems unnecessary imo to depend on an external library just to wrap and auto-
prettify those 2 stdout commands.

~~~
cirgue
Can you provide an example snippet?

~~~
user2426679
edit: how the hell does markdown/code-snippet work on hackernews?! Nicely
formatted pic of the below text soup:

[https://i.imgur.com/fvEeaJA.png](https://i.imgur.com/fvEeaJA.png)

My library function is:

import sys def writeToTerminal(txt, line_len=79): # line_len is the max num of
characters that can fit onto a terminal line without overflowing to a newline

    
    
    		# overwrite the line with blank spaces
    		# this takes care of the case where len(txt) is fewer than the [ len(txt) when this function was last called ]
    		sys.stdout.write( "\r" + " "*line_len )
    		sys.stdout.flush()
            
    		# write the txt, trimming to line_len
    		sys.stdout.write( "\r" + txt.replace("\t", "    ")[:line_len] )
    		sys.stdout.flush()
    

Example usage:

from time import sleep L = list(range(10)) for c, i in enumerate(L, start=1):
writeToTerminal("%i / %i" %(c, len(L))) sleep(1)

~~~
detaro
So it doesn't autodetect the terminal width, and thus _doesn 't_ do the same
as the library?

~~~
cat199
it shows a progress indicator, which is _functionally_ the same from a user
perspective.

the visual display of this progress indicator is different yes -

This method doesn't need any notion of terminal width because it just prints
'N / N' at the beginning of the current line where the cursor is when called.

~~~
user2426679
I tend to side with your opinion (obviously, based on the above code), but
detaro does have a point, which is that the progress message will "overflow"
onto a new/next line in the console if the message has more characters than
will fit on a single of the terminal.

fwiw, line_len=79 is the default on both windows and linux ime. You would only
encounter overflow if you (1) shrunk the terminal from its default width and
(2) printed a message that was longer than that shrunken width.

If you really needed to have a progress bar that accounted for a shrunken
display, it could be accomplished trivially with Python 3.? and less-trivially
with Py2.

I could see depending on this library if the application was client facing and
needed to be automagically pretty.

Also, one thing to note is that tqdm only fetches the console width when
init'ing a progress bar... if you shrink the terminal while a progress bar is
still "in progress," you will encounter the same overflow issue.

------
anhtran
I’m using this for progress bar in python
[http://click.palletsprojects.com/en/5.x/utils/](http://click.palletsprojects.com/en/5.x/utils/)

