简体   繁体   English

如何在 `rich.panel.Panel` object 中打印字典?

[英]How to print a dictionary in a `rich.panel.Panel` object?

As the question says, I want to pretty print a dictionary such as locals() but in a Panel object (surrounding the dictionary in a nice rectangle).正如问题所说,我想在面板 object 中漂亮地打印诸如locals()之类的字典(将字典围绕在一个漂亮的矩形中)。

On trying the naïve approach, ie,在尝试天真的方法时,即

from rich.panel import Panel
from rich import print

print(Panel(locals()))

I'd get NotRenderableError: Unable to render...; A str, Segment or object with __rich_console__ method is required我会得到NotRenderableError: Unable to render...; A str, Segment or object with __rich_console__ method is required NotRenderableError: Unable to render...; A str, Segment or object with __rich_console__ method is required

The best I could do was convert the dictionary into a renderable by digging the source-code.我能做的最好的就是通过挖掘源代码将字典转换为renderable的。

from rich.console import Console


def do(x):
    c = Console()
    print(Panel(c._collect_renderables(x, sep='\n', end='')))

do(globals())

But all it did was print the keys in a box但它所做的只是将钥匙打印在一个盒子里

╭───────────────╮
│ dataset_path  │                                                                     
│ layouts       │                                                                            
│ line_level    │                                                                            
│ n             │                                                                            
╰───────────────╯

Can anyone suggest the right document I must go through to accomplish this, and in general what is the right way to understand how to utilize rich to its fullest?谁能建议我必须通过 go 完成此操作的正确文件,一般来说,理解如何充分利用rich的正确方法是什么?

It turned out to be simpler than what I thought..结果比我想象的要简单..

from rich import print
from rich.panel import Panel
from rich.console import Console
from rich.text import Text


def print_box(x, title=None):
    console = Console()
    with console.capture() as capture:
        console.print(x)
    str_output = capture.get()
    text = Text.from_ansi(str_output)
    print(Panel(text, title=title, padding=2))
  1. Capture the text in a console output在控制台中捕获文本 output
  2. Convert it to Text object, and将其转换为Text object,然后
  3. Display in a Panel在面板中显示

╭───────────────────────────────────────── Inputs ──────────────────────────────────────────╮
│                                                                                           │
│                                                                                           │
│  {'dataset_path': '/tmp/', 'layouts': '0,1,2,3', 'line_level': False, 'n': None}          │
│                                                                                           │
│                                                                                           │
╰───────────────────────────────────────────────────────────────────────────────────────────╯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM