Py.Cafe

maartenbreddels/

solara-rerender-input-bug

Demonstrates a bug with solara updating the InputInt with the state value aggresively

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
import time
from datetime import datetime
import solara
import asyncio


@solara.component
def TimerComponent():
    reactive_text = solara.use_reactive("")
    print(f"Timer Component Update: {reactive_text.value}")

    async def _increment_counter():
        while True:
            reactive_text.value = datetime.now().strftime("%Y-%m-%D %H:%M:%S")
            await asyncio.sleep(1)

    solara.Text(text=reactive_text.value)
    solara.lab.use_task(_increment_counter, dependencies=[])


@solara.component
def InputComponent():
    v1, set_v1 = solara.use_state(1)
    reactive_int_2 = solara.use_reactive(2)
    v3, set_v3 = solara.use_state(3)
    print(f"Input Component Update: {v1}, {reactive_int_2.value}")
    solara.InputInt(label="Input 1", value=v1, on_value=set_v1)  # will overwrite with the current value
    solara.InputInt(label="Input 2", value=reactive_int_2) # same
    solara.v.TextField(v_model=v3, on_v_model=set_v3)

@solara.component
def Page():
    TimerComponent()
    InputComponent()


Page()