Ask Your Question
2

Carriage Return and notebook cell output

asked 13 years ago

Eugene gravatar image

updated 13 years ago

Hi!

This code works different in python (console) and within sage's notebook:

>>> print 'foooo','\r','baar'
baaro

print 'foooo','\r','baar'
foooo baar

Is it possible somehow to use CR in notebook?

P.S. Initial problem was to create a progress bar, since no graphical bars found, I tried to use text-based bar, and now I met more generic issue this CR thing.

Variant 2: Perhaps instead of using CR it is possible to completely clear cell's output?

Preview: (hide)

Comments

For progress bars, you might need to do something different in the notebook and from the command line. Using http://pypi.python.org/pypi/progressbar/2.2 for the command line might be good. For the notebook, it would be nice to use graphics, maybe drawing a sequence of pngs, each overwriting the previous, as the computation progresses. It might be tricky finding one solution which works for both of the settings.

John Palmieri gravatar imageJohn Palmieri ( 13 years ago )

I imagine that the "interact" functionality has to deal with a problem like this too, but I don't know how it works. Also, the responses to the following might help too (use javascript to update the output): http://ask.sagemath.org/question/517/fast-show-for-cached-plots-for-interact

niles gravatar imageniles ( 13 years ago )

Looks the same way I did a bit late, but in case of inner html changes - it wont change content immediately.

Eugene gravatar imageEugene ( 13 years ago )

3 Answers

Sort by » oldest newest most voted
2

answered 13 years ago

niles gravatar image

Here's something I stumbled accross; it doesn't solve the carriage return problem, but does give a single line printed incrementally, so might work for you. The essential ingredient is sys.stdout.flush():

import sys
print "working:",
sys.stdout.flush()
for i in range(10):
    sleep(.5)
    print i,
    sys.stdout.flush()
print ''
sleep(1)
print "done!"

working: 0 1 2 3 4 5 6 7 8 9 
done!
Preview: (hide)
link
0

answered 13 years ago

Eugene gravatar image

I tried to do a trick like:

html('<div id="mydiv"></div>')

And update it's content later:

import time

for i in range(10):
    time.sleep(0.5)
    html('<script>document.getElementById("mydiv").innerHTML = "Test % i"</script>' % i)

But this options cannot be used because of two things: it creates lots of empty newlines (after each html injection) and (I do not know why?) div block actually updates itself only after all calculation in current cell.

Preview: (hide)
link

Comments

Does anybody know how to fix this two problems? (Or should I create another question for it, cuz it is not really related to CR problem?)

Eugene gravatar imageEugene ( 13 years ago )
0

answered 13 years ago

niles gravatar image

Hmmm . . . I couldn't figure it out either. The following does work:

s = 'fooooo'
t = 'baar'
print t + s[len(t):]

baaroo

Will that work in your situation?

Preview: (hide)
link

Comments

Thanks for the reply! Unfortunately this will not work for me, the idea is to print progress change, i.e. to print '###### 80%' then ''####### 90%' etc, and to use CR to replace previously printed line with the new one.

Eugene gravatar imageEugene ( 13 years ago )

oh, right . . . sorry

niles gravatar imageniles ( 13 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 13 years ago

Seen: 2,657 times

Last updated: Aug 12 '11