python的rich终端显示

Rich

从0开始学python,发现Rich这个库,蛮有意思的,推荐给大家看看。

Rich 是一个 Python 库,可以为您在终端中提供富文本和精美格式。

Rich API 可以很容易的在终端输出添加各种颜色和不同风格。Rich 还可以绘制漂亮的表格,进度条,markdown,突出显示语法的源代码及回溯等等,不胜枚举。

兼容性

Rich 适用于 Linux,OSX 和 Windows。真彩色/表情符号可与新的 Windows 终端一起使用,Windows 的经典终端仅限 8 种颜色。

Rich 还可以与Jupyter 笔记本一起使用,而无需其他配置。

安装说明

使用pip或其他 PyPi 软件包管理器进行安装。

pip install rich
!git clone https://gitee.com/livingbody/rich.git --depth=1
Receiving objects:  41% (114/276), 1.49 MiB | 375.00 KiB/s   
remote: Total 276 (delta 1), reused 86 (delta 0), pack-reused 0[K
Receiving objects: 100% (276/276), 13.42 MiB | 253.00 KiB/s, done.
Resolving deltas: 100% (1/1), done.
Checking connectivity... done.
# rich安装
!pip install rich

1.漂亮的tree

import os
import pathlib
import sys

from rich import print
from rich.filesize import decimal
from rich.markup import escape
from rich.text import Text
from rich.tree import Tree


def walk_directory(directory: pathlib.Path, tree: Tree) -> None:
    """Recursively build a Tree with directory contents."""
    # Sort dirs first then by filename
    paths = sorted(
        pathlib.Path(directory).iterdir(),
        key=lambda path: (path.is_file(), path.name.lower()),
    )
    for path in paths:
        # Remove hidden files
        if path.name.startswith("."):
            continue
        if path.is_dir():
            style = "dim" if path.name.startswith("__") else ""
            branch = tree.add(
                f"[bold magenta]:open_file_folder: [link file://{path}]{escape(path.name)}",
                style=style,
                guide_style=style,
            )
            walk_directory(path, branch)
        else:
            text_filename = Text(path.name, "green")
            text_filename.highlight_regex(r"\..*$", "bold red")
            text_filename.stylize(f"link file://{path}")
            file_size = path.stat().st_size
            text_filename.append(f" ({decimal(file_size)})", "blue")
            icon = "🐍 " if path.suffix == ".py" else "📄 "
            tree.add(Text(icon) + text_filename)


try:
    directory = os.path.abspath(".")
except IndexError:
    print("[b]Usage:[/] python tree.py <DIRECTORY>")
else:
    tree = Tree(
        f":open_file_folder: [link file://{directory}]{directory}",
        guide_style="bold bright_blue",
    )
    walk_directory(pathlib.Path(directory), tree)
    print(tree)

📂 /home/aistudio                                                                            
┣━━ 📂 data                                                                                  
┣━━ 📂 rich                                                                                  
┃   ┣━━ 📂 docs                                                                              
┃   ┃   ┣━━ 📂 source                                                                        
┃   ┃   ┃   ┣━━ 📂 appendix                                                                  
┃   ┃   ┃   ┃   ┣━━ 📄 box.rst (14.0 kB)                                                     
┃   ┃   ┃   ┃   ┗━━ 📄 colors.rst (59.0 kB)                                                  
┃   ┃   ┃   ┣━━ 📂 reference                                                                 
┃   ┃   ┃   ┃   ┣━━ 📄 abc.rst (60 bytes)                                                    
┃   ┃   ┃   ┃   ┣━━ 📄 align.rst (66 bytes)                                                  
┃   ┃   ┃   ┃   ┣━━ 📄 bar.rst (60 bytes)                                                    
┃   ┃   ┃   ┃   ┣━━ 📄 color.rst (66 bytes)                                                  
┃   ┃   ┃   ┃   ┣━━ 📄 columns.rst (72 bytes)                                                
┃   ┃   ┃   ┃   ┣━━ 📄 console.rst (70 bytes)                                                
┃   ┃   ┃   ┃   ┣━━ 📄 emoji.rst (71 bytes)                                                  
┃   ┃   ┃   ┃   ┣━━ 📄 highlighter.rst (114 bytes)                                           
┃   ┃   ┃   ┃   ┣━━ 📄 init.rst (52 bytes)                                                   
┃   ┃   ┃   ┃   ┣━━ 📄 live.rst (60 bytes)                                                   
┃   ┃   ┃   ┃   ┣━━ 📄 logging.rst (97 bytes)                                                
┃   ┃   ┃   ┃   ┣━━ 📄 markdown.rst (75 bytes)                                               
┃   ┃   ┃   ┃   ┣━━ 📄 markup.rst (67 bytes)                                                 
┃   ┃   ┃   ┃   ┣━━ 📄 measure.rst (70 bytes)                                                
┃   ┃   ┃   ┃   ┣━━ 📄 padding.rst (70 bytes)                                                
┃   ┃   ┃   ┃   ┣━━ 📄 panel.rst (71 bytes)                                                  
┃   ┃   ┃   ┃   ┣━━ 📄 pretty.rst (69 bytes)                                                 
┃   ┃   ┃   ┃   ┣━━ 📄 progress.rst (73 bytes)                                               
┃   ┃   ┃   ┃   ┣━━ 📄 progress_bar.rst (87 bytes)                                           
┃   ┃   ┃   ┃   ┣━━ 📄 prompt.rst (67 bytes)                                                 
┃   ┃   ┃   ┃   ┣━━ 📄 protocol.rst (73 bytes)                                               
┃   ┃   ┃   ┃   ┣━━ 📄 rule.rst (61 bytes)                                                   
┃   ┃   ┃   ┃   ┣━━ 📄 segment.rst (70 bytes)                                                
┃   ┃   ┃   ┃   ┣━━ 📄 spinner.rst (70 bytes)                                                
┃   ┃   ┃   ┃   ┣━━ 📄 status.rst (68 bytes)                                                 
┃   ┃   ┃   ┃   ┣━━ 📄 style.rst (96 bytes)                                                  
┃   ┃   ┃   ┃   ┣━━ 📄 styled.rst (69 bytes)                                                 
┃   ┃   ┃   ┃   ┣━━ 📄 syntax.rst (74 bytes)                                                 
┃   ┃   ┃   ┃   ┣━━ 📄 table.rst (64 bytes)                                                  
┃   ┃   ┃   ┃   ┣━━ 📄 text.rst (67 bytes)                                                   
┃   ┃   ┃   ┃   ┣━━ 📄 theme.rst (71 bytes)                                                  
┃   ┃   ┃   ┃   ┣━━ 📄 traceback.rst (96 bytes)                                              
┃   ┃   ┃   ┃   ┗━━ 📄 tree.rst (62 bytes)                                                   
┃   ┃   ┃   ┣━━ 📄 appendix.rst (96 bytes)                                                   
┃   ┃   ┃   ┣━━ 📄 columns.rst (810 bytes)                                                   
┃   ┃   ┃   ┣━━ 🐍 conf.py (2.3 kB)                                                          
┃   ┃   ┃   ┣━━ 📄 console.rst (16.2 kB)                                                     
┃   ┃   ┃   ┣━━ 📄 group.rst (1.4 kB)                                                        
┃   ┃   ┃   ┣━━ 📄 highlighting.rst (3.2 kB)                                                 
┃   ┃   ┃   ┣━━ 📄 index.rst (759 bytes)                                                     
┃   ┃   ┃   ┣━━ 📄 introduction.rst (5.2 kB)                                                 
┃   ┃   ┃   ┣━━ 📄 live.rst (5.9 kB)                                                         
┃   ┃   ┃   ┣━━ 📄 logging.rst (1.7 kB)                                                      
┃   ┃   ┃   ┣━━ 📄 markdown.rst (928 bytes)                                                  
┃   ┃   ┃   ┣━━ 📄 markup.rst (3.9 kB)                                                       
┃   ┃   ┃   ┣━━ 📄 padding.rst (1.6 kB)                                                      
┃   ┃   ┃   ┣━━ 📄 panel.rst (1.0 kB)                                                        
┃   ┃   ┃   ┣━━ 📄 progress.rst (9.5 kB)                                                     
┃   ┃   ┃   ┣━━ 📄 prompt.rst (1.6 kB)                                                       
┃   ┃   ┃   ┣━━ 📄 protocol.rst (3.9 kB)                                                     
┃   ┃   ┃   ┣━━ 📄 reference.rst (847 bytes)                                                 
┃   ┃   ┃   ┣━━ 📄 style.rst (7.3 kB)                                                        
┃   ┃   ┃   ┣━━ 📄 syntax.rst (1.9 kB)                                                       
┃   ┃   ┃   ┣━━ 📄 tables.rst (5.2 kB)                                                       
┃   ┃   ┃   ┣━━ 📄 text.rst (2.6 kB)                                                         
┃   ┃   ┃   ┣━━ 📄 traceback.rst (786 bytes)                                                 
┃   ┃   ┃   ┗━━ 📄 tree.rst (2.0 kB)                                                         
┃   ┃   ┣━━ 📄 make.bat (799 bytes)                                                          
┃   ┃   ┣━━ 📄 Makefile (638 bytes)                                                          
┃   ┃   ┗━━ 📄 requirements.txt (55 bytes)                                                   
┃   ┣━━ 📂 examples                                                                          
┃   ┃   ┣━━ 🐍 bars.py (417 bytes)                                  
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值