Skip to content Skip to sidebar Skip to footer

Dynamic Terminal Printing With Python

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 fo

Solution 1:

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()

Post a Comment for "Dynamic Terminal Printing With Python"