Boost Your CLI’s Look with This Powerful Python Library

The article introduces Textual, a Python library built on Rich that lets developers create modern, CSS‑styled terminal user interfaces, shows how to install it, walk through a complete counter example, and explains its ability to run in browsers for lightweight ops dashboards.

IT Services Circle
IT Services Circle
IT Services Circle
Boost Your CLI’s Look with This Powerful Python Library

Background

Traditional Python CLI tools use print and input, which are simple but lack visual appeal and friendliness for non‑programmer users. Textual, a rapidly popular open‑source project on GitHub, extends the Rich library to provide a browser‑like, widget‑rich UI inside the terminal. It includes windows, sidebars, buttons, progress bars, and supports CSS‑style sheets for visual customization.

Installation

pip install textual

Minimal Counter Application

from textual.app import App, ComposeResult
from textual.widgets import Button, Digits
from textual.containers import Center, Middle

class CounterApp(App):
    """A simple counter app inheriting from textual.app.App"""

    CSS = """
    Digits {
        color: #00ff00;
        margin-bottom: 1;
        text-align: center;
        width: auto;
    }
    Button {
        width: 16;
        margin: 1;
    }
    """

    def compose(self) -> ComposeResult:
        """Define the UI components (the body of the app)."""
        with Middle():
            with Center():
                yield Digits("0")
            with Center():
                yield Button("点我加1", variant="success", id="add")
                yield Button("点我减1", variant="error", id="sub")

    def on_button_pressed(self, event: Button.Pressed) -> None:
        """Handle button click events: update the displayed number."""
        digits = self.query_one(Digits)
        count = int(digits.value)
        if event.button.id == "add":
            digits.update(str(count + 1))
        else:
            digits.update(str(count - 1))

if __name__ == "__main__":
    app = CounterApp()
    app.run()

Key points:

Creating an App subclass and calling run() starts the UI.

The compose method adds three widgets: a Digits widget for the number display and two Button widgets for increment/decrement, positioned with Middle and Center containers.

The on_button_pressed method reacts to button clicks, checks the button id, and updates the Digits value accordingly.

Running the script turns the terminal into an interactive UI that supports mouse clicks and keyboard shortcuts.

Running in a Browser

Install the development helper to enable browser rendering: pip install textual-dev Serve the script with a single command: textual serve your_code.py The command starts a local server; opening the displayed URL in a browser shows the same terminal UI, useful for remote demos or lightweight operational dashboards.

Typical Use Cases and Limitations

Textual is well‑suited for tools that need richer interaction than plain CLI but do not justify a full GUI or web stack, such as database managers, file‑search utilities, or personal ops dashboards. It runs wherever a terminal is available, including SSH sessions, because it is cross‑platform.

For throwaway scripts focused solely on speed, or applications requiring heavy 3D graphics or massive real‑time data streams, traditional GUI frameworks or web front‑ends remain more appropriate.

Project repository: https://github.com/Textualize/textual

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLIPythonrichTerminal UITUITextual
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.