Py.Cafe

maartenbreddels/

quixotic-husband

Exception Handler Using Solara

DocsPricing
  • app.py
  • requirements.txt
app.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
import solara

set_fail = None
clear = None


@solara.component
def UnstableComponent(number: int):
    if number == 3:
        raise Exception("I do not like 3")
    return solara.Text(f"You picked {number}")


@solara.component
def Page():
    value, set_value = solara.use_state(1)
    value_previous = solara.use_previous(value)
    exception, clear_exception = solara.use_exception()
    print(exception)
    if exception:
        def reset():
            set_value(value_previous)
            clear_exception()

        # bug in reacton, can not be a function component, only a widget component
        with solara.v.Container():
            solara.Text("Should show this")
            solara.Text("Exception: " + str(exception))
            solara.Button(label="Go to previous state", on_click=reset)
    else:
        with solara.Column(margin=12):
            solara.IntSlider(value=value, min=0, max=10, on_value=set_value, label="Pick a number, except 3")
            UnstableComponent(value)