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 |
+------+--------------+-------------+
2
  • what operating system you want this program to run on? Commented Jul 19, 2018 at 13:37
  • the operating system is ubuntu16.04 Commented Jul 19, 2018 at 13:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your answer. it always puts the key-placer on top even though the command run in the <top, middle, bottom> of the screen and I have changed the lines number many times: from 1 line to the table length + n
I'm not sure if it is ubuntu related or ssh terminal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.