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

Make it easy to use dataclass like models using familiar apis #6912

Open
wants to merge 65 commits into
base: main
Choose a base branch
from

Conversation

MarcSkovMadsen
Copy link
Collaborator

@MarcSkovMadsen MarcSkovMadsen commented Jun 11, 2024

Superseedes #6892.

Also motivated by me trying to demonstrate that you can just as well use Panel for geospatial applications as Solara by creating apps similar to https://github.com/opengeos/solara-geospatial/tree/main/pages. But currently Panel is harder to use because it requires adding more code for using observer pattern.

Scope: Currently ipywidgets, Pydantic models

Easy to view docs

Todo

  • Create design proposal
  • Get initial design feedback
  • Update design proposal
  • Implement design
  • Clean Up
    • names
    • docstrings
    • Add examples to docstring
    • missing tests
  • Make functionality covers functionality needed to wrap and share ipywidget as reusable component with Viewer interface.
  • Support ipywidgets
  • Support pydantic
  • Document
    • Update IPyWidget reference notebook
      • remove example files.
    • Create how-to guide for wrapping ipywidget using functionality in panel.ipywidget.
    • Philipp?: Fix broken dev docs: TypeError: Cannot read properties of undefined (reading 'loader').
    • Philipp?: Fix test error. I don't know how to fix the markdown docs test failure.
    • Philipp?: Fix self.param.add_parameter(parameter, param.Parameter())
    • Philipp?: Fix JSON pane updating before model issue.
    • Build documentation and test
  • Fix issues identified
    • Fix not working create_parameter of ipywidgets.
    • Fix missing pydantic docs dependency
    • Add create_parameter for pydantic to add appropriate types of parameters.
    • Fix ipywidget tuple length issue
    • Philipp?: Add sqlite as a dependency for running the ipywidgets docs in pyodide
  • Test complex use cases with multiple models and sessions
  • Final review and update

Maybe later

Promotion

Note: Features have been moved to panel.dataclass module since this video was made. WidgetViewer has been renamed to ModelViewer.

wrapping_ipywidgets.mp4

Design Principles

  • I've tried to create small pieces of functionality that build up to the simple to use ModelViewer class and create_rx function such that there are no dead ends and its testable.
  • I've tried to use naming conventions that would be general enough to accommodate similar functionality for dataclasses, Pydantic etc. one day.
  • I've not gone into creating parameters of similar types to the traits. For now that is something the developer must do if that is needed.

@MarcSkovMadsen
Copy link
Collaborator Author

Hi @philippjfr . Would you take a look at the design spec, i.e. the current files? Thanks.

panel/ipywidget.py Outdated Show resolved Hide resolved
panel/ipywidget.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jun 11, 2024

Codecov Report

Attention: Patch coverage is 94.89144% with 40 lines in your changes missing coverage. Please review.

Project coverage is 81.94%. Comparing base (7267b38) to head (d364cfe).
Report is 7 commits behind head on main.

Files Patch % Lines
panel/_dataclasses/base.py 86.71% 19 Missing ⚠️
panel/_dataclasses/ipywidget.py 86.66% 8 Missing ⚠️
panel/_dataclasses/pydantic.py 89.65% 6 Missing ⚠️
panel/tests/dataclasses/test_dataclass.py 99.00% 4 Missing ⚠️
panel/dataclass.py 97.39% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6912      +/-   ##
==========================================
+ Coverage   81.71%   81.94%   +0.22%     
==========================================
  Files         326      331       +5     
  Lines       48082    48861     +779     
==========================================
+ Hits        39292    40040     +748     
- Misses       8790     8821      +31     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

panel/ipywidget.py Outdated Show resolved Hide resolved
panel/ipywidget.py Outdated Show resolved Hide resolved
panel/ipywidget.py Outdated Show resolved Hide resolved
panel/ipywidget.py Outdated Show resolved Hide resolved
await sleep(0.250)
return json.loads(model.json())
exclude = list(layout_params)
def view_model(*args):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exclude attributes of a Pydantic model?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think there was a good reason.

@MarcSkovMadsen
Copy link
Collaborator Author

I have starting adding support for specific parameter types. Its easy. But i need to refactor the Way initial values are assigned.

Ooops, missed this. I just pushed it.

No worries. I'm happy.

@philippjfr
Copy link
Member

Not sure I love panel.dataclass naming. While yes, they're dataclass-like things they're not actually dataclasses and I'd rather focus on the functionality this provides, i.e. the fact that this provides a compatibility layer or bridge between the libraries.

@MarcSkovMadsen
Copy link
Collaborator Author

Not sure I love panel.dataclass naming. While yes, they're dataclass-like things they're not actually dataclasses and I'd rather focus on the functionality this provides, i.e. the fact that this provides a compatibility layer or bridge between the libraries.

1. panel.compat

Explanation: This name focuses on the core functionality of the module, which is to provide compatibility between different frameworks. It conveys the idea that this module helps integrate various dataclass-like libraries with HoloViz Param.

2. panel.bridge

Explanation: The term "bridge" clearly indicates that the module serves as a connector between disparate systems. It emphasizes the role of the module in linking different frameworks, highlighting its purpose without misrepresenting the nature of the classes involved.

3. panel.integration

Explanation: This name underscores the module's role in facilitating integration between HoloViz Param and other frameworks like traitlets, ipywidgets, and pydantic. It suggests a broader scope of bringing together various components, focusing on the unifying function of the module.

Each of these names highlights the module's purpose of enhancing interoperability and integration between HoloViz Param and other dataclass-like frameworks, without suggesting that the module contains actual dataclasses.

@philippjfr
Copy link
Member

Docs build now working.

@MarcSkovMadsen
Copy link
Collaborator Author

When I click the run with pyodide button I see

image

It seems we need to add sqlite as a dependency.

@MarcSkovMadsen
Copy link
Collaborator Author

MarcSkovMadsen commented Jul 14, 2024

FIXED

I can see that the ipywidgets create_parameter is not working for the example

import panel as pn
import ipyleaflet as ipyl

pn.extension("ipywidgets")

leaflet_map = ipyl.Map(zoom=4)

viewer = pn.dataclass.ModelViewer(model=leaflet_map, sizing_mode="stretch_both")
pn.Row(pn.Column(viewer.param, scroll=True), viewer, height=400).servable()

The problem is that the trait is an instance and not a type

image

If I change to use type(...)

image

then it can error

ValueError: Attribute 'length' of Tuple parameter 'Map.bounds' is not of the correct length (0 instead of 2).

Traceback (most recent call last):
  File "/home/jovyan/repos/private/panel/panel/_dataclasses/base.py", line 156, in sync_with_parameterized
    setattr(model, field, parameter_value)
  File "/home/jovyan/repos/private/panel/.venv/lib/python3.11/site-packages/traitlets/traitlets.py", line 715, in __set__
    raise TraitError('The "%s" trait is read-only.' % self.name)
traitlets.traitlets.TraitError: The "bounds" trait is read-only.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jovyan/repos/private/panel/panel/io/handlers.py", line 389, in run
    exec(self._code, module.__dict__)
  File "/home/jovyan/repos/private/panel/script.py", line 8, in <module>
    viewer = pn.dataclass.ModelViewer(model=leaflet_map, sizing_mode="stretch_both")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/repos/private/panel/panel/dataclass.py", line 159, in __init__
    super().__init__(**params)
  File "/home/jovyan/repos/private/panel/panel/viewable.py", line 302, in __init__
    super().__init__(**params)
  File "/home/jovyan/repos/private/panel/panel/dataclass.py", line 106, in __init__
    utils.sync_with_parameterized(self.model, self, names=names)
  File "/home/jovyan/repos/private/panel/panel/_dataclasses/base.py", line 159, in sync_with_parameterized
    setattr(parameterized, parameter, field_value)
  File "/home/jovyan/repos/private/panel/.venv/lib/python3.11/site-packages/param/parameterized.py", line 528, in _f
    instance_param.__set__(obj, val)
  File "/home/jovyan/repos/private/panel/.venv/lib/python3.11/site-packages/param/parameterized.py", line 530, in _f
    return f(self, obj, val)
           ^^^^^^^^^^^^^^^^^
  File "/home/jovyan/repos/private/panel/.venv/lib/python3.11/site-packages/param/parameterized.py", line 1498, in __set__
    self._validate(val)
  File "/home/jovyan/repos/private/panel/.venv/lib/python3.11/site-packages/param/parameters.py", line 1192, in _validate
    self._validate_length(val, self.length)
  File "/home/jovyan/repos/private/panel/.venv/lib/python3.11/site-packages/param/parameters.py", line 1185, in _validate_length
    raise ValueError(
ValueError: Attribute 'length' of Tuple parameter 'Map.bounds' is not of the correct length (0 instead of 2).

@MarcSkovMadsen
Copy link
Collaborator Author

MarcSkovMadsen commented Jul 14, 2024

FIXED

Pydantic seems to missing as a dependency for the docs

image

@MarcSkovMadsen
Copy link
Collaborator Author

MarcSkovMadsen commented Jul 14, 2024

FIXED

Ahh. The dataclass functionality for Pydantic does not add specific parameter types yet.

@MarcSkovMadsen
Copy link
Collaborator Author

MarcSkovMadsen commented Jul 15, 2024

Fixed by adding ModelForm

Our code instantiates pydantic models. Often they don't have default values. Instead initial values are required.

This makes it a bit hard to use our features. Especially for creating forms with validation which a popular use case (c.f. pydantic-panel, streamlit-pydantic, dash-pydantic-form).

For example the below test will currently raise an exception

def test_to_parameterized_no_defaults():
    from pydantic import BaseModel
    class ExampleModel(BaseModel):
        some_text: str
        some_number: int
    
    class ExampleModelParameterized(ModelParameterized):
        _model_class = ExampleModel

    ExampleModelParameterized()

Something like the default_values of the below code is currently required to work around this

import panel as pn
from pydantic import BaseModel
import param
from panel._dataclasses.pydantic import PydanticUtils

pn.extension()

class ModelForm(pn.viewable.Viewer):
    value = param.ClassSelector(class_=BaseModel, allow_None=True)

    submit_button_visible = param.Boolean(default=True, label="Show Submit Button")

    def __init__(self, model_class, submit_button_visible: bool=True, **params):
        self._model_class = model_class
        self._fields = list(model_class.model_fields.keys())
                
        super().__init__(**params)

        fields = model_class.model_fields
        default_values = {field: PydanticUtils.create_parameter(model_class, field).default for field, info in fields.items() if info.is_required()}
        model=model_class(**default_values)
        self._model = model=model_class(**default_values)
        parameters = list(ExampleModel.model_fields.keys())
        parameterized = pn.dataclass.to_viewer(model)
        parameterized.param.watch(self._update_value_on_parameter_change, parameters)
        submit = pn.widgets.Button(name="Submit", button_type="primary", on_click=self._update_value, visible=self.param.submit_button_visible)
        
        self._form = pn.Column(
            pn.Param(parameterized, parameters=parameters),
            submit)

    def _update_value(self, *args):
        self.value = self._model.copy(deep=True)

    def _update_value_on_parameter_change(self, *args):
        if not self.submit_button_visible:
            self.value = self._model.copy(deep=True)

    def __panel__(self):
        return self._form

    @param.depends("value")
    def value_as_dict(self):
        if not self.value:
            return {}
        return self.value.dict()

class ExampleModel(BaseModel):
    some_text: str
    some_number: int
    some_boolean: bool

form = ModelForm(model_class=ExampleModel)
    

pn.Column(form, pn.pane.JSON(form.value_as_dict), form.param.submit_button_visible).servable()

return tuple(rx_values)


class ModelForm(Viewer):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the ModelForm to support the form use case. This is the use case that pydantic-panel, streamlit-pydantic and dash-pydantic-form all support.

Its really a more general request as in #3687. We can solve this is several ways:

  • not solve :-)
  • document how to solve
  • make general Panel form functionality
  • add this dataclass specific form functionality.

What do you recommend @philippjfr ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its worth noting that I achieve the form functionality a bit differently than the packages mentioned above. They translate pydantic fields to widgets while I translate pydantic fields to Parameters which Panel can translate to widgets. I.e. in my version we instantiate a Parameterized and then use Param. In the packages they can avoid instantiating a model before the user has filled out the form including required values.

Is my version the right way to go @philippjfr ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! And the form use case should definitely be supported.

@MarcSkovMadsen
Copy link
Collaborator Author

MarcSkovMadsen commented Jul 17, 2024

I've tried to achieve almost feature parity with pydantic-panel.

What is missing is Pydantic BaseModel attributes and pandas intervals.

  • BaseModels will be added later
  • I did not understand the pandas intervals support.

image

pip install pydantic-panel
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()

@MarcSkovMadsen
Copy link
Collaborator Author

What else should be done here?

@philippjfr
Copy link
Member

I'm still fully on board with the aims of this PR but it's simply too large a PR to make it into 1.5.0 at this point.

@philippjfr philippjfr modified the milestones: v1.5.x, v1.6.0 Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants