Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

ascii progressbar in notebook broken #2285

Open
mqk opened this Issue · 2 comments

3 participants

@mqk

I recently updated to the tip of the IPython development and discovered that the ascii progressbar that I like to use (http://code.google.com/p/python-progressbar/) no longer works in the notebook. [I understand that using a javascript progressbar would be more appropriate for the notebook, but for sake of compatibility with running the same code in a terminal I'd like to use the ascii-based one also in the notebook.]

I've tracked the problem down to commit d980adf (yay git bisect!) where this line

txt = txt.replace('\r', '');

was removed from the function fixConsole() in IPython/frontend/html/notebook/static/js/utils.js. If I reinstate that line, the progressbar once again works for me.

Here's an example:

import time
import progressbar as pb

def example1():
    widgets = ['Test: ', pb.Percentage(), ' ', pb.Bar(marker=pb.RotatingMarker()),
               ' ', pb.ETA(), ' ', pb.FileTransferSpeed()]

    Neval = 1000
    pbar = pb.ProgressBar(widgets=widgets, maxval=Neval).start()
    for i in range(Neval):
        time.sleep(0.01)
        pbar.update(i)
    pbar.finish()

example1()
@fperez
Owner

I'm not sure whether this regression is a bug we can fix or an unavoidable side-effect of the improvements @minrk made in general to special character handling in the notebook. That logic was very involved and I know he went to a lot of trouble to handle as many special cases correctly as possible, so I'll defer to him on the handling of this one. Thanks for reporting it!

@minrk
Owner

The issue is the difference between how our js interprets '\r' and how a regular terminal does.

We interpret '\r' as clearing the line before it, whereas a terminal simply moves the cursor. This is rarely an issue, because in the following, the two interpretations behave the same:

markers = '|/-\\'
for i in range(32):
    m = markers[i % len(markers)]
    sys.stdout.write('\r' + (m * 10))
    sys.stdout.flush()
    time.sleep(0.1)

There are two cases where they do not behave the same:

  1. where subsequent lines are shorter than previous (terminals will only overwrite a partial line, leaving remnants (this is rarely desirable).
  2. if you put \r at the end of each line, instead of the beginning. In a terminal, this is perfectly identical to \r at the beginning (barring edge cases of the line before / after the progress bar), but in the Notebook it means each line is cleared before it ever gets drawn.

progressbar does 2., hence this issue.

You can actually fix this by changing this line in progressbar to putting \r at the head of the line instead of the tail.

Getting full \r support in the notebook is far from trivial, but we might be able to change fixCarriageReturn to handle \r at the end of a line specially.

@jgosmann jgosmann referenced this issue in nengo/nengo
Closed

Fancy progress #493

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.