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 demos for using ui.refreshable with local scope #3279

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions website/documentation/content/refreshable_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,91 @@ def counter(name: str):
counter('B')


@doc.demo('Global scope', '''
When defining a refreshable function in the global scope,
every refreshable UI that is created by calling this function will share the same state.
In this demo, `time()` will show the current date and time.
When opening the page in a new tab, _both_ tabs will be updated simultaneously when clicking the "Refresh" button.

See the "local scope" demos below for a way to create independent refreshable UIs instead.
''')
def global_scope():
from datetime import datetime

@ui.refreshable
def time():
ui.label(f'Time: {datetime.now()}')

@ui.page('/global_refreshable')
def demo():
time()
ui.button('Refresh', on_click=time.refresh)

ui.link('Open demo', demo)


@doc.demo('Local scope (1/3)', '''
When defining a refreshable function in a local scope,
refreshable UI that is created by calling this function will refresh independently.
In contrast to the "global scope" demo,
the time will be updated only in the tab where the "Refresh" button was clicked.
''')
def local_scope_1():
from datetime import datetime

@ui.page('/local_refreshable_1')
def demo():
@ui.refreshable
def time():
ui.label(f'Time: {datetime.now()}')

time()
ui.button('Refresh', on_click=time.refresh)

ui.link('Open demo', demo)


@doc.demo('Local scope (2/3)', '''
In order to define refreshable UIs with local state outside of page functions,
you can, e.g., define a class with a refreshable method.
This way, you can create multiple instances of the class with independent state,
because the `ui.refreshable` decorator acts on the class instance rather than the class itself.
''')
def local_scope_2():
from datetime import datetime

class Clock:
@ui.refreshable
def time(self):
ui.label(f'Time: {datetime.now()}')

@ui.page('/local_refreshable_2')
def demo():
clock = Clock()
clock.time()
ui.button('Refresh', on_click=clock.time.refresh)

ui.link('Open demo', demo)


@doc.demo('Local scope (3/3)', '''
As an alternative to the class definition shown above, you can also define the UI function in global scope,
but apply the `ui.refreshable` decorator inside the page function.
This way the refreshable UI will refresh independently.
''')
def local_scope_3():
from datetime import datetime

def time():
ui.label(f'Time: {datetime.now()}')

@ui.page('/local_refreshable_3')
def demo():
refreshable_time = ui.refreshable(time)
refreshable_time()
ui.button('Refresh', on_click=refreshable_time.refresh)

ui.link('Open demo', demo)


doc.reference(ui.refreshable)
Loading