Dynamic terminal printing with python

PythonTerminal

Python Problem Overview


Certain applications like hellanzb have a way of printing to the terminal with the appearance of dynamically refreshing data, kind of like top().

Whats the best method in python for doing this? I have read up on logging and curses, but don't know what to use. I am creating a reimplementation of top. If you have any other suggestions I am open to them as well.

Python Solutions


Solution 1 - Python

The simplest way, if you only ever need to update a single line (for instance, creating a progress bar), is to use '\r' (carriage return) and sys.stdout:

import sys
import time

for i in range(10):
    sys.stdout.write("\r{0}>".format("="*i))
    sys.stdout.flush()
    time.sleep(0.5)

If you need a proper console UI that support moving the pointer etc., use the curses module from the standard library:

import time
import curses

def pbar(window):
    for i in range(10):
        window.addstr(10, 10, "[" + ("=" * i) + ">" + (" " * (10 - i )) + "]")
        window.refresh()
        time.sleep(0.5)
    
curses.wrapper(pbar)

It's highly advisable to use the curses.wrapper function to call your main function, it will take care of cleaning up the terminal in case of an error, so it won't be in an unusable state afterwards.

If you create a more complex UI, you can create multiple windows for different parts of the screen, text input boxes and mouse support.

Solution 2 - Python

As most of the answers have already stated, you really have little option on Linux but to use ncurses. But what if you aren't on Linux, or want something a little more high-level for creating your terminal UI?

I personally found the lack of a modern, cross-platform terminal API in Python frustrating, so wrote asciimatics to solve this. Not only does it give you a simple cross-platform API, it also provides a lot of higher level abstractions for UI widgets and animations which could be easily used to create a top-like UI.

Solution 3 - Python

Sending output to the terminal via the print() command can be done without scrolling if you use the attribute "end".

The default is end='\n' which is a new line.

To suppress scrolling and overwrite the whole previous line, you can use the RETURN escape which is '\r'.

If you only want to rewrite the last four characters, you can use a few back-spaces.

print(value, "_of_", total, end='\r')

NOTE This works for the standard system terminal. The terminal emulator in some tools like IDLE has an error and the '\r' does not work properly, the output is simply concatenated with some non-printable character between.

BONUS INFORMATION FOR print() In the example above, the spaces on each side of "of" are meant to insure white-space between my values and the word "of". However, the default separater of the print() is a " " (space) so we end up with white space between the value and underscore of "_of_".

>> print (value, "_of_", total, end='\r')
8 _of_ 17

The sepparator attribute, sep, can be used to set character between printed items. In my example, I will change it to a null string ('') to make my output suit my needs.

>> print (value, "_of_", total, sep='', end='\r')
8_of_17

Solution 4 - Python

I hacked this script using curses. Its really a ad-hoc solution I did for a fun. It does not support scrolling but I think its a good starting point if you are looking to build a live updating monitor with multiple rows on the terminal.

https://gist.github.com/tpandit/b2bc4f434ee7f5fd890e095e79283aec

Here is the main:

    if __name__ == "__main__":
        stdscr = curses.initscr()
        curses.noecho()
        curses.cbreak()
        curses.start_color()
        curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
        curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
        curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
        curses.init_pair(4, curses.COLOR_CYAN, curses.COLOR_BLACK)

        try:
            while True:
                resp = get_data()
                report_progress(get_data())
                time.sleep(60/REQUESTS_PER_MINUTE)
        finally:
            curses.echo()
            curses.nocbreak()
            curses.endwin()

Solution 5 - Python

Sounds like a job for curses. It is the most commonly used library for text-mode screen layout and management. Python has very good support for curses, including support for panels:

Solution 6 - Python

When I do this in shell scripts on Unix, I tend to just use the clear program. You can use the Python subprocess module to execute it. It will at least get you what you're looking for quickly.

Solution 7 - Python

import time
for i in range(10):
    print('\r{}>'.format('='*i), end='')
    time.sleep(0.5)

Solution 8 - Python

I don't think that including another libraries in this situation is really good practice. So, solution:

print("\rCurrent: %s\t%s" % (str(<value>), <another_value>), end="")

Solution 9 - Python

[ignacio@localhost ~]$ ldd /usr/bin/top | grep curses
        libncursesw.so.5 => /lib64/libncursesw.so.5 (0x0000003ae1400000)

http://docs.python.org/library/curses.html">`curses`</a> it is.

Solution 10 - Python

To print static and dynamic information I used a string in loop.

from sys import stdout

for I in range(1,10):
  std_string = 'Current no. is ' + I
  stdout.write("\r%s"%std_string)
  stdout.flush()
stdout.write('\n')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRecursionView Question on Stackoverflow
Solution 1 - PythonTorsten MarekView Answer on Stackoverflow
Solution 2 - PythonPeter BrittainView Answer on Stackoverflow
Solution 3 - PythonGT ElectronicsView Answer on Stackoverflow
Solution 4 - PythonTejasView Answer on Stackoverflow
Solution 5 - PythongavinbView Answer on Stackoverflow
Solution 6 - PythonOmnifariousView Answer on Stackoverflow
Solution 7 - PythonXiang ZhaoView Answer on Stackoverflow
Solution 8 - Pythonr1v3nView Answer on Stackoverflow
Solution 9 - PythonIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 10 - PythonA BView Answer on Stackoverflow