-
Notifications
You must be signed in to change notification settings - Fork 142
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
dynamic forms in solara #512
Comments
Unsure if this helps, but I added a very basic implementation of forms to this effect here: It uses pydantic to do this, and pydantic models can be autogenerated from json schema (https://docs.pydantic.dev/latest/integrations/datamodel_code_generator/). It's in a very WIP PR to that repo. |
On the topic of mixing classic widgets with components, this might be a good example on how to add a classic widget in a component (in this case ipyautoui cc @jgunstone): import solara
from pydantic import BaseModel, Field
from ipyautoui import AutoUi
class LineGraph(BaseModel):
"""parameters to define a simple `y=m*x + c` line graph"""
title: str = Field(default='line equation', description='add chart title')
m: float = Field(default=2, description='gradient')
c: float = Field(default=5, ge=0, le=10, description='intercept')
x_range: tuple[int, int] = Field(
default=(0,5), ge=0, le=50, description='x-range for chart')
y_range: tuple[int, int] = Field(
default=(0,5), ge=0, le=50, description='y-range for chart')
@solara.component
def Page():
# important to use a widget component, not a function component
container = solara.v.Html(tag="div")#, children=[])
# this will reset the children (possibly a bug in reacton)
# container = solara.Column()
def add_classic_widget():
# generate your normal widget
ui = AutoUi(schema=LineGraph)
# add it to the generated widget by solara/reacton
container_widget = solara.get_widget(container)
container_widget.children = (ui,)
solara.use_effect(add_classic_widget, dependencies=[])
return container One worry is that this does not close the AutoUi widget, and might result in a (temporary) memory leak. It's temporary because all widgets belonging to a virtual kernel will be closed once the virtual kernel will be closed (on browser/page close or kernel cull timeout - default 24h https://solara.dev/docs/understanding/solara-server ) Does it make sense to have this example at https://solara.dev/docs/howto/ipywidget-libraries ? |
the one i made is here: havent used it in a while though |
nice @Jhsmit one note for people reading this: code in @Jhsmit is that use of fields and .dict() is deprecated in newer versions of pydantic:
|
thanks @swelborn, i might update it |
@maartenbreddels - thanks for the mention - as an almost exclusive rule in class MyWidget(w.VBox):
my_trait = tr.Unicode()
def __init__(self, **kwargs):
# pre-init code
super().__init__(**kwargs)
# post-init code I had an initial quick look at your code-gen tool... a v simple widget worked fine (SaveButtonBar).... but the more complex ones didn't... you got any hot tips? |
having recently tried to prototype something with solara this would help tremendously, but for sure needs to have some context of when to apply it. |
I think either ipyautoui is completely using reacton/solara, it we should just advice to use the above example. I have some ideas how we can make it easier to use widgets in a component directly (With a proper memory cleanup phase), but for now the above will suffice. |
@Jhsmit Sorry I didn't give it a go, I just pulled into vscode and noticed the deprecation warnings (which show as the function crossed through inline). |
We would like to display dynamic forms in our solara frontend based on pydantic models / json schemas.
There are multiple IPyWidgets out there that provide this functionality namely:
However both currently do not seem to be compatible with solara. Mainly because they do not directly provide a widget but rather create and manage exisiting widgets. Therefore they do not have the option to use
.element
for initialisation.Do you have any hints on how to best cover this use case?
Should the packages provide a proper widget?
Would it be sufficient to be able to swap the widget classes for the corresponding solara ones? e.g. sl.InputText for IPyWidgets.Text
Is there a good way to mix "traditional" IPyWidgets with reactive ones?
The text was updated successfully, but these errors were encountered: