Make Your CLI Apps Look Stunning with the Python Textual Library

The article introduces Textual, a Python library built on Rich that lets developers create modern, styled terminal user interfaces—including windows, sidebars, buttons, and animated progress bars—using CSS‑like styling, and shows how to install, build a simple counter app, run it in the terminal or browser, and discusses suitable use cases.

IT Services Circle
IT Services Circle
IT Services Circle
Make Your CLI Apps Look Stunning with the Python Textual Library

Command‑line interfaces (CLI) are simple and stable but often lack visual appeal and friendly interaction, which can be a barrier for non‑programmer users. The article presents Textual , a library built on Rich , that enables developers to craft modern, CSS‑styled terminal user interfaces (TUIs) that feel like a browser inside the terminal.

Installation

Textual is installed via the standard Python package manager:

pip install textual

Quick Start Example

A minimal counter application demonstrates the core workflow. The code imports the necessary classes, defines a CSS block for styling, composes the UI layout, and handles button events.

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

class CounterApp(App):
    """A simple counter application"""
    CSS = """
    Digits {
        color: #00ff00;   /* green numbers */
        margin-bottom: 1; /* spacing */
        text-align: center;
        width: auto;
    }
    Button {
        width: 16;        /* fixed width */
        margin: 1;        /* breathing space */
    }
    """

    def compose(self) -> ComposeResult:
        """Define the UI components (the body)"""
        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:
        """Event handler: on_ + component name + _event name"""
        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()

The compose method builds the UI hierarchy, placing a Digits widget for the number and two Button widgets for incrementing and decrementing. The on_button_pressed method reacts to button clicks, updates the displayed number, and demonstrates the event‑driven model.

Running the Application

Executing the script launches a terminal window that supports mouse clicks, keyboard shortcuts, and dynamic updates, turning the CLI into an interactive mini‑software.

Browser Support

Textual can also render the same UI in a web browser without code changes. After installing the auxiliary package: pip install textual-dev the command textual serve your_script.py starts a local server. Opening the provided URL displays the terminal UI in the browser, useful for remote demos or lightweight operational dashboards.

Applicable Scenarios

Textual is ideal for tools that need a balance between development effort and visual quality, such as database management utilities, local file searchers, or personal ops dashboards. It is less suited for throw‑away automation scripts where speed outweighs UI polish, or for data‑intensive real‑time visualizations that demand full‑featured GUI or web frameworks.

Overall, Textual offers Python developers a middle ground between plain console scripts and heavyweight GUI/web applications, enabling attractive, cross‑platform terminal interfaces with minimal code.

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 UICSS stylingTUITextual
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.