maxfordham / ipyautoui

create ipywidgets user input form pydantic model or jsonschema
https://maxfordham.github.io/ipyautoui/
39 stars 4 forks source link

✨ Update AutoUi on schema change #192

Open ollyhensby opened 11 months ago

ollyhensby commented 11 months ago

Feature Request To make it possible for an AutoUi object to update based on a new schema passed.

Expected Behaviour Create AutoUi object.

from ipyautoui import AutoUi
from pydantic import BaseModel

class Stringy(BaseModel):
    stringy: str

ui = AutoUi(schema=Stringy)
display(ui)

Displays this UI object. image

Then setting schema

class Inty(BaseModel):
    inty: int

ui.schema = Inty.schema() OR ui.schema = Inty

Would display this on the same UI object. image

Use Case My use case for this is when using EditGrid: the value and schema can be set in the object, which updates the DataGrid, but the AutoUi object doesn't update leading to a mismatch between the columns (or rows) shown in the DataGrid and the fields in the AutoUi object. The only way this can be done is by re-instantiating the whole EditGrid object. It would be great to have an AutoUi schema setter that updates AutoUi object.

jgunstone commented 11 months ago

this will be possible following the next update

jgunstone commented 7 months ago

reviewing this again... I'm not 100% sure it is possible... this @classmethods init the form from schema / model: https://github.com/maxfordham/ipyautoui/blob/3421b2eb8fdb052d2f94aaf51e50fefdd3108ce1/src/ipyautoui/watch_validate.py#L133-L168

probs need to extract them out and make a tr.observe event to super().__init__() on change... not 100% it would work well though...