Skip to main content

cui-py

Cython bindings for CUI, an immediate-mode UI library in C99 with about 190 components. The distribution is cui-py; the import name is cui.

CUI 1.5.0 is vendored in thirdparty/cui and compiled into the extension, so there are no runtime dependencies. Linux needs the X11 development headers to build (libx11-dev).

Status: alpha. Tested on macOS (Cocoa, arm64, CPython 3.14). The CI workflows build macos, Linux and Windows.

import cui

clicks = 0

def frame(ui: cui.Context) -> None:
    global clicks
    with ui.column(pad=32, gap=16):
        ui.heading("Hello, CUI", 2)
        if ui.button("Click me", variant=cui.BUTTON_PRIMARY):
            clicks += 1
        ui.text(f"Clicked {clicks} times")

cui.App("Hello", 420, 300).run(frame)

examples/ has hello.py, todo.py (a port of CUI's tutorial app), dashboard.py, notepad.py (a text editor with a menu bar), tictactoe.py (menus and shortcuts from one action table) and showcase.py (forms, charts, a node graph, a live Markdown editor and a console; --png DIR saves each page headless).

Install

cui-py needs CPython 3.11 or later. One abi3 wheel per platform covers every later version. The macOS wheels need macOS 13 or later.

pip install cui-py

or if you want to build from source:

git clone https://github.com/shakfu/cui-py && cd cui-py
uv sync          # builds the extension
make test

How the C API maps to Python

  • Names. cui_button_ex is ui.button. CUI_BUTTON_PRIMARY is cui.BUTTON_PRIMARY. Icons are strings: cui.ICON_PLUS.

  • Options. The fields of a C options struct become keyword-only arguments. An argument you leave out keeps CUI's default.

  • Outputs. A pointer the component writes to becomes a return value, after the component's own result:

    changed, value = ui.checkbox("Remember me", value)
    changed, index = ui.select("fruit", ["apple", "pear"], index)
    
  • Option pointers. An option C reads and writes takes a cui.Ref. Read .value after the frame, since some components write it at the end of a scope:

    sel = cui.Ref(-1)
    ui.treemap("t", items, selected=sel)
    if sel.value >= 0: ...
    

    An option array takes a list. cui-py sets its count from the list, or checks the list against the length CUI reads, such as one icon per segment.

  • Text fields. A text field edits a cui.TextBuffer(capacity, value) in place. Capacity counts bytes, including the trailing NUL, as in C.

  • Log and terminal. ui.log_view reads a cui.LogBuffer and ui.terminal a cui.TerminalBuffer. Both are rings that drop the oldest lines when full, and any thread may write to them:

    log = cui.LogBuffer(lines=4096)
    log.append("disk almost full", cui.LOG_WARN)    # from any thread
    ui.log_view("log", log, timestamps=True)
    
    r = ui.terminal("term", term)
    if r.command is not None:
        term.write(f"\x1b[32mran\x1b[0m {r.command}\n")
    

    A write made after the view in the same frame shows from the next frame. CUI cannot be woken from another thread, so call ui.request_wakeup(seconds) while a thread is writing.

  • Lists. A list of strings or floats replaces a C array and its count. A list of records replaces an array of structs. Each record can be a namedtuple (cui.VizItem(label="a", value=3)), a tuple, or a dict. Missing fields are zero.

  • Containers. A begin/end pair is a context manager. When begin can decline, as a closed dialog does, the scope is falsy and end is skipped:

    with ui.dialog("Settings", open) as d:
        if d:
            ui.text("inside")
    open = d.open
    

    The scope exposes the pair's outputs as attributes (d.open, vl.first). The value end returns is in .result.

  • Values.

    • Colors: 0xRRGGBB, (r, g, b[, a]), cui.rgb(), or theme tokens such as cui.COLOR_ACCENT.

    • Sizes: a number in points, cui.FILL, cui.pct(50) or cui.grow(2).

    • Dates: datetime.date, or None for unset.

  • Images. image_create and image_update take any C-contiguous buffer of RGBA bytes: bytes, memoryview, or a NumPy (h, w, 4) uint8 array.

  • Drawing. ui.canvas(id, width, height, paint) calls paint(rect) after layout. Only there do the draw_* methods work. focusable=True makes it a Tab stop. With component_input=True it also receives keys and text through ctx.dispatch_input(event, route), replaying events an input hook (ctx.set_input_hook) captured.

cui.Context() with no window renders headless with CUI's software renderer. Its parts:

  • input arrives through ui.input_*

  • render() and save_png() read the pixels

  • copy and paste use ctx.clipboard

The tests use this.

Terminal on a pty

cui.Pty runs a child process on a pseudo-terminal, and ui.pty(id, p) shows it. libghostty-vt 1.0.0 parses the output. The Linux and macOS wheels include it. Windows has no pty, and cui.PTY_AVAILABLE is false there. The terminal code is in csrc/term/ and its C tests in tests/c/; docs/dev/terminal.md covers its design.

A source build leaves it off. To turn it on you need Zig 0.17 and CMake 3.19 or later:

make build-pty                                           # from a checkout
pip install cui-py --no-binary cui-py -C cmake.define.CUI_PY_PTY=ON

The first build downloads libghostty-vt (SHA-256 pinned in CMakeLists.txt).

with cui.Pty(["python3", "-q"]) as p:
    for ui in app.frames():
        ui.pty("repl", p)
        if p.exited:
            break
  • The view reads the child's output each frame. A Pty no view shows stalls a child that writes a lot.
  • p.write() sends input, and p.screen() reads the screen as text.
  • A Pty works only on the thread that made it.

examples/term.py is a terminal window. examples/ide.py puts one in a panel beside a file tree and editors.

Errors instead of crashes

Misuse that would be undefined behaviour in C raises an exception:

  • building outside a frame

  • building inside a paint callback

  • drawing outside one

  • ending a frame with a scope open

  • using a closed context

  • passing strings with NUL characters

  • passing arrays of unequal length, or of a length CUI would read past

  • building UI or injecting input inside an input hook or route

  • updating a region outside an image, or a texture that is not one

  • passing color channels outside 0..255

  • writing more text than a buffer holds

Strings, arrays and Refs passed to CUI stay alive until the frame is painted.

The vendored CUI carries one patch, for state shared by two component types with one id. A narrower case remains; see docs/dev/known-issues.md.

Coverage

The generator wraps 421 entries: 403 Context methods, including 53 scopes, and 18 module functions. Another 48 functions are wrapped by hand. docs/dev/coverage.md lists the 34 functions not wrapped yet and why, and the 12 options that have no keyword argument.

Development

tools/gen_bindings.py reads CUI's headers with libclang and writes these files:

  • src/cui/_cui.pxd

  • src/cui/_gen.pxi

  • src/cui/_core.pyi

  • docs/dev/coverage.md

The output is committed. Run make gen after updating thirdparty/cui. Hand-written code lives in src/cui/_core.pyx.

Target
make build rebuild the extension
make build-pty rebuild with cui.Pty (needs Zig 0.17)
make test headless tests, including one call to every generated method
make test-window also open a hidden window
make test-asan tests under AddressSanitizer (macOS)
make test-c C tests of the terminal code in csrc/term/ (needs Zig 0.17)
make qa lint, format check, mypy, tests

License

MIT. The vendored CUI is MIT, with its fonts under OFL (Inter, Geist) and ISC (Lucide). See thirdparty/cui/LICENSE, thirdparty/cui/THIRD_PARTY.md and thirdparty/cui/assets/fonts/. A CUI_PY_PTY build also links libghostty-vt, MIT. Its license goes in cui/licenses/.

Metadata

Release files for cui-py 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cui-py 0.3.0
File Size Uploaded Provenance Actions
cui_py-0.3.0.tar.gz 1.5 MB Without provenance Details for cui_py-0.3.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for cui-py 0.3.0
File Actions
cui_py-0.3.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for cui_py-0.3.0-cp311-abi3-win_amd64.whl
cui_py-0.3.0-cp311-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.11 abi3 Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 Details for cui_py-0.3.0-cp311-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
cui_py-0.3.0-cp311-abi3-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl CPython 3.11 abi3 Linux glibc 2.27+ ARM64, Linux glibc 2.28+ ARM64 Details for cui_py-0.3.0-cp311-abi3-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
cui_py-0.3.0-cp311-abi3-macosx_13_0_x86_64.whl CPython 3.11 abi3 macOS 13.0+ x86-64 Details for cui_py-0.3.0-cp311-abi3-macosx_13_0_x86_64.whl
cui_py-0.3.0-cp311-abi3-macosx_13_0_arm64.whl CPython 3.11 abi3 macOS 13.0+ ARM64 Details for cui_py-0.3.0-cp311-abi3-macosx_13_0_arm64.whl

Total release size: 15.2 MB

Release files / cui_py-0.3.0.tar.gz

Download URL cui_py-0.3.0.tar.gz
Size 1.5 MB
Tags Source
SHA-256 checksum
How to use checksums
33c8910f02a1c1ab1ffac1826921b3b8d062f55752f41fcf9e9bebaf990514ba
BLAKE2b-256 checksum
How to use checksums
e6ede6999238d8e814fcc6fcd66a469133b8c3c2f0b8b663e234fd1d604ca261
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / cui_py-0.3.0-cp311-abi3-win_amd64.whl

Download URL cui_py-0.3.0-cp311-abi3-win_amd64.whl
Size 1.9 MB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
065b58299d1e0c948d01ab5af9780858aaff11e08380ead063bca559ac5166d1
BLAKE2b-256 checksum
How to use checksums
5a36f1c7afe5b842c4e3f74aa6c5e1d33f7556ed68a93674b9e0cb80792259a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / cui_py-0.3.0-cp311-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL cui_py-0.3.0-cp311-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 3.2 MB
Tags CPython 3.11 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
6987378f92056459bf9409548152c7b6159c9d8603a09f56f55b959d1c7b7227
BLAKE2b-256 checksum
How to use checksums
f8f57583988613a268eb7680445cb5d3b64126faa783abe655adfa333eb18889
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / cui_py-0.3.0-cp311-abi3-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl

Download URL cui_py-0.3.0-cp311-abi3-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Size 3.0 MB
Tags CPython 3.11 Linux glibc 2.27+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
d988fd09bbda343af1874b463685e7885cc700407741701ceda293b4ecc5acb6
BLAKE2b-256 checksum
How to use checksums
94c8b80fa5158412a4253b36b4aedcb0a92cb3fd4b5137f8c96cee77fed1ea23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / cui_py-0.3.0-cp311-abi3-macosx_13_0_x86_64.whl

Download URL cui_py-0.3.0-cp311-abi3-macosx_13_0_x86_64.whl
Size 2.9 MB
Tags CPython 3.11 abi3 macOS 13.0+ x86-64
SHA-256 checksum
How to use checksums
2aa16450a2a75ff75b529e96cde2247c2bd71bb09a9f91cef687e65b032a38d7
BLAKE2b-256 checksum
How to use checksums
060a508c6a9f36f454f9aa2d9d8d7a78cfc738341be545e6843cb0fb0ca7cc4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / cui_py-0.3.0-cp311-abi3-macosx_13_0_arm64.whl

Download URL cui_py-0.3.0-cp311-abi3-macosx_13_0_arm64.whl
Size 2.7 MB
Tags CPython 3.11 abi3 macOS 13.0+ ARM64
SHA-256 checksum
How to use checksums
6bdd1638e75acdd8a704d46c71830fabd6c3bec82033c23a7536cb36cadb3816
BLAKE2b-256 checksum
How to use checksums
539410bb36a3448ebdba085993daf93193520a2db6cbd7b7d4962b7f0d4b7fff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for cui-py 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page