Dash pydantic form

Hi all :wave:

I have created a package to make it easy to create and validate forms with Dash and Pydantic: dash-pydantic-form It leverages Pydantic 2 and dash mantine components (0.14) for the heavy lifting and allows auto-generated, customisable forms.

Update: here is the link to the docs site

Some of its features include:

  • Handling of nested models and list of nested models
  • Form sections
  • Conditional visibility
  • Retrieval of the whole form data from a single entrypoint

Here is an example of simple app with a form, showing validation errors when there are any. Check out usage.py in the repo for the code!

demo-usage

Also the Readme should have the minimum to get started but the whole API is not entirely documented just yet :slightly_smiling_face:

I hope this can be useful to the community!

20 Likes

Thank you for creating and sharing this, @RenaudLN

Is this a side project or do you plan to use it for work as well?

1 Like

I’m going to use this for work! Past 3 months has been hell, creating constant forms for sites, users, forms within forms all a headache with many long callbacks. This looks awesome, excited to start using this inplace :star2: take my star and thanks / congratulations on building out this project.

4 Likes

We’re already using this for work yes :slight_smile:

2 Likes

Wow - I was working on the same thing, but your solution already much more polished! :smiley: Do you have / plan on adding support for type unions? Let’s say I have the following models,

class Cat(BaseModel):
    animal_type: Literal["cat"] = "cat"
    meows: bool = True

class Dog(BaseModel):
    animal_type: Literal["dog"] = "dog"
    barks: bool = True

Animal = Union[Cat, Dog]

class Pet(BaseModel):
    name: str
    animal: Animal = Field(discriminator="animal_type")

I would then imagine a form, that would have,

  • A text input for name
  • A selector for animal (with all possible selections, here cat and dog, available)

After selecting the animal, the form would extend itself with fields relevant to the selected animal (e.g. barks).

In any case - great work! I think this package will be usefull for a lot of people :slight_smile:

I was wondering about a good way to do type unions. For now what I’ve been doing is add a type attribute to my models, then the fields that are related to one type or another are conditionally visible based on the value of type.

It would be great to support something more elegant though, happy to take suggestions / PRs!

I’ll try to find time to make a PR. I have the basic functionality working in my POC, but it’s not pretty :smile:

Got something working for that, will test some more and release :slight_smile:

discriminated-union

1 Like

Curious why the python version is capped at Python <3.13,>=3.10 . I thought most of plotly infrastructure is currently based and being built on <3.8 which is what I build most of my projects with. Just was wondering if this is a limitation on pydantic and if their where plans to update the projects python version.

I built this using some of the more recent Python typing syntax which doesn’t work with 3.8. It could probably all be converted to work with older versions but would be a bit of work.

Sounds :+1:, i might jump into the repo and help when i find some time. Would be nice to expand out the coverage a bit. Thanks and nice job on the usage.py really great build of an example of the project

2 Likes

I thought the last few lines of the CI config file (plotly.py/.circleci/config.yml at master · plotly/plotly.py · GitHub) indicated what versions of Python plotly is currently tested against. This suggests 3.8 through to 3.12 for most tests, the exceptions being orca (3.8 only) and percy (3.9 only)

Was likely a typo where PipInstall meant >=3.8

Yeah, it was a typo on my part meant >=3.8

This is really slick, I have a couple of forms that I think I could replace with this.

It sooooo beats what I was doing. :slight_smile:

Just curious, is there a way to customize the grid layout?

For now it’s pretty basic, there are 4 columns ( which shrinks to only 1 column on mobile size) so you can pick between 1 and 4 columns for each field. Could definitely add more customisation options there.

So, each column would then get a span of 3?

Hmm no it’s just 4 columns not the 12 columns system

1 Like

I just toke the package for a spin. It’s amazing! It saves so much code - and the resulting code is a lot easier to read, clearly expressing the intent :smiley:

4 Likes

I attempted to work on getting it setup for python >= 3.8.18, was able to get most setup but I’m stuck on the model_form.py specifically the 3 lines of code with: item: Union[BaseModel, Type[BaseModel]] idk if anyone would be interested at taking a crack at it but I have a fork ready to go: GitHub - pip-install-python/dash-pydantic-form: Goal to update to 3.8 and help maintain.