rsalmei / alive-progress

A new kind of Progress Bar, with real-time throughput, ETA, and very cool animations!
MIT License
5.53k stars 206 forks source link

bar() prints current position on Windows #50

Closed thmsklngr closed 4 years ago

thmsklngr commented 4 years ago

Hi,

I just installed your lib, it's great. Unfortunately I have an issue, which I can't explain.

I used your same code:

with alive_bar(1000) as bar:
     for i in range(1000):
             if i % 300 == 0:
                     print('found!')
             time.sleep(0.01)
             bar()

Result:

on 0: found!
on 1: 1
on 2: 2
on 3: 3
on 4: 4
on 5: 5
on 6: 6
on 7: 7
on 8: 8
on 9: 9
on 10: 10
on 11: 11
on 12: 12
on 13: 13
on 14: 14
on 15: 15
on 16: 16
...

So, even though I do not have any kind of print command, there's output.

Any ideas why?

Regards, Thomas

rsalmei commented 4 years ago

Hello @mosesontheweb, thank you, appreciated!

Unfortunately, I can't imagine how this would be possible. Note the code doesn't even include a print(i) to cause that output. Are you really sure this was the code you ran? Which version are you using? Which Python? I've simulated it now, latest version in a Python 3.8: asd

Regards, Rogério.

thmsklngr commented 4 years ago

Hi,

I'm running Python 3.6.9. Here's a cast I recorded for you: alive_progress

Regards, Thomas

rsalmei commented 4 years ago

Hey, that's bizarre! I don't have the slightest idea how that could happen, I'd say it's impossible without manually fiddling with the source files...

Could you please try in a new virtualenv? I like pyenv-virtualenv very very much, it just takes:

$ pyenv virtualenv 3.6.9 alive-test
$ pyenv shell alive-test

And this shell has that venv activated. Then you could:

$ pip install alive-progress
$ python

Could you please try that?

rsalmei commented 4 years ago

Well, please reopen if you have more information.

rsalmei commented 4 years ago

Hey @mosesontheweb, how are you?

~Why didn't you tell me this was occurring on Windows? It would be super relevant...~

Anyway, I just installed Python on a Windows machine, and experienced this exact issue! All I can say for now is that it really is the bar() handle, but don't have a clue why. Every time it is called, a new line gets printed. image

The relevant code seems to be my flush_buffer, which is called on every bar call. That is because if there's anything on print_buffer, it must be flushed to get the correct count, before incrementing it. There it does have a print() statement. https://github.com/rsalmei/alive-progress/blob/master/alive_progress/core/progress.py#L121-L123

But for anything to enter the print_buffer, it must be in: https://github.com/rsalmei/alive-progress/blob/master/alive_progress/core/progress.py#L163-L167

Would it be the \n? Maybe on Windows it would be \r\n? I'm going to open this again, and do some more debugging.

rsalmei commented 4 years ago

Humm, inside WSL (Windows Subsystem for Linux) it does work. Because it's ipython, not because of WSL. image

rsalmei commented 4 years ago

Awesome, I found it!!!! Regardless of how bizarre it may seem, it's the return value of bar() that is being printed on screen! I don't know why this Python is doing that, but just commenting that return makes it all work nicely! 😄 image

rsalmei commented 4 years ago

Ok, I've been using ipython and bpython for so long in my live that I've forgot about this quirk. It's because of Python, the default REPL, that this happens. It has nothing to do with Windows. Even on macOS I can simulate, it is because Python does not know how to ignore a return value. ipython on the other hand, interprets the statement differently, and captures only its final output:

image

That way, what I am going to do: I'll remove the return value by default, and create a new method called bar.current() that will return the current position if needed. It will be available in the next major 2.0! See #51

thmsklngr commented 4 years ago

Hi, sorry for the late reply. I thought that I mentioned it somewhere, but it seems that I didn't. My bad. But glad to see that you were finally able to reproduce the issue and add a fix in the next major.

Regards, Thomas