Skip to content

Coming from Plushie

This page is for people who have written apps with Plushie, a Python library for desktop apps that also uses the Elm architecture and iced's widgets. Plushie runs iced in a separate renderer program: your Python code sends it the view and gets events back, over a pipe. yugui compiles iced into a Python extension instead, so both run in one process.

Plushie's Python code talks to a renderer process over a pipe; yugui calls iced in the same process

The programming model is the same: an App with init, update and view, frozen dataclasses for the model, match in update. What changes is mostly how widgets report events, because there's no protocol to route them through. Compared to others weighs the two designs.

The main change: messages instead of widget IDs

In Plushie, widgets have IDs and update receives events about them (Click(id="inc"), Input(id="name", value=...)). In yugui, a widget carries the message it sends, so most widgets need no ID:

Plushie yugui
ui.button("inc", "+") and case Click(id="inc"): ui.button("+", on_press="inc") and case "inc":
ui.text_input("name", model.name) and case Input(id="name", value=v): ui.text_input(model.name, on_input=ui.set("name")) and case ui.Set(): return message.apply(model), or on_input=Rename and case Rename(text):
case Submit(id="name", value=v): on_submit="save" and case "save": (the value is already in the model)
ui.checkbox("done", item.done) in a scope, case Toggle(id="done", scope=(item_id, *_)): ui.checkbox(item.done, on_toggle=partial(Toggle, item.id)) and case Toggle(item_id, done):
ui.text("count", f"Count: {n}") ui.text(f"Count: {n}")

A message can be any Python object: a string, a dataclass instance, a tuple. A widget that produces a value (an input's text, a checkbox's state, a slider's position) calls its handler with the value and sends the result, so a dataclass with one field is a handler as it is. Scopes, which Plushie needed to tell apart the widgets of each row, become arguments of the message.

IDs remain only where a task has to name a widget: ui.text_input(..., id="new-todo") with ui.focus("new-todo"), or ui.scrollable(..., id="log") with ui.snap_to("log", y=1.0).

The app

Plushie yugui
class App(plushie.App[Model]) class App(ui.App[Model])
view returns ui.window("main", ...) view returns the main window's content; secondary windows come from windows(model)
subscribe(model) returning [Subscription.every(1000, "tick")] subscription(model) yielding ui.every(1, "tick") (seconds)
case TimerTick(tag="tick"): case "tick": (the message itself)
Subscription.on_key_press() and case KeyEvent(type="press"): ui.on_key() and case ui.KeyEvent(kind="press"):, or ui.on_key(handler) to turn keys into messages
update must return the model; None is detected and warned about returning None keeps the model, so unhandled messages need no case _: return model
Command.task(fn, "fetch") and case AsyncResult(tag="fetch", value=v): ui.perform(fn, then=Fetched, catch=Failed) and case Fetched(data): / case Failed(error):
Command.focus("id") ui.focus("id")
Command.exit() ui.exit()
plushie.run(App) ui.run(App())

update returns tasks the same way Plushie returns commands: (model, task), or (model, [task, ...]). In yugui a task alone keeps the model.

Mutable models are allowed: change the model in place and return nothing.

Testing

Plushie's AppFixture drives an app through the renderer binary. yugui's yugui.testing.Driver drives it through iced's widgets in-process, with no binary and no display:

Plushie yugui
app.click("inc") d.click("+") (by the text shown) or d.click_at(x, y)
app.type_text("name", "Ada") d.type("Ada") into the focused widget, or d.send(ui.Set("name", "Ada"))
app.press("Enter") d.key("Enter")
app.model d.model

What yugui doesn't have

yugui is younger and smaller than Plushie. Missing so far: accessibility (screen readers), animations, hot reload, Windows support, rendering on another machine, custom widgets with their own state and event handling, Command.stream, Command.cancel and Command.send_after, the state helpers (data, selection, route; undo is ui.History), and the SDKs for other languages.

On the other hand, there is no renderer to start, crash or reconnect to: an exception in update keeps the previous model, and one in view shows an error screen until the next view succeeds.

The examples ported from Plushie are Plushie's own demos: compare them with the originals to see the changes on real code.