textual
The lean application framework for Python. Build sophisticated user interfaces with a simple Python API. Run your apps in the terminal and a web browser.
Top Related Projects
A powerful little TUI framework 🏗
Rich is a Python library for rich text and beautiful formatting in the terminal.
Console user interface library for Python (official repo)
A cross platform package to do curses-like operations, plus higher level APIs and widgets to create text UIs and ASCII art animations
Library for building powerful interactive command line applications in Python
Quick Overview
Textual is a Python framework for creating sophisticated Text User Interfaces (TUIs) and terminal applications. It provides a rich set of widgets and tools to build interactive, responsive, and visually appealing console-based applications with minimal effort.
Pros
- Easy to use with a high-level API, allowing rapid development of complex TUIs
- Cross-platform compatibility, working on Windows, macOS, and Linux
- Rich set of pre-built widgets and layouts for creating diverse interfaces
- Supports modern terminal features like true color and emojis
Cons
- Limited to terminal environments, not suitable for GUI applications
- Learning curve for developers new to TUI paradigms
- May have performance limitations for extremely complex interfaces
- Dependency on third-party libraries for some advanced features
Code Examples
- Creating a simple "Hello, World!" app:
from textual.app import App, ComposeResult
from textual.widgets import Static
class HelloWorld(App):
def compose(self) -> ComposeResult:
yield Static("Hello, World!")
if __name__ == "__main__":
app = HelloWorld()
app.run()
- Adding a button with an event handler:
from textual.app import App, ComposeResult
from textual.widgets import Button
class ButtonApp(App):
def compose(self) -> ComposeResult:
yield Button("Click me!", id="my_button")
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "my_button":
self.exit(message="Button clicked!")
if __name__ == "__main__":
app = ButtonApp()
app.run()
- Creating a simple form with input fields:
from textual.app import App, ComposeResult
from textual.widgets import Input, Button
class FormApp(App):
def compose(self) -> ComposeResult:
yield Input(placeholder="Enter your name")
yield Input(placeholder="Enter your email", id="email")
yield Button("Submit", id="submit")
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "submit":
email = self.query_one("#email", Input).value
self.exit(message=f"Form submitted with email: {email}")
if __name__ == "__main__":
app = FormApp()
app.run()
Getting Started
To get started with Textual, follow these steps:
-
Install Textual using pip:
pip install textual
-
Create a new Python file (e.g.,
my_app.py
) and import the necessary modules:from textual.app import App, ComposeResult from textual.widgets import Static
-
Define your app class and compose method:
class MyApp(App): def compose(self) -> ComposeResult: yield Static("Welcome to my Textual app!") if __name__ == "__main__": app = MyApp() app.run()
-
Run your app:
python my_app.py
This will create a simple Textual app with a static text widget. You can build upon this foundation to create more complex and interactive TUIs.
Competitor Comparisons
A powerful little TUI framework 🏗
Pros of Bubbletea
- Written in Go, offering better performance and easier deployment
- Simpler API with a focus on composability and functional programming
- Lightweight and minimal dependencies
Cons of Bubbletea
- Less feature-rich compared to Textual's extensive widget library
- Limited styling options and layout control
- Steeper learning curve for developers not familiar with Go
Code Comparison
Bubbletea (Go):
func (m model) View() string {
return fmt.Sprintf("Hello, %s!", m.name)
}
Textual (Python):
class HelloWorld(App):
def compose(self) -> ComposeResult:
yield Label(f"Hello, {self.name}!")
Both frameworks provide a declarative approach to building TUIs, but Textual offers a more Pythonic syntax and richer set of built-in widgets. Bubbletea's simplicity and performance make it attractive for Go developers, while Textual's extensive features and Python ecosystem integration appeal to those familiar with Python.
Bubbletea excels in scenarios requiring high performance and minimal resource usage, whereas Textual shines in complex applications with rich user interfaces and rapid prototyping needs.
Rich is a Python library for rich text and beautiful formatting in the terminal.
Pros of Rich
- Simpler to use for basic text formatting and styling
- Lighter weight and faster for simple console output
- More focused on console output rather than full TUI applications
Cons of Rich
- Less suitable for creating complex, interactive TUI applications
- Limited layout and widget capabilities compared to Textual
- Lacks built-in support for user input and event handling
Code Comparison
Rich:
from rich import print
print("[bold red]Hello[/bold red] [green]World[/green]!")
Textual:
from textual.app import App, ComposeResult
from textual.widgets import Label
class HelloWorld(App):
def compose(self) -> ComposeResult:
yield Label("Hello World!")
HelloWorld().run()
Rich is more straightforward for simple console output, while Textual provides a framework for building complete TUI applications with widgets and layouts. Rich focuses on enhancing console output, whereas Textual offers a full-fledged TUI development environment with more advanced features and interactivity.
Console user interface library for Python (official repo)
Pros of urwid
- Mature and stable library with a long history of development
- Lightweight and efficient, suitable for resource-constrained environments
- Extensive widget set and customization options
Cons of urwid
- Steeper learning curve due to lower-level API
- Less modern and intuitive syntax compared to newer libraries
- Limited documentation and community resources
Code Comparison
urwid:
import urwid
def exit_on_q(key):
if key in ('q', 'Q'):
raise urwid.ExitMainLoop()
main = urwid.Padding(urwid.Text(u"Hello World"), left=2, right=2)
top = urwid.Overlay(main, urwid.SolidFill(u'\N{MEDIUM SHADE}'),
align='center', width=('relative', 60),
valign='middle', height=('relative', 60),
min_width=20, min_height=9)
urwid.MainLoop(top, unhandled_input=exit_on_q).run()
Textual:
from textual.app import App, ComposeResult
from textual.widgets import Header, Footer
class HelloWorld(App):
def compose(self) -> ComposeResult:
yield Header()
yield Footer()
def on_mount(self) -> None:
self.sub_title = "Hello World!"
if __name__ == "__main__":
app = HelloWorld()
app.run()
The code comparison shows that Textual offers a more modern, declarative approach to building TUIs, while urwid requires a more imperative style. Textual's syntax is generally more concise and easier to read for newcomers to TUI development.
A cross platform package to do curses-like operations, plus higher level APIs and widgets to create text UIs and ASCII art animations
Pros of asciimatics
- More mature and stable project with a longer history
- Supports a wider range of terminal types and platforms
- Includes built-in animation and special effects capabilities
Cons of asciimatics
- Less active development and community support
- More complex API, potentially steeper learning curve
- Limited modern UI components compared to Textual
Code Comparison
asciimatics:
from asciimatics.screen import Screen
from asciimatics.scene import Scene
from asciimatics.effects import Cycle, Stars
from asciimatics.renderers import FigletText
def demo(screen):
effects = [
Cycle(
screen,
FigletText("ASCIIMATICS", font='big'),
int(screen.height / 2 - 8)),
Stars(screen, 200)
]
screen.play([Scene(effects, 500)])
Screen.wrapper(demo)
Textual:
from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, Button
class MyApp(App):
def compose(self) -> ComposeResult:
yield Header()
yield Button("Click me!")
yield Footer()
if __name__ == "__main__":
app = MyApp()
app.run()
Library for building powerful interactive command line applications in Python
Pros of python-prompt-toolkit
- Focused on building interactive command-line applications
- Extensive features for command-line interfaces (auto-completion, syntax highlighting, etc.)
- Lightweight and can be easily integrated into existing projects
Cons of python-prompt-toolkit
- Limited to terminal-based interfaces
- Less suitable for creating full-screen applications or complex layouts
- Steeper learning curve for advanced features
Code Comparison
python-prompt-toolkit:
from prompt_toolkit import prompt
user_input = prompt('Enter your name: ')
print(f"Hello, {user_input}!")
Textual:
from textual.app import App
from textual.widgets import Input
class NameApp(App):
def compose(self):
yield Input(placeholder="Enter your name")
NameApp().run()
Summary
python-prompt-toolkit excels in creating interactive command-line interfaces with advanced features like auto-completion and syntax highlighting. It's lightweight and easily integrable but limited to terminal-based applications. Textual, on the other hand, is designed for building full-screen TUI applications with complex layouts and widgets, offering a more comprehensive framework for text-based user interfaces. The code comparison illustrates the difference in approach, with python-prompt-toolkit focusing on simple prompts and Textual providing a more structured, widget-based system for user input.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Textual
Textual is a Rapid Application Development framework for Python.
Build sophisticated user interfaces with a simple Python API. Run your apps in the terminal and a web browser!
ð¬ Demonstration
A quick run through of some Textual features.
https://user-images.githubusercontent.com/554369/197355913-65d3c125-493d-4c05-a590-5311f16c40ff.mov
About
Textual adds interactivity to Rich with an API inspired by modern web development.
On modern terminal software (installed by default on most systems), Textual apps can use 16.7 million colors with mouse support and smooth flicker-free animation. A powerful layout engine and re-usable components makes it possible to build apps that rival the desktop and web experience.
Compatibility
Textual runs on Linux, macOS, and Windows. Textual requires Python 3.8 or above.
Installing
Install Textual via pip:
pip install textual
If you plan on developing Textual apps, you should also install the development tools with the following command:
pip install textual-dev
See the docs if you need help getting started.
Demo
Run the following command to see a little of what Textual can do:
python -m textual
Documentation
Head over to the Textual documentation to start building!
Join us on Discord
Join the Textual developers and community on our Discord Server.
Examples
The Textual repository comes with a number of examples you can experiment with or use as a template for your own projects.
ð¬ Code browser
This is the code_browser.py example which clocks in at 61 lines (including docstrings and blank lines).
https://user-images.githubusercontent.com/554369/197188237-88d3f7e4-4e5f-40b5-b996-c47b19ee2f49.mov
ð¬ Stopwatch
This is the Stopwatch example from the tutorial.
https://user-images.githubusercontent.com/554369/197360718-0c834ef5-6285-4d37-85cf-23eed4aa56c5.mov
Reference commands
The textual
command has a few sub-commands to preview Textual styles.
ð¬ Easing reference
This is the easing reference which demonstrates the easing parameter on animation, with both movement and opacity. You can run it with the following command:
textual easing
https://user-images.githubusercontent.com/554369/196157100-352852a6-2b09-4dc8-a888-55b53570aff9.mov
ð¬ Borders reference
This is the borders reference which demonstrates some of the borders styles in Textual. You can run it with the following command:
textual borders
https://user-images.githubusercontent.com/554369/196158235-4b45fb78-053d-4fd5-b285-e09b4f1c67a8.mov
ð¬ Colors reference
This is a reference for Textual's color design system.
textual colors
https://user-images.githubusercontent.com/554369/197357417-2d407aac-8969-44d3-8250-eea45df79d57.mov
Top Related Projects
A powerful little TUI framework 🏗
Rich is a Python library for rich text and beautiful formatting in the terminal.
Console user interface library for Python (official repo)
A cross platform package to do curses-like operations, plus higher level APIs and widgets to create text UIs and ASCII art animations
Library for building powerful interactive command line applications in Python
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot