1

I have a python program that outputs a few numpy matricies and some other information to my terminal in this fashion: Terminal output

Currently, to view the data, I follow this order:

  1. Print data
  2. Clear Screen
  3. Re print (and repeat)

However, this is really inefficient. What I would like to be able to do is to see the numbers changing as the program runs, but without the overhead of screen clearing. Is there a way I can format my output using native python so that I won't have to to this expensive operation? The idea is I can have something that works like htop (or similar formatted terminal programs, such as cmus).

1

2 Answers 2

0

Why clearing the screen is such an expense? The terminal has, most likely, a buffer of fixed size which is overwritten as older lines "scroll up". Rewriting the output may take a few ms (or more, depends on how big it is). If the output is really massive, consider a different approach, e.g. slice your matrices and only output what changed or use a plot to inspect your data (hint: matrices can be displayed as images). If you really need a "fixed" display on a terminal, then curses module is the way to go, but then your output cannot be that big, otherwise it would not fit a terminal window anyway. If you need to implement scrolling, then consider moving to a graphical user interface.

3
  • On my server (where the script is running), there is a measurable slowdown in the process for unknown reasons. In addition, clearing the screen, causes a lot of flickering, making any data hard to read (mostly for changes) Commented Apr 22, 2016 at 18:19
  • Do the numbers change very frequently? Would refreshing the display only when something changes work? If not, you really need curses.
    – Cyb3rFly3r
    Commented Apr 22, 2016 at 18:24
  • Most numbers stay constant, and the script already only prints when certain changes are met, so the change idea is already there. Looks like it's going to be curses for me. Commented Apr 23, 2016 at 16:41
0

you can use watch shell command

if it is a python script or can be trasformed in an unique python script

watch -n 1 'python xxx.py'

1 is the refresh time

-d highlight changes between updates

2
  • 1
    I had considered this, but I don't know the exact refresh rate of my program, the -d option is quite good though! Commented Apr 22, 2016 at 16:56
  • do you need more faster than 1 sec? or the script print other if is not ready? Commented Apr 22, 2016 at 17:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.