Skip to content

Latest commit

 

History

History
186 lines (130 loc) · 6.56 KB

README.rst

File metadata and controls

186 lines (130 loc) · 6.56 KB

pydantic-panel

pydantic-panel makes it easy to auto-generate UI elements from Pydantic models.

Pypi package version Python version Build Status Documentation Status MIT License

Getting Started | Documentation | Support

pydantic-panel makes it easy to auto-generate UI elements from Pydantic models and any other Python object. The UI elements can be used in your Jupyter Notebook and in your Panel data app.

images/pydantic-panel-simple.png

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 Started

Step 1 - Install

Requirements: Python 3.7+.

pip install pydantic-panel

Step 2 - Import panel and add your models to layouts!

import pydantic
import panel as pn

pn.extension()

class SomeModel(pydantic.BaseModel):
    name: str
    value: float

model = SomeModel(name="meaning", value=42)

widget = pn.panel(model)

layout = pn.Column(widget, widget.json)
layout.servable()

Now you can edit your model:

images/simple_model_example.png

How it works

If you install pydantic_panel, it will register the widget automatically using the panel.BasePane.applies interface. After importing, calling panel.panel(model) will return a panel.CompositeWidget whos 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 pass bidirectional = True to 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 Behavior

You 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.

from pydantic_panel import infer_widget
from pydantic.fields import FieldInfo
from typing import Optional

# precedence > 0 will ensure this function will be called
# instead of the default which has precedence = 0
@infer_widget.dispatch(precedence=1)
def infer_widget(value: MY_TYPE, field: Optional[FieldInfo] = None, **kwargs):
    # extract relavent info from the pydantic field info here.

    # return your favorite widget
    return MY_FAVORITE_WIDGET(value=value, **kwargs)

Supported types

  • int
  • float
  • str
  • list
  • tuple
  • dict
  • datetime.datetime
  • BaseModel
  • List[BaseModel]
  • pandas.Interval
  • numpy.ndarray

FAQ

Q: 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 .value attribute just like any other field and therefore requires no special treatment. When the parent collects the values of its children it just reads the widget.value attribute 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 .value attribute of its children widgets for changes.

Features

  • TODO

Support & Feedback

Type Channel
🐛 Bugs BugImage
🎁 Features FeatureImage
❓ Questions QuestionImage

Credits

This package was created with Cookiecutter and the briggySmalls/cookiecutter-pypackage project template.