Skip to main content

Top-level package for pydantic-panel.

Project description

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 pydantic_panel and add your models to layouts!

import pydantic

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

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

import panel as pn
import pydantic_panel
pn.extension()

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_panel-0.2.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

pydantic_panel-0.2.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_panel-0.2.0.tar.gz.

File metadata

  • Download URL: pydantic_panel-0.2.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for pydantic_panel-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2ac8a5e81f4b1c305f3b3509d5f54e2ae2a70cc861e8acef085232a168efe734
MD5 5fea0fd772080a57e6067b8b380f4ed0
BLAKE2b-256 63cc4aaa66621ee313c300b46f308efc90da058b59e023ab7ebd24a9ffc65593

See more details on using hashes here.

File details

Details for the file pydantic_panel-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_panel-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 836640a360eabc03f771de419ceffd1dba068f6d3daad9dd5d44ea7efa7d6650
MD5 52c1a00254e8b9e25f6b0a82f04d593b
BLAKE2b-256 95ff7895eb6ab1fa912e0621f1980aa8dc70fed34dfbae9078a713185bf6d349

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page