jmosbacher / pydantic-panel

Edit pydantic models with widgets from the awesome Panel package
MIT License
24 stars 3 forks source link

FYI. Panel is adding dataclass conversion. Also Pydantic -> Parameterized #28

Open MarcSkovMadsen opened 2 months ago

MarcSkovMadsen commented 2 months ago

See https://github.com/holoviz/panel/pull/6912

MarcSkovMadsen commented 1 month ago

I've tried to achieve almost feature parity. What is missing is BaseModel attributes and pandas intervals. BaseModels will be added later. I did not understand the pandas intervals support.

If you see other missing things please let me know. Thx.

image

import pydantic
import panel as pn
from typing import List
from pydantic_panel.dispatchers import infer_widget
from datetime import datetime, date
import numpy as np
import pandas as pd

pn.extension("tabulator")

class ChildModel(pydantic.BaseModel):
    name: str = "child"

class SomeModel(pydantic.BaseModel):
    name: str = "some model"

    child_field: ChildModel = ChildModel()
    date_field: date = date(2024,1,2)
    dateframe: pd.DataFrame = pd.DataFrame({"x": [1], "y": ["a"]})
    datetime_field: datetime = datetime(2024,1,1)
    dict_field: dict = {"a": 1}
    float_field: float = 42
    int_field: int = pydantic.Field(default=2, lt=10, gt=0, multiple_of=2)
    list_field: list = [1, "two"]
    nparray_field: np.ndarray = np.array([1, 2, 3])
    str_field: str = pydantic.Field(default = "to", min_length=2, max_length=10)
    tuple_field: tuple = ("a", 1)

    class Config:
        arbitrary_types_allowed = True # to allow np.array

model = SomeModel()

pydantic_panel_editor = pn.panel(model, sizing_mode="fixed") # Pydantic(model).layout[0]
print(type(pydantic_panel_editor))
panel_editor = pn.Param(pn.dataclass.to_parameterized(model))

pn.Row(
    pydantic_panel_editor,
    panel_editor,
).servable()
jmosbacher commented 1 month ago

Thank you @MarcSkovMadsen, thats awesome! I will mention this at the top of this repos readme so people arriving here know pydantic now supported natively in panel. What destination should i link to in the readme?

MarcSkovMadsen commented 1 month ago

I will put a link here once its released.