Very good questions, will get back to

Very good questions, will get back to you on this.
2 Replies
MaartenBreddels
MaartenBreddelsOP8mo ago
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.
Monty Python
Monty Python8mo ago
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):
import solara
import reacton.core


text = solara.reactive("Initial text")

@solara.component
def Page():
rc = reacton.core.get_render_context()
print("Render with: ", text.value)
def change_text():
with rc:
text.value = "Should never be rendered visible"
text.value = "Updated text"

solara.Button("Change text", on_click=change_text)
solara.Text(text.value)
import solara
import reacton.core


text = solara.reactive("Initial text")

@solara.component
def Page():
rc = reacton.core.get_render_context()
print("Render with: ", text.value)
def change_text():
with rc:
text.value = "Should never be rendered visible"
text.value = "Updated text"

solara.Button("Change text", on_click=change_text)
solara.Text(text.value)
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.
import solara


text = solara.reactive("Initial text")

@solara.component
def Page():
print("Render with: ", text.value)
def change_text():
with solara.batch_update:
text.value = "Should never be rendered visible"
text.value = "Updated text"

solara.Button("Change text", on_click=change_text)
solara.Text(text.value)
import solara


text = solara.reactive("Initial text")

@solara.component
def Page():
print("Render with: ", text.value)
def change_text():
with solara.batch_update:
text.value = "Should never be rendered visible"
text.value = "Updated text"

solara.Button("Change text", on_click=change_text)
solara.Text(text.value)
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

Did you find this page helpful?