python动态终端打印
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

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.

#0

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.

#1

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.

#2

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

#3

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:

#4

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

#5

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.

推荐文章

将json转换为C#数组?

将json转换为C#数组?

推荐文章

无法从谷歌地图获取touchevent

无法从谷歌地图获取touchevent

推荐文章

在字符串中生成字符组合并不完全有效,为什么?

在字符串中生成字符组合并不完全有效,为什么?

推荐文章

另一篇关于MVC责任的帖子,谁应该知道?

另一篇关于MVC责任的帖子,谁应该知道?

推荐文章

控制Facebook oauth登录体验

控制Facebook oauth登录体验

推荐文章

控制文本装饰上的下划线位置:下划线

控制文本装饰上的下划线位置:下划线

推荐文章

WPF文本编辑元素中插入符号的可视化移动

WPF文本编辑元素中插入符号的可视化移动

推荐文章

在Windows之间共享计时器/变量

在Windows之间共享计时器/变量

推荐文章

再次添加数组的每个成员,紧跟其后

再次添加数组的每个成员,紧跟其后

推荐文章

轨道STI布线

轨道STI布线

推荐文章

从远程服务器读取证书哈希

从远程服务器读取证书哈希

推荐文章

我怎样才能使一个更高的div环绕在短div底部的较短div周围?

我怎样才能使一个更高的div环绕在短div底部的较短div周围?

推荐文章

论代码的清晰性和功能性

论代码的清晰性和功能性

推荐文章

需要更新数组中的值,但保留原始值才能再次更新它

需要更新数组中的值,但保留原始值才能再次更新它

推荐文章

使用JavaScript读取CSS值

使用JavaScript读取CSS值

推荐文章

为什么不能在触发器中添加datetime石英网?

为什么不能在触发器中添加datetime石英网?