-
I'm going through the tutorial in the docs for the chat app and I see in def chat() -> rx.Component:
return rx.box(
rx.foreach(
State.chat_history,
lambda messages: qa(messages[0], messages[1]),
)
) If I didn't want to use lambda, how would I go about that? First I thought I could do: qa(State.chat_history[0], State.chat_history[1]) but that was dumb. So I though I could just convert to a function because that's what lambda is: def chat() -> rx.Component:
return rx.box(
rx.foreach(
State.chat_history,
test(State.chat_history),
)
)
def test(messages):
return qa(messages[0], messages[1]) And then I realized idk wtf i'm doing here. For reference: qa function def qa(question: str, answer: str) -> rx.Component:
return rx.box(
rx.box(
rx.text(question, style=style.question_style),
text_align="right",
),
rx.box(
rx.text(answer, style=style.answer_style),
text_align="left",
),
margin_y="1em",
) State class for chat history list class State(rx.State):
# The current question being asked.
question: str
# Keep track of the chat history as a list of (question, answer) tuples.
chat_history: list[tuple[str, str]]
def answer(self):
# Our chatbot is not very smart right now...
answer = "I don't know!"
self.chat_history.append((self.question, answer))
self.question = '' So I'm just curious at this point, how would I go about not using lambda, or is it necessary for this? |
Beta Was this translation helpful? Give feedback.
Answered by
Dekubaka
May 13, 2024
Replies: 1 comment
-
I asked the discord. I was close but the alternative was def chat() -> rx.Component:
return rx.box(
rx.foreach(
State.chat_history,
test
)
)
def test(messages):
return qa(messages[0], messages[1]) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Dekubaka
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I asked the discord. I was close but the alternative was