Getting Started¶
Build one small app, a label and a button that counts, twice: declaratively
(a TDL view and a Python ViewModel) and imperatively (Python calls that
create nodes). Each step shows both, and every program here is a file in
examples/getting_started/
that Tesserae's tests run.
Install¶
You need Python 3.14 or newer and a desktop session with a graphics driver the engine can use (on Linux, Vulkan; see Installation if a window won't open).
mkdir counter && cd counter
python3 -m venv .venv
source .venv/bin/activate # fish: source .venv/bin/activate.fish
pip install tesserae-ui
tesserae-ui is the name on PyPI; you import tesserae. Everything below goes
in the counter folder, with the environment active.
1. A window¶
An App owns the window. Make one 360 by 160 with a dark background (so the
white text we add next reads on any desktop theme) and open it.
---
TDL: 0.2
Name: Counter
Tesserae: "0.6"
---
Container {
Style { Flex_Direction: vertical, Width: 360, Height: 160, Gap: 12, Padding: 16, Background: "#1C1B1F" }
}
from pathlib import Path
from tess_ui import App
HERE = Path(__file__).parent
app = App(width=360, height=160, title="Counter")
app.open_view(HERE / "Counter_View.tdl")
app.show("Counter")
app.run()
from tess_ui import App
app = App(width=360, height=160, title="Counter")
window = app.window
window.root.set(flex_direction="vertical", gap=12, padding=16, fill=(0x1C, 0x1B, 0x1F, 0xFF))
app.run()
Declaratively the window shows a screen: app.open_view reads
Counter_View.tdl and registers it, and app.show("Counter") puts it in the
window. The screen's name is its file's name without _View.tdl. It has no
ViewModel yet, because it has nothing to react to. Imperatively there is no
file: window.root is the box every node goes in, and one call sets its layout
and background.
python app.py
An empty window titled "Counter" opens.
2. A node¶
A node is one thing on screen. Add a label that says Count: 0.
---
TDL: 0.2
Name: Counter
Tesserae: "0.6"
---
Container {
Style { Flex_Direction: vertical, Width: 360, Height: 160, Gap: 12, Padding: 16, Background: "#1C1B1F" }
Children {
Text {
Name: label
Text: "Count: 0"
Font_Family: Roboto
Font_Size: 20
Style { Width: 160, Height: 28, Foreground: "#FFFFFF" }
}
}
}
from pathlib import Path
from tess_ui import App
HERE = Path(__file__).parent
app = App(width=360, height=160, title="Counter")
app.open_view(HERE / "Counter_View.tdl")
app.show("Counter")
app.run()
from tess_ui import App
app = App(width=360, height=160, title="Counter")
window = app.window
window.root.set(flex_direction="vertical", gap=12, padding=16, fill=(0x1C, 0x1B, 0x1F, 0xFF))
label = window.create("text", text="Count: 0", font_size=20, width=160, height=28, fill=(255, 255, 255, 255))
window.root.add_child(label)
app.run()
In TDL a node is an entry in Children { ... }: Text { ... } makes a label and
Text holds what it says. Every node is a widget, and the rest
of its keys are that widget's properties (Font_Size), its Style and a
Name to find it by. In code, window.create("text", ...) makes the node
and window.root.add_child(...) puts it in the window. The text's colour is
Foreground in TDL and fill= in code.
If Python prints an error
A mistake in a view file ends in a traceback; read the last line. It names the file, the line and column, the widget and the problem, and for a misspelt word says what you probably meant:
tess_ui.spec.nodes.LoadError: .../Counter_View.tdl:9:37: error: Text: style 'foregorund' is not valid here (did you mean 'foreground')
3. A second node¶
Add a button: a purple, rounded box with a centred text node in it.
---
TDL: 0.2
Name: Counter
Tesserae: "0.6"
---
Container {
Style { Flex_Direction: vertical, Width: 360, Height: 160, Gap: 12, Padding: 16, Background: "#1C1B1F" }
Children {
Text {
Name: label
Text: "Count: 0"
Font_Family: Roboto
Font_Size: 20
Style { Width: 160, Height: 28, Foreground: "#FFFFFF" }
}
Rect {
Name: button
Style { Width: 96, Height: 40, Background: "#6750A4", Corner_Radius: 20, Align_Content: left }
Children {
Text {
Name: button_label
Text: Count
Font_Family: Roboto
Font_Size: 16
Text_Align: center
Style { Width: 96, Height: 20, Foreground: "#FFFFFF" }
}
}
}
}
}
from pathlib import Path
from tess_ui import App
HERE = Path(__file__).parent
app = App(width=360, height=160, title="Counter")
app.open_view(HERE / "Counter_View.tdl")
app.show("Counter")
app.run()
from tess_ui import App
app = App(width=360, height=160, title="Counter")
window = app.window
window.root.set(flex_direction="vertical", gap=12, padding=16, fill=(0x1C, 0x1B, 0x1F, 0xFF))
label = window.create("text", text="Count: 0", font_size=20, width=160, height=28, fill=(255, 255, 255, 255))
window.root.add_child(label)
button = window.create("box", width=96, height=40, corner_radius=20, fill=(0x67, 0x50, 0xA4, 0xFF),
align_items="center")
button.add_child(window.create("text", text="Count", font_size=16, width=96, height=20,
fill=(255, 255, 255, 255), text_align="center"))
window.root.add_child(button)
app.run()
A button is a Rect (a coloured box) with a Text in it. The two ways name
things a little differently:
| TDL view | Python |
|---|---|
Text { ... } / Rect { ... } |
window.create("text", ...) / window.create("box", ...) |
Style { Width: 96, ... } |
width=96 |
Foreground, Background ("#RRGGBB") |
fill=(r, g, b, a) |
Text: "...", Font_Size: 20 |
text="...", font_size=20 |
Children { ... } |
node.add_child(other) |
4. Make it count¶
from tess_ui import Signal, ViewModel
class CounterViewModel(ViewModel):
views = "Counter" # the view it serves: the name after `View` at the top of Counter_View.tdl
def __init__(self):
super().__init__()
self.count = Signal(0)
def increment(self):
self.count.update(lambda n: n + 1)
---
TDL: 0.2
Name: Counter
Tesserae: "0.6"
---
Container {
Name: Counter # the name the ViewModel serves
Style { Flex_Direction: vertical, Width: 360, Height: 160, Gap: 12, Padding: 16, Background: "#1C1B1F" }
Children {
Text {
Name: label
Text: Count: <| count |>
Font_Family: Roboto
Font_Size: 20
Style { Width: 160, Height: 28, Foreground: "#FFFFFF" }
}
Rect {
Name: button
Handlers {
On_Click: increment
}
Style { Width: 96, Height: 40, Background: "#6750A4", Corner_Radius: 20, Align_Content: left }
Children {
Text {
Name: button_label
Text: Count
Font_Family: Roboto
Font_Size: 16
Text_Align: center
Style { Width: 96, Height: 20, Foreground: "#FFFFFF" }
}
}
}
}
}
from pathlib import Path
from tess_ui import App
from Counter_ViewModel import CounterViewModel
HERE = Path(__file__).parent
app = App(width=360, height=160, title="Counter")
app.bind(CounterViewModel)
app.open_view(HERE / "Counter_View.tdl")
app.show("Counter")
app.run()
from tess_ui import App
app = App(width=360, height=160, title="Counter")
window = app.window
window.root.set(flex_direction="vertical", gap=12, padding=16, fill=(0x1C, 0x1B, 0x1F, 0xFF))
label = window.create("text", text="Count: 0", font_size=20, width=160, height=28, fill=(255, 255, 255, 255))
window.root.add_child(label)
button = window.create("box", width=96, height=40, corner_radius=20, fill=(0x67, 0x50, 0xA4, 0xFF),
align_items="center")
button.add_child(window.create("text", text="Count", font_size=16, width=96, height=20,
fill=(255, 255, 255, 255), text_align="center"))
window.root.add_child(button)
clicks = 0
def count():
global clicks
clicks += 1
label.set(text=f"Count: {clicks}")
button.on("click", count)
button.set(role="button", label="Count", focusable=True, cursor="pointer") # Tab, then Enter or Space
app.run()
Declaratively the state lives in a ViewModel. The view says which one by its
Name: Counter, and the ViewModel says which views it serves with views =
"Counter"; app.bind(CounterViewModel) joins them. Its Signal holds the
count; the label's Text: Count: <| count |> keeps showing it, and
Handlers { On_Click: increment } calls increment on a click. A Signal reads as its value in a view,
so <| count |> follows it.
Imperatively the state is a variable and you are the view: button.on("click",
count) runs count, which sets the label with label.set(text=...).
Run it. Click the button, or press Tab and then Enter or Space, and the count goes up. The two versions are the same app.
To see an edit without restarting, run the declarative version with app.run(hot_reload=True), count a few clicks, then change the label's text in
Counter_View.tdl and save: the window shows the new text with the count kept (Hot Reload).
Which to use¶
Use declarative for the screens you design: layout, styling, themes and
bindings live in the view file, and the ViewModel holds only your logic. Use
imperative for what is made while the app runs; the
Widgets in Python guide covers it. The two mix freely,
because a declarative screen is made of the same nodes you create in code:
app.screen("Counter") gives the view and its ViewModel, and
view.node("root.button") returns any node the view declared (a node's id is its
path from the root, using each Name).
Next¶
- Tutorial: build a bigger app with a custom window, several views and ViewModels, components and styling, in one flat folder; or as a project, with the files in folders and found by name.
- Projects and the
tesseraecommand: start an app from a template, with its files in standard folders. - Apps & Screens, Layout and Themes.
- Components: every MD3 component, with its TDL.