Sheets and dialogs

Use a sheet for a short task over the native interface your Python script is showing. For a place card, the sheet makes room to preview the guest’s name and a dialog lets you ask for a new one.

A place card preview on iPhone A place card preview on iPhone A place card preview on iPad A place card preview on iPad
"""Edit a name in a place card preview, then return to the table plan."""

from dataclasses import dataclass

from juno import ui


@dataclass
class State:
    preview: bool = True
    guest: str = "Ada"


state = State()


def open_preview():
    state.preview = True


def dismiss():
    state.preview = False


def rename():
    name = ui.prompt("Guest name", initial=state.guest)
    if name is not None:
        state.guest = name.strip()


def clear():
    if ui.confirm("Clear this place card?", ok_text="Clear", destructive=True):
        state.guest = ""


def show_seat():
    ui.alert("Seat assignment", "Table by the window, seat four.")


def body(s):
    card = ui.VStack(
        ui.Text(s.guest or "Unassigned", font="large_title", design="serif"),
        ui.Button("Edit name", on_press=rename, style="bordered"),
        ui.Button("Seat assignment", on_press=show_seat),
        ui.Button("Clear name", on_press=clear, disabled=not s.guest),
        ui.Button("Done", on_press=dismiss, style="filled"),
        padding=20, spacing=12)
    return ui.VStack(
        ui.Text(f"Seat four: {s.guest or 'Unassigned'}"),
        ui.Button("Preview card", on_press=open_preview),
        ui.Sheet(card, title="Place card", is_presented=s.preview, on_dismiss=dismiss),
        padding=20)


if __name__ == "__main__":
    ui.run(body, state, title="Table plan")

Open and close the preview

Put one Sheet directly in the root VStack of the window or a page. The example starts with is_presented=True so the preview is open. Swiping it away calls on_dismiss, where you set the state flag to False. The Done button clears the same flag, and Preview card opens the sheet again.

Ask for one answer

Call a dialog from a callback, outside body. Use alert for a message with an OK button, like the seat assignment. Use confirm to ask before clearing the card: it returns True for confirmation and False for Cancel. Use prompt to edit a name: it returns the submitted string, even an empty one, or None for Cancel.

Parameters

Sheet

Parameter

Default

Meaning

content

Required

One view shown in the sheet.

is_presented

Required

Controlled bool; clear it when the user dismisses.

on_dismiss

Required

Function accepting no arguments and clearing presentation.

title

''

Sheet title; defaults to an empty string.

top_bar

None

Optional view of Buttons, Text and TextFields, shown in a bar under the sheet’s title, as a Page’s top_bar.

key

None

Optional persistent sibling key.

alert

Parameter

Default

Meaning

title

Required

Dialog title.

message

''

Optional explanatory text.

confirm

Parameter

Default

Meaning

title

Required

Dialog title.

message

''

Optional explanatory text.

ok_text

'OK'

Confirmation button label.

cancel_text

'Cancel'

Cancellation button label.

destructive

False

Whether the confirmation has destructive styling.

prompt

Parameter

Default

Meaning

title

Required

Dialog title.

message

''

Optional explanatory text.

initial

''

Initial text.

placeholder

''

Hint for empty input.

ok_text

'OK'

Submission button label.

cancel_text

'Cancel'

Cancellation button label.

secure

False

Whether to hide typed characters.

Seen in

Run Weather from Home’s Native UI section and add a city to see a sheet with a search field.