Skip to main content

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

Metadata

Release files for pydantic-panel 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-panel 0.2.0
File Size Uploaded Provenance
pydantic_panel-0.2.0.tar.gz 15.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-panel 0.2.0
File Interpreter ABI Platform
pydantic_panel-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 31.0 kB

Release files / pydantic_panel-0.2.0.tar.gz

Download URL pydantic_panel-0.2.0.tar.gz
Size 15.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2ac8a5e81f4b1c305f3b3509d5f54e2ae2a70cc861e8acef085232a168efe734
BLAKE2b-256 checksum
How to use checksums
63cc4aaa66621ee313c300b46f308efc90da058b59e023ab7ebd24a9ffc65593
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.8

Release files / pydantic_panel-0.2.0-py3-none-any.whl

Download URL pydantic_panel-0.2.0-py3-none-any.whl
Size 15.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
836640a360eabc03f771de419ceffd1dba068f6d3daad9dd5d44ea7efa7d6650
BLAKE2b-256 checksum
How to use checksums
95ff7895eb6ab1fa912e0621f1980aa8dc70fed34dfbae9078a713185bf6d349
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.8

Security for pydantic-panel 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page