Skip to content
ReferenceAdvancedPass extra values to a route.

Argument

Group: Actions

Description

An Argument is an extra value that a button or link delivers to its route, filling a parameter by name exactly the way a form field would. Arguments let several buttons share one route while still telling that route which button was clicked, which is the basis of the one-route-many-items pattern.

Syntax

Button("Label", "target_route", [Argument(name, value)])
Argument(name, value)

Parameters

Parameter Type Default Meaning
name str required The target route's parameter to fill. Must be a valid Python parameter name.
value JSON-safe required The value to deliver: strings, numbers, booleans, or lists of those.

Examples

from drafter import *
from dataclasses import dataclass


@dataclass
class State:
    last_wish: str


@route
def index(state: State) -> Page:
    return Page(state, [
        "The genie last heard: " + state.last_wish + "\n",
        Button("Wish for gold", "wish", [Argument("wish_for", "gold")]),
        Button("Wish for time", "wish", [Argument("wish_for", "time")]),
        Button("Wish for naps", "wish", [Argument("wish_for", "naps")])
    ])


@route
def wish(state: State, wish_for: str) -> Page:
    state.last_wish = wish_for
    return index(state)


start_server(State("nothing yet"))

Notes

  • A button or link accepts a single Argument, a list of them, a list of (name, value) pairs, or a dictionary such as {"wish_for": "gold"}. Each entry fills the route parameter with the same name, so several entries fill several parameters. Passing arguments on the Button page shows all four forms.
  • An argument given to a Button or Link is stored on that one button or link, and only a click on that component delivers it. This is why the three buttons in the example can each carry a different wish_for value.
  • An Argument placed directly in a page's content list behaves differently. Drafter renders it as a hidden input, so every button and link on the page delivers its value, the same way they deliver the contents of a TextBox. A hidden-input argument also follows the rule for inputs that its name must be unique on the page, and a second component with the same name causes a duplicate-name error.
  • When a button's argument and an input on the page have the same name, the route receives the argument's value, and the debug panel warns that the name was provided more than once.
  • The arguments keyword also works on any component that has an on_* event handler. TextBox("amount", on_change="update", arguments={"row": 3}) calls update with row set to 3 each time the box changes.
  • Values must be JSON-safe (text, numbers, booleans, or lists of those). Passing a whole dataclass is not supported; instead, pass a name or index and look the object up inside the route, as the shop example does.
  • An argument whose name matches no parameter of the target route does not stop the page. The route runs without that value. The opposite mistake is an error, because a route parameter that no argument or input fills produces a missing-parameter error page. A misspelled argument name therefore usually causes both problems at once. For the unmatched name, the debug panel shows a warning like this one:

    The component argument 'wish_fr' was sent to 'wish', but the function has no matching parameter.
    

    Expanding the warning's Details shows a suggestion, here "Did you mean the parameter 'wish_for'?". When no parameter has a similar name, the Details say to add a parameter with that name or to remove the argument. An unused argument is reported every time, because you wrote it for that one route. An unused form field is reported only when its name is close to a parameter that received nothing.