magicgui icon indicating copy to clipboard operation
magicgui copied to clipboard

Support pydantic types

Open tlambert03 opened this issue 4 years ago • 6 comments

As @sofroniewn suggested, pydantic's constrained types make for a natural way to express things like ranges and widget parameters as type annotations. We should detect and support pydantic types

tlambert03 avatar Jan 14 '21 02:01 tlambert03

Currently I am building my user interface using the direct widget API and noticed I should avoid repetition by keeping widget constraints (type, min/max) in a central place, my data model which is a Pydantic class and has all these constraints. As a Pydantic class (from which I can generate documentation) it also can hold widget texts (Pydantic title) and tooltips (Pydantic description). Basically I ended up with rather repetitive code that feeds each Pydantic field property to an argument of the widget constructor.

It would be nice to generate a magicgui either from a pydantic class (like magic-class from basic dataclasses), or to have something similar to widgets.create_widget() with a Pydantic field as single required argument.

aeisenbarth avatar Nov 13 '21 16:11 aeisenbarth

thanks @aeisenbarth ... I have a local branch where I started to implement this. will try to get back on it soon

tlambert03 avatar Nov 13 '21 17:11 tlambert03

Seeing the discussions in #474 and #475, is the plan still to have first-class support for Pydantic classes? Or should I rather look into using @dataclass or @guiclass?

imagejan avatar Feb 20 '24 14:02 imagejan

it's basically possible already with the logic underlying guiclass, but not everything is public yet. Can you read through this topic on imagesc and let me know what your specific desired use case would be? https://forum.image.sc/t/building-a-napari-widget-from-a-pydantic-model/90257/5

tlambert03 avatar Feb 20 '24 16:02 tlambert03

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/using-magicgui-to-create-pydantic-class-instances/92520/1

imagesc-bot avatar Feb 21 '24 14:02 imagesc-bot

Can you read through this topic on imagesc and let me know what your specific desired use case would be? forum.image.sc/t/building-a-napari-widget-from-a-pydantic-model/90257/5

Thanks @tlambert03, that's a very useful forum post! I opened a new topic (Using magicgui to create pydantic class instances) to describe my intended use case and post a few more questions 🙂.

imagejan avatar Feb 21 '24 14:02 imagejan