maxfordham / ipyautoui

create ipywidgets user input form pydantic model or jsonschema
https://maxfordham.github.io/ipyautoui/
42 stars 4 forks source link

docstrings, __init__ *args and **kwargs and traitlets #122

Closed jgunstone closed 1 year ago

jgunstone commented 1 year ago

hwo to deal with: docstrings, __init__ *args and **kwargs and traitlets

generally within the ipywidget universe, arguments are just traitlets that are piped to the Javascript side of the UI where they are observed to trigger events on_change, this means they can also be edited during the lifetime of the object.

For this reason, I guess, the arguments are defined something like:

import traitlets as tr

class MyClass(tr.HasTraits):
    my_arg = tr.Bool()
    def __init__(self, **kwargs):
        super(MyClass).__init__(*args, **kwargs)

^ kwargs then set the traitlets on instantiation.

how do others do it?


ipycanvas

in ipycanvas it looks like something clever is happening as documentation accompanies the traitlets definitions in the source code:

https://github.com/martinRenou/ipycanvas/blob/d9ee0a16cf0011978b5a5eedb165dbca564eb8c4/ipycanvas/canvas.py#L462-L483

i.e. the #: lines in the code above which then makes it into the API Documentation


ipydatagrid

https://github.com/bloomberg/ipydatagrid/blob/848238356083575c1cef4b150137edfdf5c04b16/ipydatagrid/datagrid.py#L213-L323

the above simply documents all the traitlets attributes in the docstring. this is nice and simple, and would also make it easier to document traitlets from child classes. This isn't being used to produce API documentation though.


One annoying thing about both of these approaches is that intellisense doesn't work as because the args aren't explicitly defined in the function call.

In reacton-ipycanvas there is a generate function that appears to read the ipycanvas package and convert the traitlets definition into function calls, thus supporting intellisense.

jgunstone commented 1 year ago

implemented something closest to the the ipydatagrid approach described above here: https://github.com/maxfordham/ipyautoui/pull/125

did it for AutoUi / AutoObject only... need to apply the same rationale throughout the repo

jgunstone commented 1 year ago

throughout the package traits are now used rather than init args. most similar to ipydatagrid. still interested in the the reacton generate function for nesting this in solara world

merged to main in this PR https://github.com/maxfordham/ipyautoui/pull/199