Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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]))
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
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]))
Exemplo n.º 12
0
class MyComponent3(event.Component):

    foo = event.IntProp(settable=True)

    @event.reaction
    def react2foo(self):
        print('foo is', self.foo)
Exemplo n.º 13
0
    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)
Exemplo n.º 14
0
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])
Exemplo n.º 15
0
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)
Exemplo n.º 16
0
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)
Exemplo n.º 17
0
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)
Exemplo n.º 18
0
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
Exemplo n.º 19
0
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)
Exemplo n.º 20
0
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))
Exemplo n.º 21
0
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])
Exemplo n.º 22
0
class MyPropClass1(app.PyComponent):
    foo = event.IntProp(1, settable=True)
Exemplo n.º 23
0
class MyObject2(event.Component):

    foo = event.IntProp(settable=True)
    bar = event.IntProp(7, settable=True)
Exemplo n.º 24
0
    class TestComponent1(event.Component):

        foo = event.IntProp()
Exemplo n.º 25
0
 class SharedComponent(event.Component):
     foo = event.IntProp(0, settable=True)
Exemplo n.º 26
0
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)
Exemplo n.º 27
0
class MyObject4(event.Component):

    bar = event.IntProp(7, settable=True)