A Python terminal enhanced open source library

49783a3a8daffa5d21001e761bbf5939.png

Lost little book boy

Needed after reading

4

minute

Speed ​​reading only takes 2 minutes

1

   

Introduction

rich is an open source library developed by Will McGugan to provide better terminal text rendering and style handling capabilities. It provides rich text formatting options, including color, bold, italics, underline, alignment, etc. rich is not only suitable for beautifying the command line interface, but can also be used to generate rich text reports, logging, terminal UI, etc.

2

   

Implementation principle

Rich controls the style of terminal output by using ANSI escape codes. ANSI escape codes are special character sequences used to display color, style, and formatting on the terminal, and rich uses these escape codes to achieve text highlighting, coloring, and styling effects.

3

   

Install

Use the following command to install

pip install rich

After the installation is complete, you can simply test it

python -m rich

2b21e33314439a77e5a360e2ba74a569.jpeg

4

   

Sample code

Below is a simple example showing the stylizing effect of the rich library on terminal output.

from rich import print


print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals())

This code will output styled text in the terminal. The effect of program execution is as follows

193cfc3c7cd4b33658040f5a8be51666.jpeg

Let’s look at an example of displaying a progress bar:

from time import sleep
from urllib.request import urlopen


from rich.progress import wrap_file


# 访问外部链接
response = urlopen("https://www.textualize.io")


# 获取大小
size = int(response.headers["Content-Length"])


# 获取文件并显示进度条
with wrap_file(response, size) as file:
    for line in file:
        print(line.decode("utf-8"), end="")
        sleep(0.1)

The result of program execution

f79343003e0b6b064cf96fc1aa98a1c4.jpeg

Finally, again

from random import randint


from rich import print
from rich.highlighter import Highlighter


# 定义Highlighter的子类,重写highlight方法
class RainbowHighlighter(Highlighter):
    def highlight(self, text):
        for index in range(len(text)):
            text.stylize(f"color({randint(16, 255)})", index, index + 1)




rainbow = RainbowHighlighter()
print(rainbow("I must not fear. Fear is the mind-killer."))

The highlight method uses randint to randomly generate color numbers, and stylize to apply a different color to each character.

The effect of the above code

050a7433981bc0855440310b58c0b745.jpeg

5

   

Summarize

Through the above introduction and examples, I hope you have a basic understanding of the Python open source library rich. rich provides powerful text rendering and style processing functions. Its easy-to-use API and rich documentation make it very convenient to use and integrate. For more usage tips and application examples, please refer to the official documentation.

6

   

References

  • https://github.com/Textualize/rich ( https://github.com/Textualize/rich )

  • https://link.zhihu.com/?target=https%3A//handwiki.org/wiki/ANSI_escape_code ( https://link.zhihu.com/?target=https%3A//handwiki.org/wiki/ANSI_escape_code )

7

   

free community

249bb48cf8e931783baa2ff53eeed586.jpeg

e245bc82de981e55cf655fa0a9861de9.gif

Guess you like

Origin blog.csdn.net/djstavaV/article/details/132893522