Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add early exit when setting the value of ui.state variables #2922

Merged
merged 1 commit into from
Apr 20, 2024

Conversation

falkoschindler
Copy link
Contributor

This PR solves a problem described in #2921 where setting the value of a ui.state variable causes an infinite recursion.

Take this simple example code:

@ui.refreshable
def counter():
    count, set_count = ui.state(0)
    color, set_color = ui.state('black')
    ui.number(value=count, on_change=lambda e: set_count(e.value))
    ui.select(['black', 'red', 'green', 'blue'], value=color, on_change=lambda e: set_color(e.value))
    if count == 0:
        set_color('red')

counter()

Whenever the count is zero, the color is set to "red", which causes the refreshable to re-run - even if the color hasn't changed. The proposed change causes an early exit to avoid the infinite recursion.

@falkoschindler falkoschindler added the enhancement New feature or request label Apr 20, 2024
@falkoschindler falkoschindler added this to the 1.4.23 milestone Apr 20, 2024
@vickorian
Copy link

@falkoschindler

I pushed your PR to my version and it solves the recursion but that only brought out a set_value problem, it does not play well with arrays, making some more complex things not possible. I'm planning on tackling that when i finish this project along with the documentation for advanced aesthetics. More complex state based systems aren't possible without using custom binary functions to specify exact states via precalculated dictionaries for each state, which can get, to say the least, cumbersome.

@rodja rodja merged commit 0994ca6 into main Apr 20, 2024
7 checks passed
@rodja rodja deleted the set-state-value branch April 20, 2024 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants