Skip to main content

A simple and robust terminal UI library, written in Python.

Project description

title

A simple yet powerful TUI framework for your Python (3.9+) applications

pip3 install pytermgui

PyPI version Pylint quality

Core principles

PTG was written with some core ideas in mind, such as:

  • Pythonic syntax
  • Flexible systems
  • Extensibility by design
  • Readable, reproducible code
from pytermgui import Container, Label
root = Container() + Label("Clean code is [bold]cool[/bold]!")
root.print()

What we provide

>>> from pytermgui import optimize_ansi
>>> original = "\x1b[0m\x1b[0m\x1b[0m\x1b[38;5;141m\x1b[38;5;65m\x1b[0m\x1b[1mSee the difference?\x1b[0m"
>>> optimize_ansi(original)

'\x1b[1mSee the difference?\x1b[0m'

Example to get started with

from pytermgui import Container, Label, InputField, MarkupFormatter, getch, alt_buffer, boxes

border_corner_markup = MarkupFormatter("[60 bold]{item}")
Container.set_style("border", border_corner_markup)
Container.set_style("corner", border_corner_markup)
boxes.SINGLE.set_chars_of(Container)

root = Container()
root.forced_width = 70

boxes.DOUBLE_TOP.set_chars_of(root)
root += Label("[210 bold] Welcome to [italic]PyTermGUI!", align=Label.ALIGN_LEFT)
root += Label()

field = InputField("Enter something!")
field.set_style("value", MarkupFormatter("[italic 72]{item}"))
field.set_style("cursor", MarkupFormatter("[@72]{item}"))

field_container = Container(vert_align=Container.VERT_ALIGN_TOP) + field
field_container.forced_height = 7
root += field_container

root += Label("[245 italic]> Press CTRL_C to exit...", align=Label.ALIGN_RIGHT)

root.focus()

with alt_buffer(cursor=False):
    root.center()
    root.print()

    while True:
        key = getch(interrupts=False)

        if key == chr(3):
            break

        field.send(key)
        root.center()
        root.print()

print("Goodbye!")

readme

Some screenshots

hello_world bezo calc

Why the long nose version requirement?

PyTermGUI makes heavy use of the typing module, which in Python 3.9 saw the inclusion of container parameterizing, allowing the use of list[str] instead of List[str].

The previous method is now deprecated, and there isn't any nice way of supporting both forever. As such, the use of the new syntax makes Python 3.8x and lower raise SyntaxError.

There isn't really much to help this issue, and its good practice to stay on the most recent release regardless.

from pytermgui import Container, Prompt, Label, boxes
boxes.EMPTY_VERTICAL.set_chars_of(Container)

def dict_to_container(data: dict[str, str]) -> Container:
    """Create container from a dict"""

    root = Container()
    for key, value in data.items():
        root += Prompt(key, value)

    return root

root = Container()
root += Label("[246 italic bold] a guide on the python version you should use")

root += dict_to_container({
    "[157]python >3.9": "[157]good",
    "[210]python <3.8": "[210]bad",
})
root.print()

guide

Documentation

As the project is in its infancy, dedicated documentation is not yet available.

If you are interested in help about anything the module provides, you can read its docstring:

python3 -c "help(<name>)"

However, proper documentation is coming soon.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytermgui-0.1.2.tar.gz (38.1 kB view details)

Uploaded Source

File details

Details for the file pytermgui-0.1.2.tar.gz.

File metadata

  • Download URL: pytermgui-0.1.2.tar.gz
  • Upload date:
  • Size: 38.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.5

File hashes

Hashes for pytermgui-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a387bc044e3aa5066788031ae33386485ef884c5e4b63a483bd0c7384281f257
MD5 4409b2fa8f0ba419f74ca3a286d1f59a
BLAKE2b-256 3754ec37c07d7e038d9a05eaf637d4981aee9283d9d417841442611ec0da17c5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page