0

My program is written in Python 2.7 and I want to do a dynamic update. The output is in table-view and has hundreds of characters. A really good answer I found here, however, more than 100 characters it crushed (as promised in the article). Moreover, I don't know how many lines the table has, it changes dynamically. Furthermore, I don't want to use curses, because I want the output to be inline in the console like running 'regular' command

Output example:

+------+--------------+-------------+
| Type |  IP Address  |    Status   |
+------+--------------+-------------+
|  aa  | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
| cccc | 10.11.10.10  | in Progress |
|  aa  | 10.11.10.10  | in Progress |
|  aa  | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
| cccc | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
+------+--------------+-------------+

I'm trying to avoid:

+------+--------------+-------------+
| Type |  IP Address  |    Status   |
+------+--------------+-------------+
|  aa  | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
| cccc | 10.11.10.10  | in Progress |
|  aa  | 10.11.10.10  | in Progress |
|  aa  | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
| cccc | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
+------+--------------+-------------+
+------+--------------+-------------+
| Type |  IP Address  |    Status   |
+------+--------------+-------------+
|  aa  | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
| cccc | 10.11.10.10  | in Progress |
|  aa  | 10.11.10.10  | in Progress |
|  aa  | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
| cccc | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
+------+--------------+-------------+
+------+--------------+-------------+
| Type |  IP Address  |    Status   |
+------+--------------+-------------+
|  aa  | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
| cccc | 10.11.10.10  | in Progress |
|  aa  | 10.11.10.10  | in Progress |
|  aa  | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
| cccc | 10.11.10.10  | in Progress |
|  bb  | 10.11.10.10  | in Progress |
+------+--------------+-------------+
ihatecsv
  • 532
  • 6
  • 20
Bar
  • 11
  • 1
  • 6

1 Answers1

0

You can use ANSI code to go up multiple lines. It should work in linux:

import time
import sys

for i in range(0, 100):
  # print 4 lines
  print ("line 1\nline 2\nline 3")
  print (str(i))
  time.sleep(0.2)
  sys.stdout.write("\033[4A") # go up 4 lines

It will work in some terminals. It worked in windows putty. If you go back more than number of rows in putty terminal, it won't let you roll the text up, it will move cursor to the first visible line. Source of the ANSII code.

nio
  • 5,141
  • 2
  • 24
  • 35
  • thank you for your answer. it always puts the key-placer on top even though the command run in the of the screen and I have changed the lines number many times: from 1 line to the table length + n – Bar Jul 19 '18 at 14:23
  • I'm not sure if it is ubuntu related or ssh terminal – Bar Jul 19 '18 at 15:25