Skip to content

CLI to TUI

Yapx offers experimental support for displaying a Textual User Interface (TUI) of your CLI.

It does this using argparse-tui, my fork of the Textualize Trogon library. To use this experimental feature:

pip install yapx[tui]

Yapx will detect the module and add a --tui flag to your CLI.

Example

To create a functional app with a CLI and TUI, write this script to a file:

example-tui.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python3

import yapx
from yapx.types import Literal


def setup(log_level: Literal["error", "info", "debug"] = "info"):
    print(f"Log level: {log_level}")


def _say_greeting(greeting, name, uppercase=False):
    msg = f"{greeting} {name}!"
    if uppercase:
        msg = msg.upper()
    print(msg)


def say_hello(name, uppercase=False):
    _say_greeting(greeting="Hello", name=name, uppercase=uppercase)


def say_goodbye(name, uppercase=False):
    _say_greeting(greeting="Goodbye", name=name, uppercase=uppercase)


yapx.run(setup, [say_hello, say_goodbye])

Then, make the script executable:

chmod +x ./example-tui.py

Now invoke the script:

./example-tui.py --tui

If you want the TUI to open when no arguments are provided:

yapx.run(
    setup,
    [say_hello, say_goodbye],
    default_args=["--tui"],
)

Now you can view the TUI without giving any args:

./example-tui.py

By default, the TUI is invoked with the parameter --tui. To use a command instead of a parameter:

yapx.run(
    setup,
    [say_hello, say_goodbye],
    tui_flags="tui",
    default_args=["tui"],
)

An example TUI could look like:

gif source: github.com/Textualize/trogon