ipyvue icon indicating copy to clipboard operation
ipyvue copied to clipboard

Add a "computed" observer

Open ggael opened this issue 7 months ago • 0 comments

I've started to write a computed decorator that behave like computed variables in vue, and I'm wondering whether this could be a welcome addition to vuejs?

It is based on traitlets.ObserveHandler. To make it work I've also borrowed the as_refs and create_ref_and_observe functions from VueTemplateWidget.py. Exposing those functions or integration this decorator in ipyvue would avoid code duplication.

Contrary to vuejs, dependencies must be explicitly listed.

Usage example:

from vue_computed import computed

class MyComponent(v.VuetifyTemplate):
    val1 = traitlets.Float(2.3).tag(sync=True)
    val2 = traitlets.Float(4.5).tag(sync=True)
    
    @computed('val1','val2')
    def vue_val3(self) -> traitlets.Float:
        return self.val1 + self.val2

    @traitlets.default('template')
    def _template(self):
        return '''
        <template>
            <div>{{val3}}</div>
        </template>
        '''

vue_computed.py.gz

ggael avatar Jul 01 '24 09:07 ggael