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)