State and effects¶
Your app function runs again whenever something changes. Anything that must last between runs is asked for by name, through a hook.
ui.state¶
def counter(ui: UI) -> View:
count, set_count = ui.state(0)
return View.text(str(count)).on_click(lambda: set_count(count + 1))
ui.state(initial) returns the current value and a setter. Calling the setter marks the frame dirty, so the app runs again and count is the new value.
It is generic: ui.state(0) gives an int, ui.state(Buffer()) gives a Buffer.
Store immutable values
A new frame is scheduled only when the setter is handed something that compares unequal to what is already there. A list mutated in place and passed back is indistinguishable from no change at all. The screen stops updating.
items, set_items = ui.state(())
set_items((*items, new)) # good: a new tuple
items.append(new); set_items(items) # BAD: nothing redraws
Prefer tuples, NamedTuples and frozen dataclasses, and build a new value each time. The type parameter cannot express this, which is why it is written down here.
Declaring what a slot holds¶
The type comes from the initial value, which is occasionally too narrow:
Give it a declared constant instead:
ui.reducer¶
When the next value depends on the old one through a transition function:
def apply(model: Model, action: str) -> Model: ...
model, dispatch = ui.reducer(Model(), apply)
dispatch("start")
apply must return a new state, for the same reason as above.
ui.scope: the rule that will bite you¶
Hooks are matched between frames by the order they are called in. That works only while the order is stable. A part of the app that appears and disappears (a screen, a tab, a panel behind a toggle) shifts every hook after it, so one widget gets handed another's state.
Wrap anything conditional in its own scope:
Inside the scope, hooks get their own slots keyed by current_screen. Leaving a scope unrendered unmounts it: the state inside is dropped and the timers cancelled, like any widget that stops being drawn.
Forget it and you get:
RuntimeError: ui.every() found AppCounts in hook slot 3. Hooks are matched between
frames by call order... A part of the app that appears and disappears needs its own
slots: `with ui.scope(key): ...`
This is the single sharpest edge in turbodesk, and any app with more than one screen hits it immediately.
Effects¶
ui.every(5.0, refresh) # run every 5 seconds, sync or async
ui.task(stream_logs) # run once, the first time this hook is reached
ui.spawn(coroutine) # fire and forget
every and task are hooks, so they follow the call-order rule too. Both are cancelled when the widget that asked for them stops being rendered. A closed pane stops polling without you arranging it.
When one of them raises¶
It ends the program, the same way a crash in your app function does: the terminal is handed back and the traceback printed on the normal screen, with exit code 1. The first exception is the one reported, since a dying app tends to raise more than once on the way down.
Asyncio would behave differently on its own. Left alone, asyncio reports a failing spawn with Task exception was never retrieved, written into the alternate screen where the next frame paints over it. A failing every says nothing at all and stops ticking. Either way the app runs on with a feature missing, which is the shape of every "it just stopped updating" bug report.
If your app can live with a background failure, catch it where it happens and say so in the UI:
async def refresh() -> None:
try:
set_rows(await fetch())
except OSError as error:
ui.notify(f"Could not refresh: {error}", kind="warn")
Cancellation is not a failure: unmounting a widget cancels its timer as part of normal running.
Notices¶
A notice is a message in the corner that goes away by itself. kind is "info", "warn" or "error".
Quitting¶
run returns whatever was passed to exit, so an app that picks something can hand it back to the caller. Ctrl-C exits with None without your app doing anything, unless it took the key over with run(interrupt=False).
The cursor and the theme¶
ui.set_cursor(tag, kind="bar") # place the hardware cursor on a tagged view
ui.set_cursor(None) # hide it
ui.set_theme(FLAVORS["dracula"]) # switch flavour for subsequent frames
ui.set_wraps(tags) # rows that continue a long line, by their tags
set_cursor takes a tag, so the cursor follows whatever the tag ends up on after layout. kind is block, bar or underline, each with a -blinking variant. An editor uses this to put the real terminal cursor where its caret is drawn, which is what makes it visible to screen readers and to terminal copy-and-paste.
set_wraps is for an app that wraps long lines itself. A terminal copies a selection across two rows as one line only if it did the wrapping, so by default a mouse copy of a wrapped line comes out with a break in the middle. The rows with the tags you name are painted straight on into the next row, which lets the terminal wrap them and copy the line out whole. Only rows the full width of the terminal are honoured, since a narrower one has other text beside it. Like set_cursor, it lasts one frame: call it from the app function on every render.
Suspending¶
To hand the terminal to something else (an editor, a pager, git commit):
The alternate screen is left, cooked mode restored and the cursor shown; on the way back everything is retaken and the next paint is a full one. Outside a real terminal (under testing.render) it does nothing, so you need not check.