Ask Your Question

Carriage Return and notebook cell output

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

Eugene gravatar image

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


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

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

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


For progress bars, you might need to do something different in the notebook and from the command line. Using 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 +0200 )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):

niles gravatar imageniles ( 2011-08-11 08:20:03 +0200 )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 +0200 )edit

3 Answers

Sort by ยป oldest newest most voted

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

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:",
for i in range(10):
    print i,
print ''
print "done!"

working: 0 1 2 3 4 5 6 7 8 9 
edit flag offensive delete link more

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

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):
    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


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 +0200 )edit

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

niles gravatar image

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

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


Will that work in your situation?

edit flag offensive delete link more


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 +0200 )edit

oh, right . . . sorry

niles gravatar imageniles ( 2011-08-11 00:24:52 +0200 )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


Asked: 2011-08-10 16:14:51 +0200

Seen: 2,496 times

Last updated: Aug 12 '11