Skip to content

On the expectations of refreshable + user storage #3233

Closed Answered by falkoschindler
ruisilvestre-snapp asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @ruisilvestre-snapp,

I think you ran into a pretty common problem with ui.refreshable. You want to keep the refreshable implementation separate from the page using it. But by moving the decorated function into global scope, it shares state with other instances using this function.

Here is a solution inspired by discussion #2535:

class ClickUI:

    @ui.refreshable
    def show(self):
        ui.label(f"Clicks: {app.storage.user.get('clicks', 0)}")

@ui.page("/")
def main():
    click_ui = ClickUI()

    def update_storage():
        app.storage.user["clicks"] = app.storage.user.get("clicks", 0) + 1
        click_ui.show.refresh()

    ui.button("Click me", on_click=update_storage)
    c…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@ruisilvestre-snapp
Comment options

@tmlmt
Comment options

@rodja
Comment options

@arunmp25
Comment options

@falkoschindler
Comment options

Answer selected by ruisilvestre-snapp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants
Converted from issue

This discussion was converted from issue #3219 on June 16, 2024 13:35.