errors.pyΒΆ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
App that can be used to generate errors on the Python and JS side. These
errors should show tracebacks in the correct manner (and not crash the app
as in #164).
"""

from flexx import app, event, ui


class ErrorsPy(app.PyComponent):

    def init(self):
        self.js = ErrorsJS(self)

    @event.action
    def do_something_stupid(self):
        self.raise_error()

    def raise_error(self):
        raise RuntimeError('Deliberate error')

    @event.reaction('!js.b4_pointer_click')
    def error_in_Py_reaction(self, *events):
        self.raise_error()


class ErrorsJS(ui.Widget):

    def init(self, pycomponent):
        self.py = pycomponent

        with ui.VBox():
            self.b1 = ui.Button(text='Raise error in JS action')
            self.b2 = ui.Button(text='Raise error in JS reaction')
            self.b3 = ui.Button(text='Raise error in Python action')
            self.b4 = ui.Button(text='Raise error in Python reaction')
            ui.Widget(flex=1)  # spacer

    @event.action
    def do_something_stupid(self):
        self.raise_error(0)

    def raise_error(self):
        raise RuntimeError('Deliberate error')

    # Handlers for four buttons

    @event.reaction('b1.pointer_click')
    def error_in_JS_action(self, *events):
        self.do_something_stupid()

    @event.reaction('b2.pointer_click')
    def error_in_JS_reaction(self, *events):
        self.raise_error()

    @event.reaction('b3.pointer_click')
    def error_in_Py_action(self, *events):
        self.py.do_something_stupid()

    @event.reaction('b4.pointer_click')
    def error_in_Py_reaction(self, *events):
        self.emit('b4_pointer_click')


if __name__ == '__main__':
    m = app.launch(ErrorsPy, 'browser')
    app.run()