Ask Your Question
2

Carriage Return and notebook cell output

asked 2011-08-10 16:14:51 +0100

Eugene gravatar image

updated 2011-08-11 03:15:27 +0100

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?

edit retag flag offensive close merge delete

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 ( 2011-08-11 00:48:33 +0100 )edit

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 ( 2011-08-11 08:20:03 +0100 )edit

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

Eugene gravatar imageEugene ( 2011-08-11 09:25:07 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted
2

answered 2011-08-12 13:59:42 +0100

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!
edit flag offensive delete link more
0

answered 2011-08-11 03:34:46 +0100

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.

edit flag offensive delete link more

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 ( 2011-08-11 15:59:27 +0100 )edit
0

answered 2011-08-10 17:15:24 +0100

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?

edit flag offensive delete link more

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 ( 2011-08-10 18:12:57 +0100 )edit

oh, right . . . sorry

niles gravatar imageniles ( 2011-08-11 00:24:52 +0100 )edit

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: 2011-08-10 16:14:51 +0100

Seen: 2,592 times

Last updated: Aug 12 '11