class MyObject(event.Component): att = event.Attribute() # Props to test basic stuff foo = event.AnyProp(6, settable=True, doc='can be anything') bar = event.StringProp('xx') # not settable # Props to test array mutations eggs = event.ListProp([], settable=True) eggs2 = event.ListProp(settable=True) eggs3 = event.ListProp([3, 4]) # All kinds of props, defaults anyprop = event.AnyProp(doc='can be anything', settable=True) boolprop = event.BoolProp(settable=True) tristateprop = event.TriStateProp(settable=True) intprop = event.IntProp(settable=True) floatprop = event.FloatProp(settable=True) stringprop = event.StringProp(settable=True) tupleprop = event.TupleProp(settable=True) listprop = event.ListProp(settable=True) dictprop = event.DictProp(settable=True) componentprop = event.ComponentProp(settable=True) # can be None # nullprop = event.NullProp(None, settable=True) # eitherprop = event.EitherProp(event.IntProp, event.NoneProp) _privateprop = event.IntProp(settable=True)
class Node(event.Component): val = event.IntProp(settable=True) parent = event.ComponentProp(settable=True) children = event.TupleProp(settable=True) @event.reaction('parent.val') def handle_parent_val(self, *events): xx = [] for ev in events: if self.parent: xx.append(self.parent.val) else: xx.append(None) print('parent.val ' + ', '.join([str(x) for x in xx])) @event.reaction('children*.val') def handle_children_val(self, *events): xx = [] for ev in events: if isinstance(ev.new_value, (int, float)): xx.append(ev.new_value) else: xx.append(None) print('children.val ' + ', '.join([str(x) for x in xx]))
class MyDefaults(event.Component): # Custom defaults anyprop2 = event.AnyProp(7, doc='can be anything') boolprop2 = event.BoolProp(True) intprop2 = event.IntProp(-9) floatprop2 = event.FloatProp(800.45) stringprop2 = event.StringProp('heya') tupleprop2 = event.TupleProp((2, 'xx')) listprop2 = event.ListProp([3, 'yy']) dictprop2 = event.DictProp({'foo':3, 'bar': 4}) componentprop2 = event.ComponentProp(None)
class Example(event.Component): foo = event.AnyProp(settable=True, doc='This can be anything.') bar = event.IntProp(10, doc='This is an int.') eggs = event.TupleProp(settable=True) @event.action def raise_the_bar(self): """ Action to change the value of bar. """ self._mutate_bar(self.bar + 1) @event.reaction def _report(self): # This gets automatically called when any of the used properties change print('foo is', self.foo) print('bar is', self.bar) print('eggs is', self.eggs)
class Tester(event.Component): children = event.TupleProp(settable=True) @event.reaction('children**.foo') def track_deep(self, *events): for ev in events: if ev.new_value: print(ev.new_value) @event.action def set_foos(self, prefix): for i, child in enumerate(self.children): child.set_foo(prefix + str(i)) for j, subchild in enumerate(child.children): subchild.set_foo(prefix + str(i) + str(j)) @event.action def make_children1(self): t1 = TestOb() t2 = TestOb() t1.set_children((TestOb(), )) t2.set_children((TestOb(), )) self.set_children(t1, t2) @event.action def make_children2(self): for i, child in enumerate(self.children): child.set_children(child.children + (TestOb(), )) @event.action def make_children3(self): # See issue #460 t = TestOb() my_children = self.children self.set_children(my_children + (t, )) for i, child in enumerate(my_children): child.set_children(child.children + (t, )) self.set_children(my_children)
class MyComponent(event.Component): a = event.AnyProp() aa = event.TupleProp()
class TestOb(event.Component): children = event.TupleProp(settable=True) foo = event.StringProp(settable=True)
class Example(ui.Widget): persons = event.TupleProp((), doc=""" People to show cards for""") first_name = event.StringProp('', settable=True) last_name = event.StringProp('', settable=True) @event.action def add_person(self, name, info): """ Add a person to our stack. """ ppl = list(self.persons) ppl.append((name, info)) self._mutate_persons(ppl) def _button_clicked(self, *events): self.add_person(self.first_name, self.last_name) def _render_dom(self): """ This function gets automatically called when needed; Flexx is aware of what properties are used here. """ # Create form elements form_nodes = [ ui.create_element( 'div', {'class': 'form-group mb-2'}, ui.create_element( 'input', { 'class': 'form-control', 'id': 'inputFirstName', 'oninput': lambda e: self.set_first_name(e.target.value) }, 'First name')), ui.create_element( 'div', {'class': 'form-group mx-sm-3 mb-2'}, ui.create_element( 'input', { 'class': 'form-control', 'id': 'inputLastName', 'oninput': lambda e: self.set_last_name(e.target.value) }, 'Last name')), ui.create_element('button', { 'class': 'btn btn-primary mb-2', 'onclick': self._button_clicked }, 'Submit'), ] # Create virtual DOM nodes for all persons. We use bootstrap cards card_nodes = [] for name, info in self.persons: person_node = ui.create_element( 'div', {'class': 'card'}, ui.create_element( 'div', {'class': 'card-body'}, ui.create_element('h5', {'class': 'card-title'}, name), ui.create_element('p', {'class': 'card-text'}, info), )) card_nodes.append(person_node) # Compose finaly DOM tree return ui.create_element( 'div', {}, ui.create_element('div', {'class': 'form-inline'}, form_nodes), *card_nodes)