-
Notifications
You must be signed in to change notification settings - Fork 63
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
Test input task button and extended task decorator #1099
Changes from 10 commits
5d4b87b
5dbb53b
1493f2a
53c19ea
df9266b
35ea991
211bb70
b21fc34
712812d
5ac19b4
80f3e00
1514d7f
a428c32
6588eef
59a5d68
47e5848
ab5f9b4
bc677da
68fcc61
6219c16
a385751
538641c
b55df77
389c64a
e286906
0bcea66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,11 +24,18 @@ async def slow_compute(a: int, b: int) -> int: | |||||
return a + b | ||||||
|
||||||
|
||||||
# @ui.bind_task_button(button_id="btn2") | ||||||
karangattu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
async def slow_input_compute(a: int, b: int) -> int: | ||||||
karangattu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
await asyncio.sleep(3) | ||||||
return a + b | ||||||
|
||||||
|
||||||
with ui.layout_sidebar(): | ||||||
with ui.sidebar(): | ||||||
ui.input_numeric("x", "x", 1) | ||||||
ui.input_numeric("y", "y", 2) | ||||||
ui.input_task_button("btn", "Compute, slowly") | ||||||
karangattu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
ui.input_task_button("btn2", "Compute 2 slowly") | ||||||
karangattu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
ui.input_action_button("btn_cancel", "Cancel") | ||||||
|
||||||
@reactive.Effect | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can not suggest a change on the next line to update There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can not suggest a change to rename |
||||||
|
@@ -37,6 +44,13 @@ def handle_click(): | |||||
# slow_compute.cancel() | ||||||
slow_compute(input.x(), input.y()) | ||||||
|
||||||
@reactive.Effect | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
@reactive.event(input.btn2, ignore_none=False) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
async def handle_click2(): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# slow_compute.cancel() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
val = await slow_input_compute(input.x(), input.y()) | ||||||
print(val) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
@reactive.Effect | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
@reactive.event(input.btn_cancel) | ||||||
def handle_cancel(): | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import asyncio | ||
from datetime import datetime | ||
|
||
from shiny import reactive, render | ||
from shiny.express import input, ui | ||
|
||
ui.h5("Current time") | ||
|
||
|
||
@render.text() | ||
def current_time() -> str: | ||
reactive.invalidate_later(0.1) | ||
return str(datetime.now().utcnow()) | ||
|
||
|
||
with ui.p(): | ||
"Notice that the time above updates every second, even if you click the button below." | ||
|
||
|
||
@ui.bind_task_button(button_id="btn") | ||
@reactive.extended_task | ||
async def slow_compute(a: int, b: int) -> int: | ||
await asyncio.sleep(3) | ||
return a + b | ||
|
||
|
||
# @ui.bind_task_button(button_id="btn2") | ||
async def slow_input_compute(a: int, b: int) -> int: | ||
await asyncio.sleep(3) | ||
return a + b | ||
|
||
|
||
with ui.layout_sidebar(): | ||
with ui.sidebar(): | ||
ui.input_numeric("x", "x", 1) | ||
ui.input_numeric("y", "y", 2) | ||
ui.input_task_button("btn", "Compute, slowly") | ||
ui.input_task_button("btn2", "Compute 2 slowly", label_busy="Blocking...") | ||
ui.input_action_button("btn_cancel", "Cancel") | ||
|
||
@reactive.Effect | ||
@reactive.event(input.btn, ignore_none=False) | ||
def handle_click(): | ||
# slow_compute.cancel() | ||
slow_compute(input.x(), input.y()) | ||
|
||
@reactive.Effect | ||
@reactive.event(input.btn2, ignore_none=False) | ||
async def handle_click2(): | ||
# slow_compute.cancel() | ||
await slow_input_compute(input.x(), input.y()) | ||
|
||
@reactive.Effect | ||
@reactive.event(input.btn_cancel) | ||
def handle_cancel(): | ||
slow_compute.cancel() | ||
|
||
ui.h5("Sum of x and y") | ||
|
||
@render.text | ||
def show_result(): | ||
return str(slow_compute.result()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from __future__ import annotations | ||
|
||
from conftest import ShinyAppProc | ||
from controls import InputNumeric, InputTaskButton, OutputText | ||
from playwright.sync_api import Page | ||
|
||
|
||
def click_extended_task_button( | ||
button: InputTaskButton, | ||
current_time: OutputText, | ||
button_label: list[str], | ||
) -> str: | ||
button.expect_state("ready") | ||
button.expect_label_text(button_label) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this out of the function and have it live in the test code directly. (It is unrelated to clicking the button) |
||
button.click(timeout=0) | ||
button.expect_state("busy", timeout=0) | ||
return current_time.get_value(timeout=0) | ||
|
||
|
||
def test_input_action_task_button(page: Page, local_app: ShinyAppProc) -> None: | ||
page.goto(local_app.url) | ||
y = InputNumeric(page, "y") | ||
y.set("4") | ||
result = OutputText(page, "show_result") | ||
current_time = OutputText(page, "current_time") | ||
current_time.expect.not_to_be_empty() | ||
karangattu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
result.expect_value("3") | ||
karangattu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# extended task | ||
button1 = InputTaskButton(page, "btn") | ||
time1 = click_extended_task_button( | ||
button1, current_time, button_label=["Compute, slowly", "\n \n Processing..."] | ||
) | ||
current_time.expect.not_to_have_text(time1, timeout=500) | ||
karangattu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
result.expect_value("3", timeout=0) | ||
result.expect_value("5", timeout=(3 + 1) * 1000) | ||
y.set("15") | ||
result.expect_value("5") | ||
karangattu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# extended task with blocking | ||
button2 = InputTaskButton(page, "btn2") | ||
time2 = click_extended_task_button( | ||
button2, current_time, button_label=["Compute 2 slowly", "\n \n Blocking..."] | ||
) | ||
current_time.expect_value(time2, timeout=0) | ||
karangattu marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move these testing changes to a local test app, rather than updating the api-examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for suggesting the updates here and not in the local test app. (This file should not be changed in the PR.)