State bools? #2042
Atomizer74
announced in
Q&A
State bools?
#2042
Replies: 1 comment
-
i was trying to reproduce this and here was my code import reflex as rx
class CBoxeState(rx.State):
choices: dict[str, bool] = {
k: False
for k in ["Choice A", "Choice B", "Choice C"]
}
_check_limit = 2
def check_choice(self, value, index):
self.choices[index] = value
@rx.var
def choice_limit(self):
return (
sum(self.choices.values()) >= self._check_limit
)
@rx.var
def checked_choices(self):
choices = [l for l, v in self.choices.items() if v]
return " / ".join(choices) if choices else "None"
@rx.var
def choices_list(self) -> list[tuple[str, bool]]:
return [tuple(i) for i in self.choices.items()]
def render_checkboxes(values, limit, handler):
return rx.vstack(
rx.checkbox_group(
rx.foreach(
values,
lambda choice: rx.checkbox(
choice[0],
is_checked=choice[1],
is_disabled=~choice[1] & limit,
on_change=lambda val: handler(
val, choice[0]
),
),
)
)
)
def dialogue(text: str, user: bool):
return rx.box(
rx.text(text, rx.cond(user, " is checked", " is not checked")),
margin_y="1em",
)
def index() -> rx.Component:
return rx.center(
rx.vstack(
rx.text("Make your choices (2 max):"),
render_checkboxes(
CBoxeState.choices,
CBoxeState.choice_limit,
CBoxeState.check_choice,
),
rx.text(
"Your choices: ", CBoxeState.checked_choices
),
rx.foreach(
CBoxeState.choices_list,
lambda messages: dialogue(messages[0], messages[1]),
),
),
height="100vh",
)
app = rx.App()
app.add_page(index)
app.compile() Borrowed from https://reflex.dev/docs/recipes/checkboxes/ |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
So I was following the basic chatbot tutorial in the documentation, and decided having the QA pairs wasn't what I wanted, so instead of a second string I used a bool, to determine if it is the user or not, and then just have a list of messages...
So I just used:
Which worked fine, but then when it got to adding the state messages:
Now I was getting this error(Updated with correct error):
So I am like, ok, I will swap to using that then, and changed the dialogue function:
And now I am getting the TypeError:
I tried
not user
,user is True
etc, I looked around and could not find any usage of bools as state, I am at a complete loss other than just feeding it as a number, or a string of "user"/"assistant".I did find this: #1750
So I tried
user is not None
,user._decode()
, even~user
, all resulting in the same error.When I tried
user.equals(True)
I get:AttributeError: 'bool' object has no attribute 'name'
I cannot understand why using a simple boolean state seems to be such a big issue, especially since they are directly usable in JSON.
The only thing I can think of is, the only issue is Reflex artificially limiting it, I mean, it is, from the docs:
Raise exception if using Var in a boolean context. Raises: TypeError: when attempting to bool-ify the Var.
But I don't know the reason, or how to get around it, other than, not using bools in State.
So, what am I missing here? It has to be something pretty simple, right?
Beta Was this translation helpful? Give feedback.
All reactions