How yugui works¶
You don't need this page to write apps. It explains where your code runs and what happens between a click and the next frame, which helps when something surprises you.
One process, three layers¶
- Your app subclasses
ui.Appand implementsinit,updateandview. It's plain Python. - yugui's Python package is the API you use. Widget functions such as
ui.buttonandui.columndon't create widgets: they returnNodes, plain values with a kind ("button"), options and children. Options are keyword-only arguments, so a misspelt one is aTypeErroron the line that has it. - The Rust extension,
yugui._native, is written in Rust with PyO3, the library for writing Python modules in Rust. It turns eachNodeinto the iced widget of the same name, using iced's own functions. It also runs the iced application, and calls yourinit,updateandviewwhen iced asks for them. - iced lays out the widgets, handles the mouse, keyboard and focus, draws on the GPU (or in software when there's no GPU), and opens the windows.
ui.run(app) must be called from the main thread, as macOS requires for windows, and it returns when the last window closes. While iced waits for events, the extension releases Python's global interpreter lock, so your other Python threads keep running. That's how ui.perform runs slow work in the background.
The update loop¶
view(model)returns a tree of nodes.- The extension builds iced widgets from them. A widget's
on_pressoron_inputbecomes a small Rust function that makes the message, without calling Python. - iced lays the widgets out and draws them. When you click a button, the button emits its message.
- iced hands the message to the extension, which calls the handler if there is one (
on_input=RenamebecomesRename(text)), then calls yourupdate(model, message). updatereturns the next model, orNoneto keep it, and possibly tasks to run. Thenviewruns again.
All your Python code runs in steps 1, 4 and 5, inside iced's view and update callbacks, and never while iced handles an event. So exceptions surface in one place:
| Where | What happens |
|---|---|
update or a handler raises |
the traceback is printed, the previous model is kept, and the app keeps running |
view raises or returns a bad tree |
the traceback is printed once, and an error screen replaces the view until a later view succeeds |
title, theme or subscription raise |
the traceback is printed, and a fallback is used |
Rebuilding every view¶
view builds the whole tree after every message, and yugui converts all of it to iced widgets, without comparing it with the previous tree. iced keeps the state of its widgets (focus, cursor, scroll offsets, an open pick list) across rebuilds, matching old and new widgets by their place in the tree and their type. So a rebuilt view keeps the text input's focus and the list's scroll position.
Two consequences:
- When list items are reordered or removed, state matched by position can stick to the wrong item: the focus moves to another row.
ui.column(..., keys=[...])matches children by key instead. - The cost is per message, not per frame. A typical view has tens to hundreds of nodes, which take a millisecond or so to build and convert. For thousands of rows,
ui.virtual_listandui.virtual_gridbuild only what is in view: the spreadsheet example's 2,600 cells cost what a screenful does.
State that yugui keeps for you¶
A few iced widgets leave part of their state to the app: a text editor's content and cursor, a combo box's search text and filtered options. An iced app written in Rust keeps that state itself and lends it to the widget in each view. In yugui, the extension keeps it for you, under the widget's id, from one view to the next:
The model holds the text. When you edit, yugui applies the edit to its copy and sends on_change with the whole text. Moving the cursor or selecting sends nothing. If view passes a text that differs from yugui's copy, after an undo for example, the copy is replaced.
Tests run the same code¶
yugui.testing.Driver replaces the window, not the widgets. Your view and update run unchanged, the nodes are converted by the same code, and iced lays them out and finds what each click lands on, as it does in a window. Only the input is simulated, and the view is drawn in software when you ask for a snapshot. Testing apps shows how to use it.