Package: pydantic-panel
pydantic-panel makes it easy to auto-generate UI elements from
Pydantic models.Getting Started|Documentation|Supportpydantic-panel makes it easy toauto-generate UI elementsfromPydanticmodels and any other Python object. The UI elements
can be used in yourJupyter Notebookand in yourPaneldata app.This project is at an early stage and potentially contains bugs. You might also
see api changes, USE AT YOUR OWN RISK.I will continue to add support for more types as I need them. Feel free to
open issues with feature requests or better yet PRs with implementations.Getting StartedStep 1 - InstallRequirements: Python 3.7+.pip install pydantic-panelStep 2 - Import pydantic_panel and add your models to layouts!importpydanticclassSomeModel(pydantic.BaseModel):name:strvalue:floatmodel=SomeModel(name="meaning",value=42)importpanelaspnimportpydantic_panelpn.extension()widget=pn.panel(model)layout=pn.Column(widget,widget.json)layout.servable()Now you can edit your model:How it worksIf you importpydantic_panel, it will register the widget automatically using thepanel.BasePane.appliesinterface.
After importing, callingpanel.panel(model)will return apanel.CompositeWidgetwhos value is the model.When you change one of the sub-widget values, the new value is validated/coerced using the corresponding pydantic
field and if it passes validation/coercion the new value is set on the model itself.
By default this is a one-way sync, if the model field values are changed via code, it does not sync the widgets.If you want biderectional sync, you can passbidirectional = Trueto the widget constructor, this will patch the model
to sync changes to the widgets but this may break without warning if pydantic change the internals of
their__setattr__method.Customizing BehaviorYou can add or change the widgets used for a given type by hooking into the dispatch
mechanism (we use plum-dispatch). This can be used to override the widget used for a supported
type or to add support for a new type.frompydantic_panelimportinfer_widgetfrompydanticimportFieldInfofromtypingimportOptional# precedence > 0 will ensure this function will be called# instead of the default which has precedence = 0@infer_widget.dispatch(precedence=1)definfer_widget(value:MY_TYPE,field:Optional[FieldInfo=None,**kwargs):# extract relavent info from the pydantic field info here.# return your favorite widgetreturnMY_FAVORITE_WIDGET(value=value,**kwargs)Supported typesintfloatstrlisttupledictdatetime.datetimeBaseModelList[BaseModel]pandas.Intervalnumpy.ndarrayFAQQ: Why did you decide to use CompositWidget instead of Pane like Param uses?A: Nested models. This is a recursive problem, so I was looking for a recursive solution. By using a Widget to
display models, all fields are treated equally. A field of type BaseModel is edited with a widget that has a.valueattribute just like any other field and therefore requires no special treatment. When the parent collects the values of its children
it just reads thewidget.valueattribute and does not need to check whether the value is nested or not. At every level
of the recursion the widget only has to care about the fields on its model class and watch only the.valueattribute of
its children widgets for changes.FeaturesTODOSupport & FeedbackTypeChannel🐛 Bugs🎁 Features❓ QuestionsCreditsThis package was created withCookiecutterand thebriggySmalls/cookiecutter-pypackageproject template.