Creating a form when I don't know how many user inputs there will be at development time
message.txt4.72KB
value that I want to pass to the should be a reactive variable (?), or else that I would create a reactive variable that is a dictionary and have it be keyed by with the values being the text in the . I've tried creating a dict of reactive variables and a reactive dict, but it doesn't seem to work.| question_id | question_text |
|-------------|------------------------------|
| 1 | What is your name? |
| 2 | What is your quest? |
| 3 | What is your favorite color? |{question_id: question_text}solara.lab.ConfirmationDialogsolara.Textsolara.InputText| submission_id | answer_id | question_id | answer_text |
|---------------|-----------|-------------|-----------------------|
| 1 | 1 | 1 | Sir Lancelot |
| 1 | 2 | 2 | I seek the Holy Grail |
| 1 | 3 | 3 | Blue |
| 2 | 4 | 1 | Sir Galahad |
| 2 | 5 | 2 | I seek the Grail |
| 2 | 6 | 3 | Blue... |InputTextInputTextquestion_idimport pandas as pd
import solara
# assume that we have a table in a database, and
# we read the data into _something_ like this:
#
question_table = pd.DataFrame(
data=[
[1, "What is your name?"],
[2, "What is your quest?"],
[3, "What is your favorite color?"]
],
columns=["question_id", "question_text"]
)
# or else it can be treated like a list of dicts
#
questions = question_table.to_dict(orient="records")
# questions
# [{'question_id': 1, 'question_text': 'What is your name?'},
# {'question_id': 2, 'question_text': 'What is your quest?'},
# {'question_id': 3, 'question_text': 'What is your favorite color?'}]@solara.component
def BridgeOfDeath(is_open, on_ok, questions):
# normally, I would create some reactive variables here, but I don't know
# how many (apart from `len(questions)`). I also need to associate those
# reactive variables with to the question IDs so that I can save the data.
# Should it be a reactive dict, like:
# answers_by_question_id = solara.use_reactive({})
# Or a dict of reactive variables like:
answers_by_question_id = {
question["question_id"]: solara.use_reactive("")
for question in questions
}
def _on_ok():
print("_on_ok called")
on_ok(answers_by_question_id)
with solara.lab.ConfirmationDialog(
is_open,
ok=solara.Button(
label="Answer",
on_click=_on_ok,
# disabled= # if any question is unanswered
),
on_ok=_on_ok,
title="Answer",
max_width=1000,
):
for question in questions:
question_id = question["question_id"]
question_text = question["question_text"]
with solara.Card():
solara.Text(question_text)
solara.v.Spacer()
solara.InputText(
label=f"Question #{question_id}",
value=answers_by_question_id[question_id].value,
on_value=answers_by_question_id[question_id].set,
continuous_update=True,
)@solara.component
def Page():
def save_answers(answers):
print(f"Method to save answers to the database: {answers}")
bridge_of_death_is_open = solara.use_reactive(False)
with solara.Card(
"Who would cross the Bridge of Death must answer me "
"these questions three, ere the other side ye see:"
):
BridgeOfDeath(
bridge_of_death_is_open,
on_ok=save_answers,
questions=questions,
)
solara.Button(
label="Ask me the questions, I am not afraid.",
on_click=lambda: bridge_of_death_is_open.set(True),
)
Page()