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

DataclassTransform does not serialise on Optional annotation #345

Open
tomblazejewski opened this issue Jul 28, 2024 · 1 comment
Open

Comments

@tomblazejewski
Copy link

tomblazejewski commented Jul 28, 2024

dash-core-components 2.0.0
dash-extensions      1.0.18
dash-html-components 2.0.0
dash-table           5.0.0

When modifying the example from this link: https://www.dash-extensions.com/transforms/dataclass_transform as:

from datetime import datetime
from typing import Optional

from dash_extensions.enrich import (
    DashProxy,
    DataclassTransform,
    Input,
    Output,
    State,
    dcc,
    html,
)


@dataclass
class Person:
    name: str
    date_of_birth: datetime


app = DashProxy(transforms=[DataclassTransform()])
app.layout = html.Div(
    [
        dcc.Input(id="name", value="John Doe"),
        dcc.DatePickerSingle(id="picker", date=datetime(1990, 1, 1)),
        dcc.Store(id="store"),
        html.Button("Submit", id="btn"),
        html.Div(id="log"),
    ]
)


@app.callback(
    Input("btn", "n_clicks"),
    State("name", "value"),
    State("picker", "date"),
    Output("store", "data"),
)
def submit(_, name, date):
    dt = datetime.fromisoformat(date)
    return Person(name, dt)  # no (manual) serialization here


@app.callback(
    Output("log", "children"),
    Input("store", "data"),
)
def log(person: Optional[Person]):
    return f"{person.name} was born on {person.date_of_birth}"  # no (manual) deserialization here


if __name__ == "__main__":
    app.run_server(port=8060)

The object returned from the "submit" function is a dict (not serialised).
image

@tomblazejewski
Copy link
Author

I believe I got it to work by changing the _try_load method for DataclassTransform - if this is something desirable I could create a PR

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

1 participant