Skip to content

Custom components

Custom components are components for your domain (a flight, an invoice, a ticket), defined in Python. Use one when you’d otherwise arrange the same built-ins over and over, or when the model should be able to ask for it by name.

The snippets below are fragments that assume an app = dyui.App(...); the complete example is a whole file.

A custom component is a pydantic model with a render() method that returns built-in components. Register it with @app.component. It joins app.catalog, its line is added to the system prompt, and the model can use it from the next turn on.

import dyui
from dyui import ui
@app.component
class Flight(dyui.Component):
"""A bookable flight option. Use when listing specific flights."""
airline: str
depart: str
arrive: str | None = None
price: float
examples = [{"airline": "TAP", "depart": "Fri 18:05", "price": 189}]
def render(self):
return ui.Card(
self.airline,
ui.Stat("Price", f"${self.price:.0f}"),
actions=[ui.Button("Book", action="book_flight", data=self.model_dump())],
)

The model sees:

flight(airline: string, depart: string, arrive?: string, price: number) — A bookable flight option. Use when listing specific flights.
Python Prompt / behaviour
class name Flight component name flight (snake_case). Override with name = "flight_option".
first docstring paragraph the description. Say when to use it. Override with description = "...".
fields props; fields with defaults are optional (?)
examples = [...] validated when the class is defined, so a typo fails at import
render() the built-in tree the browser receives

render() may return a component, a list of components (wrapped in a card), a string (rendered as Markdown) or a {"ui": ...} dict.

Custom components are expanded on the server. The browser only renders built-ins, and the outer node keeps your name in props.__component__ for debugging.

Set these as plain class attributes:

Option Effect
name The name the model writes (default: snake_case of the class name)
description The prompt description (default: first docstring paragraph)
examples Example prop dicts, validated at class creation
llm = False Never offered to the model; your code can still yield it
positional = ("airline", "depart") Props that can be passed positionally: Flight("TAP", "Fri 18:05", price=189)

A registered component is an ordinary model, so you can construct and yield it yourself:

yield Flight(airline="TAP", depart="Fri 18:05", price=189)

When the built-ins aren’t enough, render your own HTML in a sandboxed iframe: sandbox="allow-scripts", no same-origin access, height sized automatically. The model can never produce HTML; only your code can.

yield ui.Html("<b>Boarding pass</b> · Gate B32", height=120)
# from a template file — {{ key }} is HTML-escaped, {{{ key }}} is inserted raw
yield dyui.html_template("cards/boarding_pass.html", passenger="Alex Morgan", gate="B32")
# the same, as a component
yield ui.Html(template="cards/boarding_pass.html", props={"passenger": "Alex Morgan", "gate": "B32"})

A template path is relative and is looked up in <root>/templates/, then <root>/. root is the root= you pass (template_root= on ui.Html), otherwise your app’s directory, otherwise the current directory. Only .html / .htm files inside those folders are read: absolute paths, drive letters, .. and backslash escapes, null bytes and symlinks that point outside are refused with a ValueError. The template path and props never reach the browser — only the rendered HTML does.

Scripts in your HTML run inside the app’s Content-Security-Policy: DyUI adds the page’s nonce to every <script> tag, so <script> blocks work, but inline event-handler attributes (onclick="…") are blocked — use addEventListener in a script. The iframe can’t reach the app’s cookies, storage or DOM, open popups, submit forms or navigate the page.

Terminal window
dyui claude init app.py # or gemini, codex, qwen, antigravity, kimi, opencode, cursor

This briefs that agent on DyUI and launches its CLI on this job. The agent reads your app, proposes 3–6 components, asks you to confirm, and implements them. See Build with AI for everything else the agent can do.

A Flight component, yielded by your code and offered to the model, with a Book button handled in Python. It needs no API key.

app.py
"""A custom Flight component: defined in Python, yielded by your code, and offered
to the model too (switch reply() to dyui.llm.stream with components=app.catalog).
Save as app.py, then run: dyui run app.py (dyui catalog app.py shows its prompt line)
"""
import dyui
from dyui import ui
app = dyui.App(title="Flight Finder", starters=["Flights to Lisbon"])
@app.component
class Flight(dyui.Component):
"""A bookable flight option. Use when listing specific flights."""
airline: str
depart: str
arrive: str | None = None
price: float
examples = [{"airline": "TAP", "depart": "Fri 18:05", "price": 189}]
positional = ("airline", "depart")
def render(self):
return ui.Card(
self.airline,
ui.KeyValue({"Departs": self.depart, "Arrives": self.arrive or "—", "Price": f"${self.price:.0f}"}),
actions=[ui.Button("Book", action="book_flight", data=self.model_dump(), style="primary")],
)
@app.on_message
async def reply(msg: dyui.Message, chat: dyui.Chat):
yield "Two options for Friday:"
yield Flight("TAP", "Fri 18:05", arrive="20:10", price=189)
yield Flight("easyJet", "Fri 06:50", price=142)
@app.on_action("book_flight")
async def book(action: dyui.Action, chat: dyui.Chat):
# action.data is the server's copy of the button's data, so the price can be trusted
yield ui.Callout(f"Booked {action.data['airline']} at {action.data['depart']} for ${action.data['price']:.0f}.",
level="success")
  1. Save it as app.py in a new, empty file, or download app.py.

  2. Run it:

    Terminal window
    pip install dyui
    dyui run app.py
  3. Open http://localhost:8000, send the starter and click Book. dyui catalog app.py shows the line the model would see for flight.

DyUI Docs assistant

Answers come from the DyUI docs. gpt-oss-120b via Groq. Press Escape or the close button to return to the page.