Creating a form when I don't know how many user inputs there will be at development time
3 Replies
Sorry for the bad formatting... Discord won't let me paste text without adding it as an attachment.
I want to create a form that has a few questions for a user to answer. I want to store the questions in a database, which means that the application won't know ahead of time how many questions there will be.
So let's say a have a table like:
On application start, I would expect to read these questions into a dictionary
{question_id: question_text}
. When constructing my form (using solara.lab.ConfirmationDialog
), I would loop through the list of questions to create a prompt (solara.Text
) and an input text box (solara.InputText
).
When the user submits the form, I want to capture the responses and write them to a table in my database like:
My issue/question is: how do I create the reactive variable(s) to capture the user input?
I would expect that the value
that I want to pass to the InputText
should be a reactive variable (?), or else that I would create a reactive variable that is a dictionary and have it be keyed by question_id
with the values being the text in the InputText
. I've tried creating a dict of reactive variables and a reactive dict, but it doesn't seem to work.
Here is a minimal example. It "works," except that when you click "Answer" nothing happens -- the print statements do not execute.
Here is an example of a table driven editor, using a dataframe. https://py.cafe/rob.on.lists/solara-inventory-tracker-using_dummy_reactive
To display the updates to the df, I use a reactive that I just increment.
I could also have stored the whole dict in the reactive, but then updates (!) to the dict are not taken as changes, unless I first overwrite the reactive with None.
https://py.cafe/rob.on.lists/solara-inventory-editor-only-works-first-time
I believe is issue is here https://github.com/widgetti/solara/issues/245
PyCafe - Solara - Editable Inventory Tracker, updates are displayed...
Run, Edit and Share Python Apps in Your Browser with 1 click!
PyCafe - Solara - Updates show first time, but 2nd update does not ...
Run, Edit and Share Python Apps in Your Browser with 1 click!
GitHub
Event not firing when reactive variable is set · Issue #245 · widge...
I'm trying to create a solara=1.19.0 component in jupyterlab=4.0.5 with ipywidgets=8.1.0 (on Windows) I have from_date = sol.use_reactive(cast(Optional[str], None)) which will be set to (a stri...
Thank you for this example -- I was able to get it working by following what you did. Very helpful!