Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MarcSkovMadsen opened this issue Jul 15, 2024 · 3 comments

Comments

@MarcSkovMadsen
Copy link
Collaborator

See holoviz/panel#6912

@MarcSkovMadsen
Copy link
Collaborator Author

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
Copy link
Owner

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
Copy link
Collaborator Author

I will put a link here once its released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants