11

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
  • 189
  • 1
  • 2
  • 8

5 Answers5

7

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
  • 165
  • 2
  • 8
  • can you print the contents of a list on one of those lines? If I have a dynamically changing list of values, can I print that to say output_lines[3]? – corporateWhore Jun 29 '23 at 21:51
  • @corporateWhore Yes you can, as you change the output_lines[3] to the value you want to display, it will refresh the display on the command line automatically. – Yinzo Jul 05 '23 at 11:02
  • I can get it to print the list to the terminal, but I was looking for each line of the list being on it's own terminal line, '\n' separated to be specific. – corporateWhore Jul 06 '23 at 13:42
  • 1
    @corporateWhore You should assign each element in your list into different index of output_lines, or you can just use output_lines.change(your_list) to load all your elements in your_list into the output_lines object. – Yinzo Jul 07 '23 at 03:06
1

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

orip
  • 73,323
  • 21
  • 116
  • 148
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
  • 5,162
  • 2
  • 20
  • 33
0

Simply do:

import time
import random
import sys

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)
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
-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,847
  • 4
  • 28
  • 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