Py.Cafe

maartenbreddels/

solara-key-usage

Random Card Generator

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
35
36
37
38
39
40
41
42
43
import solara
import random


@solara.component
def RandomCard(idx):
    def report():
        print("RandomCard component instance created: ", idx)
        def cleanup():
            print("RandomCard component instance destroyed: ", idx)
        return cleanup
    solara.use_effect(report, [])

    print("RandomCard componnent instance rendered ", idx)

    content = random.random()
    with solara.Card("Random"):
        solara.Text(f"Card {idx}, content: {content}")


@solara.component
def Page():
    use_key = solara.use_reactive(True)
    button_count = solara.use_reactive(3)
    order = solara.use_reactive(list(range(button_count.value)))
    print("Page component instance rendered")



    def shuffle():
        order.set(random.sample(order.value, len(order.value)))

    with solara.Column():
        for i in order.value:
            if use_key.value:
                RandomCard(i).key(i)
            else:
                RandomCard(i)

    solara.Button("More Buttons!", color="primary", on_click=lambda: button_count.set(button_count.value + 1))
    solara.Button("Shuffle", color="primary", on_click=shuffle)
    solara.Switch(label="use key", value=use_key)