使用 Python Rich 在布局内获取提示

Getting a Prompt inside a Layout using Python Rich

是否可以使用提示 布局元素中使用 Python Rich 获取用户输入?

我的目标是使用 Rich 的 Layout 构建具有 4 个窗格的 full-screen window。前 3 个包含标题、成分和方法,效果很好,但我希望底部包含用户输入提示。

期望输出:

用户输入的文本出现在布局的底部面板内。

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                                                                                   │
│ Chocolate cheesecake                                                                                              │
│                                                                                                                   │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────── 'ingredients' (58 x 7) ────────────────┐┌─────────────────── 'method' (59 x 7) ───────────────────┐
│                                                        ││                                                         │
│                                                        ││                                                         │
│               Layout(name='ingredients')               ││                  Layout(name='method')                  │
│                                                        ││                                                         │
│                                                        ││                                                         │
└────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────── Search for a recipe ───────────────────────────────────────────────┐
│                                                                                                                   │
│  > :                                                                                                              │
│                                                                                                                   │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

我的尝试:

from rich import print
from rich.panel import Panel
from rich.layout import Layout
from rich.prompt import Prompt

def rich_ui():
    while True:
        layout = Layout()
        layout.split_column(
            Layout(name="banner"),
            Layout(name="recipe"),
            Layout(name="search")
        )

        layout['banner'].update(Panel('Chocolate cheesecake', padding=1))
        layout['banner'].size = 5

        layout['recipe'].split_row(
            Layout(name="ingredients"),
            Layout(name="method")
        )

        layout['search'].update(Panel(Prompt.ask('> '), title='Search for a recipe'))
        layout['search'].size = 5
        print(layout)

if __name__ == '__main__':
    rich_ui()

实际输出:

注意提示的 >: 在布局部分之外。

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                                                                                   │
│ Chocolate cheesecake                                                                                              │
│                                                                                                                   │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────── 'ingredients' (58 x 7) ────────────────┐┌─────────────────── 'method' (59 x 7) ───────────────────┐
│                                                        ││                                                         │
│                                                        ││                                                         │
│               Layout(name='ingredients')               ││                  Layout(name='method')                  │
│                                                        ││                                                         │
│                                                        ││                                                         │
└────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────── Search for a recipe ───────────────────────────────────────────────┐
│                                                                                                                   │
│                                                                                                                   │
│                                                                                                                   │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
> :

这是可能的,但 Rich 没有执行此操作的内置方法。您必须捕获键,可能使用 keyboard 模块(在 PyPi 上),并相应地更新布局。