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 MyObject1(event.Component): foo = event.IntProp(settable=True) bar = event.IntProp(7, settable=True) @event.reaction def report(self): print(self.foo, self.bar)
class MyObject3(event.Component): foo = event.IntProp(settable=True) bar = event.IntProp(7, settable=True) @event.reaction('!spam', mode='auto') def report(self, *events): assert len(events) > 0 print(self.foo, self.bar)
class MyObject_init(event.Component): foo = event.IntProp(settable=True) bar = event.IntProp(7, settable=True) spam = event.IntProp(settable=False) @event.reaction('foo', 'bar') def _report(self, *events): print('r ' + ', '.join(['%s:%i->%i' % (ev.type, ev.old_value, ev.new_value) for ev in events]))
class MyObject2(event.Component): foo = event.IntProp(settable=True) bar = event.IntProp(7, settable=True) @event.reaction def report(self, *events): assert len(events) == 0 # of course, you'd leave them out in practice print(self.foo, self.bar)
class MyObject1(event.Component): foo = event.IntProp(settable=True) bar = event.IntProp(settable=True) @event.reaction('foo') def report1(self, *events): print('foo', self.foo) @event.reaction('bar', mode='greedy') def report2(self, *events): print('bar', self.bar)
class RecursiveActions(event.Component): p1 = event.IntProp() p2 = event.IntProp() @event.action def set1(self, v): self.set2(v + 1) self._mutate_p1(v) @event.action def set2(self, v): self.set1(v - 1) self._mutate_p2(v)
class MyJComponent1(JsComponent): CSS = "xx" foo = event.IntProp() foo2 = event.IntProp() @event.action def increase_foo(self): self._mutate_foo(self.foo + 1) @event.reaction('foo') def track_foo(self, *events): pass
class CompWithInit2(event.Component): foo1 = event.IntProp(1) foo2 = event.IntProp(2, settable=True) foo3 = event.IntProp(3) def init(self, set_foos): if set_foos: self._mutate_foo1(11) self.set_foo2(12) self.set_foo3(13) @event.action def set_foo3(self, v): self._mutate_foo3(v+100)
class Relay(event.Component): number_of_connections = event.IntProp(settable=True) def init(self): self.update_number_of_connections() self.refresh() @app.manager.reaction('connections_changed') def update_number_of_connections(self, *events): n = 0 for name in app.manager.get_app_names(): sessions = app.manager.get_connections(name) n += len(sessions) self.set_number_of_connections(n) @event.emitter def system_info(self): return dict( cpu=psutil.cpu_percent(), mem=psutil.virtual_memory().percent, sessions=self.number_of_connections, total_sessions=app.manager.total_sessions, ) def refresh(self): self.system_info() asyncio.get_event_loop().call_later(1, self.refresh)
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 MyComponent3(event.Component): foo = event.IntProp(settable=True) @event.reaction def react2foo(self): print('foo is', self.foo)
class MyComp1(event.Component): foo = event.IntProp(0, settable=True) @event.reaction('foo') def on_foo(self, *events): for ev in events: res.append(ev.new_value)
class MyComponent2(event.Component): foo = event.IntProp(settable=True) @event.reaction('foo') def on_foo(self, *events): print([ev.new_value for ev in events])
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 CompWithInit4(event.Component): a_prop = event.IntProp(settable=True) def init(self, other, value): self.set_a_prop(value) other.set_a_prop(value) @event.action def create(self, other, value): self.set_a_prop(value) CompWithInit4(other, value)
class MyComponent4(event.Component): foo = event.IntProp(settable=True) other = event.ComponentProp(settable=True) @event.reaction('other.foo') def on_foo_explicit(self, *events): print('other foo is', events[-1].new_value) @event.reaction def on_foo_implicit(self): if self.other is not None: print('other foo is implicit', self.other.foo)
class MyComponent1(event.Component): foo = event.IntProp(settable=True) @event.reaction('foo') def on_foo(self, *events): pass def react2foo(self): self.foo + 1 @event.reaction def on_foo_implicit(self): self.foo
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 Test(event.Component): foo = event.IntProp(0, settable=True) @event.reaction def react_to_foo_a(self): print('A: foo changed to %i' % self.foo) @event.reaction('foo') def react_to_foo_b(self, *events): # This function print('B: foo changed from %i to %i' % (events[0].old_value, events[-1].new_value)) @event.reaction('foo') def react_to_foo_c(self, *events): print('C: foo changed:') for ev in events: print(' from %i to %i' % (ev.old_value, ev.new_value))
class PyComponentA(app.PyComponent): foo = event.IntProp(settable=True) sub = event.ComponentProp(settable=True) @event.action def greet(self, msg): print('hi', msg) @event.emitter def bar_event(self, v): return dict(value=v) @event.reaction def _on_foo(self): if self.sub is not None: print('sub foo changed', self.sub.foo) @event.reaction('bar_event') def _on_bar(self, *events): print('got bar event', [ev.value for ev in events])
class MyPropClass1(app.PyComponent): foo = event.IntProp(1, settable=True)
class MyObject2(event.Component): foo = event.IntProp(settable=True) bar = event.IntProp(7, settable=True)
class TestComponent1(event.Component): foo = event.IntProp()
class SharedComponent(event.Component): foo = event.IntProp(0, settable=True)
class LeafletWidget(ui.Widget): """ A widget that shows a slippy/tile-map using Leaflet. """ layers = event.ListProp([], doc=""" List of tilemap layer tuples: (url, 'Layer'). """) zoom = event.IntProp(8, settable=True, doc=""" Zoom level for the map. """) min_zoom = event.IntProp(0, settable=True, doc=""" self zoom level for the map. """) max_zoom = event.IntProp(18, settable=True, doc=""" Maximum zoom level for the map. """) center = event.FloatPairProp((5.2, 5.5), settable=True, doc=""" The center of the map. """) show_layers = event.BoolProp(False, settable=True, doc=""" Whether to show layers-icon on the top-right of the map. """) show_scale = event.BoolProp(False, settable=True, doc=""" Whether to show scale at bottom-left of map. """) @event.action def add_layer(self, url, name=None): """ Add a layer to the map. """ # Avoid duplicates self.remove_layer(url) if name: self.remove_layer(name) # Add layer layers = self.layers + [(url, name or 'Layer')] self._mutate_layers(layers) @event.action def remove_layer(self, url_or_name): """ Remove a layer from the map by url or name. """ layers = list(self.layers) for i in reversed(range(len(layers))): if url_or_name in layers[i]: layers.pop(i) self._mutate_layers(layers) def _create_dom(self): global L, document node = document.createElement('div') self.mapnode = document.createElement('div') node.appendChild(self.mapnode) self.mapnode.id = 'maproot' self.mapnode.style.position = 'absolute' self.mapnode.style.top = '0px' self.mapnode.style.left = '0px' self.map = L.map(self.mapnode) self.map.on('zoomend', self.map_handle_zoom) self.map.on('moveend', self.map_handle_move) self.map.on('click', self.map_handle_mouse) self.map.on('dblclick', self.map_handle_mouse) # Container to keep track of leaflet layer objects self.layer_container = [] self.layer_control = L.control.layers() self.scale = L.control.scale({'imperial': False, 'maxWidth': 200}) # Set the path for icon images L.Icon.Default.prototype.options.imagePath = '_data/shared/' return node def map_handle_zoom(self, e): global isNaN zoom = self.map.getZoom() if isNaN(zoom): return if zoom != self.zoom: self.set_zoom(zoom) def map_handle_move(self, e): center_coord = self.map.getCenter() center = center_coord.lat, center_coord.lng if center != self.center: self.set_center(center) def map_handle_mouse(self, e): latlng = [e.latlng.lat, e.latlng.lng] xy = [e.layerPoint.x, e.layerPoint.y] self.mouse_event(e.type, latlng, xy) @event.emitter def mouse_event(self, event, latlng, xy): return {'event': event, 'latlng': latlng, 'xy': xy} @event.reaction def __handle_zoom(self): self.map.setZoom(self.zoom) @event.reaction def __handle_min_zoom(self): self.map.setMinZoom(self.min_zoom) @event.reaction def __handle_max_zoom(self): self.map.setMaxZoom(self.max_zoom) @event.reaction def __handle_center(self): self.map.panTo(self.center) @event.reaction def __handle_show_layers(self): if self.show_layers: self.map.addControl(self.layer_control) else: self.map.removeControl(self.layer_control) @event.reaction def __handle_show_scale(self): if self.show_scale: self.map.addControl(self.scale) else: self.map.removeControl(self.scale) @event.reaction def __size_changed(self): size = self.size if size[0] or size[1]: self.mapnode.style.width = size[0] + 'px' self.mapnode.style.height = size[1] + 'px' # Notify the map that it's container's size changed self.map.invalidateSize() @event.reaction def __layers_changed(self): global L for layer in self.layer_container: self.layer_control.removeLayer(layer) if self.map.hasLayer(layer): self.map.removeLayer(layer) for layer_url, layer_name in self.layers: if not layer_url.endswith('.png'): if not layer_url.endswith('/'): layer_url += '/' layer_url += '{z}/{x}/{y}.png' new_layer = L.tileLayer(layer_url) self.layer_container.append(new_layer) self.map.addLayer(new_layer) self.layer_control.addOverlay(new_layer, layer_name)
class MyObject4(event.Component): bar = event.IntProp(7, settable=True)