Here is a code example to print some numbers at the same cursor position of the console, without moving the characters from place.
Code Example
from sys import stdout
from time import sleep
for i in range(1,20):
stdout.write("\r%d" % i)
stdout.flush()
sleep(1)
stdout.write("\n") # move the cursor to the next line
Question
Does this same approach work if we need to print an entire table over and over at the same position, without creating a new table line, making it altogether
static
.
My aim is to make the code given at the buttom to work, same as the code example
shared above.
When printing a table on console the headers of the table must not change, but the values (row elements) passed must dynamically change at the same cell positions, iterating the values passed.
Below is the code I aimed for.
from prettytable import PrettyTable
from sys import stdout
from time import sleep
t = PrettyTable(['Name', 'Age'])
lis = [['Alice', 25],['Alice', 20],['Man', 20]]
for x in lis:
t.add_row(x)
print(t, end='\r')
t.clear_rows()
sleep(1)
stdout.write("\n")
In here, iterating the print(t, end='\r')
is printing the tables everytime onto a new line.
I wish to see that table printed for the first iteration (for loop), gets completely replaced by the tables of the next iterations and so on.