10

I want to refresh some info dynamically(just like progress bar), I can do it with following code

#! /usr/bin/env python
import sys
import time

print "start the output"
def loop():
    i = 0

    while 1:
        i += 1
        output = "\rFirst_line%s..." % str(i) 
        sys.stdout.write(output)        
        sys.stdout.flush()
        time.sleep(1)
loop()

It could only output single_line info dynamically, When add '\n' into output, It couldn't work as expect.

output = "\rFirst_line%s...\n" % str(i)

Any way could help it to refresh multi_line content?

user1675167
  • 179
  • 1
  • 8

5 Answers5

6

I also had that situation, and I finally got a idea to solve this ;D

reprint- A simple module for Python 2/3 to print and refresh multi line output contents in terminal

You can simply treat that output instance as a normal dict or list(depend on which mode you use). When you modify that content in the output instance, the output in terminal will automatically refresh :D

Here is a example:

from reprint import output
import time
import random

print "start the output"

with output(initial_len=3, interval=0) as output_lines:
    while True:
        output_lines[0] = "First_line  {}...".format(random.randint(1,10))
        output_lines[1] = "Second_line {}...".format(random.randint(1,10))
        output_lines[2] = "Third_line  {}...".format(random.randint(1,10))
        time.sleep(0.5)
Yinzo
  • 156
  • 2
  • 5
1

You could do it with curses, but it's nontrivial.

orip
  • 69,626
  • 21
  • 116
  • 145
0

There is no way to do that system-independently (when saying "system" I mean not just OS, but also terminal app and its setup as well) since there is no standard escape sequence that would move cursor up. As for system-dependent ways, they may exist for your configuration, but more information about your system is needed. And anyway, usually it is not a good idea to use such features.

P.S. Hope you are not going to redirect your program's output to a file. If redirected to a file, such progress indicators produce terrible output.

Ellioh
  • 4,892
  • 2
  • 19
  • 32
0

Simply do:

import time
import random

while True:
    print(f'''First Line {random.randint(1,10)}
Second Line {random.randint(1,10)}        
Third Line  {random.randint(1,10)}''')
    sys.stdout.write("\x1b[1A"*3) # Cursor up 3 lines
    time.sleep(0.5)
-1

write '\b' to the console:

import sys
import time

print "start the output"
def loop():
    i = 0
    output = "\rFirst_line%s..." % str(0) 
    sys.stdout.write(output)
    while 1:
        sys.stdout.write('\b'*len(output))
        i += 1
        output = "\rFirst_line%s..." % str(i) 
        sys.stdout.write(output)        
        sys.stdout.flush()
        time.sleep(1)
loop()
imxylz
  • 7,755
  • 4
  • 26
  • 25
  • Hi, imxylz. Thanks for fast response, but if there is a '\n' in output, it couldn't still work well, such as output = "\rFirst_line%s...\n+++" % str(i). – user1675167 Jan 13 '13 at 05:19
  • You cannot write back(backspace) with new line though tt may seem reasonable but that would not be backward compatible with teletypes before. If you want clear all the screen, check this. http://stackoverflow.com/questions/3136202/deleting-already-printed-in-python#3136352 – imxylz Jan 13 '13 at 05:35
  • I try the new function print(object1,object2,sep='\n',end=''), following with sys.stdout.write('\b\b\b/b\'),the '\b' couldn't still work well for string with '\n'. It seems that I have to change my way( use os.system('clear'). Thanks again – user1675167 Jan 13 '13 at 07:08