Very good questions, will get back to
Very good questions, will get back to you on this.
2 Replies
I've created an issue for this https://github.com/widgetti/solara/issues/637 it shows how you can do this today, but also lays out some issue. Let me know what you think.
maartenbreddels
<:issue_open:882464248951877682> [widgetti/solara] Batch update: automatically?
Current situation
Currently, doing two state changes in a callback, will result in two render passes. If rendering is seen as a side-effect, rendering should only happen after the event handler is finished.
The workaround to get batch updates in event handlers for now is: (not a public solara API, might break in the future):
This has the upside of being explicit and allowing people to opt out (by simply not doing this).
Making this the default in Solara 2.0?
If we make batch updates in event handlers the default behaviour, it would be difficult to opt out (I cannot think of a good name or a way to signal this as a user).
Better API in 2.0?
Instead of making it a default and not easy to use (you have to get a handle to the render-context from the main render loop), we can think of a
solara.batch_update()
function/context-manager that more explicitly conveys intent.
Consistency with tasks
If callbacks do automatic batch updating, people might expect a task to do the same. Should we even allow people to update reactive variables from a task, or should we only allow a return value (force them to be pure)? The progress value would then be an exception to this, since that is typically updated in a loop:
```python
import time
import solara
from solara.lab import task
@task
def my_calculation():
total = 0
for i in range(10):
# mutate progress in the task
my_calculation.progress = (i + 1) * 10.0
time.sleep(0.4)
if not my_calculation.is_current():
# A new call was made before this call was finished
return
total += i**2
return total
@solara.component
def Page():
solara.Button("Run calculation", on_click=my_calculation)
solara.ProgressLinear(my_calculation.progress if my_calculation.pend...Created