Check your understanding of the previous reading: using dataclasses for a Drafter application’s state.
Assuming that the following is defined, what are valid types for the state parameter in a route function?
from dataclasses import dataclass
@dataclass
class State:
count: int
name: str
Remember, just because a dataclass is defined does not mean that we have to use it.
Select every option that applies, then check.
int works.def index(state: str) -> Page.int and str specifically - any single value, including a float, works the same way.State is exactly the kind of type this reading uses to hold more complex state.State.count is not a type at all - it's an attribute access on the class itself, which does not hold a value until an instance exists.Page is what a route returns, not what it receives as state; nothing here uses a Page instance as an application's state."index" is a route name, the kind of string passed as a Button's target - not a type.Which of the following are correct descriptions of Page?
Select every option that applies, then check.
Page function is really the Page dataclass."Page(...), is really constructing a dataclass instance.Page(...) constructs a Page instance - a dataclass's own name doubles as its constructor function, as the Dataclasses reading explains.Page builds a value; it doesn't decide which code runs, the way an if statement does.Page(...) is an expression, the value inside a return statement - it isn't a statement itself.state and content are Page's attributes; Page itself is the dataclass they belong to.state is a parameter name used throughout these examples; Page is never a parameter, only ever a return value.The following is a Drafter application that runs, but does not correctly update the state of gamma when the button is clicked.
from drafter import *
@dataclass
class State:
alpha: int
beta: str
gamma: bool
@route
def index(state: State) -> Page:
return Page(state, Div(
Text(f"Alpha: {state.alpha}"),
Text(f"Beta: {state.beta}"),
Text(f"Gamma: {state.gamma}"),
Button("Toggle Gamma", "toggle_gamma")
))
@route
def toggle_gamma(state: State) -> Page:
new_state = State(state.alpha, state.beta, state.gamma)
new_state.gamma = not state.gamma
return index(state)
start_server(State(10, "initial", False))
Why doesn't this code work as intended?
Select one answer, then check.
toggle_gamma builds new_state with the toggled gamma value, then calls index(state) with the original, untouched state - new_state is thrown away.state (plus any form fields); it never needs a separate parameter for every attribute of the state dataclass.bool; gamma being a bool is not the problem.Button takes a label and a route name, as in Button(label, url); it never takes a state attribute as an argument.index(state) to build the next Page is the same pattern the update route uses earlier in this reading; returning a Page some other way is not required.If a Drafter application wants to update its state, which of the following is required?
Select every option that applies, then check.
Button whose target names the route that performs the update.Button only takes a label and a route name (Button(label, url)); it never takes the new state as an argument.TextBox does; that naming rule applies to input components, not buttons.State and pass it (directly or through another route) to Page.Page, as plus_player1 does.Which of the following are correct unit tests for the index route below?
from drafter import *
@dataclass
class State:
count: int
@route
def index(state: State) -> Page:
state.count += 1
return Page(state, Button("Increase", "index"))
start_server(State(0))
Select every option that applies, then check.
index mutates state.count in place, so calling it on State(0) leaves that same instance with count equal to 1.count = 5, the += 1 makes it 6.index always increments count by one, so starting from 10 it becomes 11, not 10.count = 1) is equal to a fresh State(1).count = 11, not 10, so it isn't equal to State(10)..state is a State instance, not a bare number; comparing it to 1 can never be equal, no matter what count is.