Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Update AutoUi on schema change #192

Open
ollyhensby opened this issue Oct 4, 2023 · 2 comments
Open

✨ Update AutoUi on schema change #192

ollyhensby opened this issue Oct 4, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@ollyhensby
Copy link
Contributor

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.

@ollyhensby ollyhensby added the enhancement New feature or request label Oct 4, 2023
@jgunstone
Copy link
Collaborator

this will be possible following the next update

@jgunstone
Copy link
Collaborator

reviewing this again... I'm not 100% sure it is possible...
this @classmethods init the form from schema / model:

@classmethod
def from_jsonschema(cls, schema: dict, value: ty.Any = None, **kwargs):
if not isinstance(schema, dict):
raise ValueError(f"schema must be a dict of type jsonschema, not {type(schema)}")
else:
model = None
if "$defs" in schema.keys():
try:
schema = replace_refs(schema)
schema = {k: v for k, v in schema.items() if k != "$defs"}
except ValueError as e:
logger.warning(f"replace_refs error: \n{e}")
pass
if value is not None:
schema["value"] = value
schema = {**schema, **kwargs}
ui = cls(**schema)
ui.schema = schema
return ui
@classmethod
def from_pydantic_model(cls, model: ty.Type[BaseModel], value: ty.Any = None, **kwargs):
if not (issubclass(model, BaseModel) or issubclass(model, RootModel)):
raise ValueError(f"schema must be a pydantic model, not {type(model)}")
else:
schema = replace_refs(model.model_json_schema())
schema = {k: v for k, v in schema.items() if k != "$defs"}
if value is not None:
schema["value"] = value
schema = {**schema, **kwargs}
ui = cls(**schema)
ui.model = model
ui.schema = schema
ui._init_validation_error()
ui._set_validate_value(ui.value)
return ui

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants