-
Notifications
You must be signed in to change notification settings - Fork 100
feat(clientdata): id
is now optional on clientdata.output_*()
methods
#1978
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
Open
cpsievert
wants to merge
14
commits into
main
Choose a base branch
from
feat/current_output_id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
15be8ad
feat(session): Add a current_output_id attribute for identifying curr…
cpsievert abaebbb
feat(session.clientdata): allow output_*() methods to be called witho…
cpsievert e97b0f0
Update changelog
cpsievert cb9fdde
Appease import lint
cpsievert 7f3e958
Make sure context manager always yields
cpsievert 0908a4e
Make it a method instead of attribute. More careful cleanup
cpsievert 0088ff4
Tweak wording
cpsievert ce79a5f
Bugfix and add test
cpsievert 11e8a70
Import lint
cpsievert 1a9ed7f
Address feedback
cpsievert 95cdc59
Merge branch 'main' into feat/current_output_id
cpsievert 6b07a8e
Update tests
cpsievert 5866600
Run isort
cpsievert 4de9a7f
Fix
cpsievert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from __future__ import annotations | ||
|
||
from contextlib import contextmanager | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
|
@@ -162,7 +163,7 @@ def __call__(self, _fn: ValueFn[IT]) -> Self: | |
raise TypeError("Value function must be callable") | ||
|
||
# Set value function with extra meta information | ||
self.fn = AsyncValueFn(_fn) | ||
self.fn = AsyncValueFn(_fn, self) | ||
|
||
# Copy over function name as it is consistent with how Session and Output | ||
# retrieve function names | ||
|
@@ -350,6 +351,7 @@ class AsyncValueFn(Generic[IT]): | |
def __init__( | ||
self, | ||
fn: Callable[[], IT | None] | Callable[[], Awaitable[IT | None]], | ||
renderer: Renderer[Any], | ||
): | ||
if isinstance(fn, AsyncValueFn): | ||
raise TypeError( | ||
|
@@ -358,12 +360,14 @@ def __init__( | |
self._is_async = is_async_callable(fn) | ||
self._fn = wrap_async(fn) | ||
self._orig_fn = fn | ||
self._renderer = renderer | ||
|
||
async def __call__(self) -> IT | None: | ||
""" | ||
Call the asynchronous function. | ||
""" | ||
return await self._fn() | ||
with self._current_output_id(): | ||
return await self._fn() | ||
|
||
def is_async(self) -> bool: | ||
""" | ||
|
@@ -404,3 +408,19 @@ def get_sync_fn(self) -> Callable[[], IT | None]: | |
) | ||
sync_fn = cast(Callable[[], IT], self._orig_fn) | ||
return sync_fn | ||
|
||
@contextmanager | ||
def _current_output_id(self): | ||
from ...session import get_current_session | ||
|
||
session = get_current_session() | ||
if session is None: | ||
yield | ||
return | ||
|
||
old_id = session._current_output_id | ||
try: | ||
session._current_output_id = self._renderer.output_id | ||
yield | ||
finally: | ||
session._current_output_id = old_id | ||
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 like this approach!, but I feel it is in the wrong place. 👀 for a different location. 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. IIRC, we ended up not finding a better place? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from shiny import App, Inputs, Outputs, Session, render, ui | ||
|
||
app_ui = ui.page_fluid( | ||
ui.input_dark_mode(mode="light", id="dark_mode"), | ||
ui.output_text("text1"), | ||
ui.output_text("text2"), | ||
ui.output_text("info").add_class("shiny-report-theme"), | ||
) | ||
|
||
|
||
def server(input: Inputs, output: Outputs, session: Session): | ||
|
||
@render.text | ||
def text1(): | ||
id = session.current_output_id() or "None" | ||
return f"Output ID: {id}" | ||
|
||
@output(id="text2") | ||
@render.text | ||
def _(): | ||
id = session.current_output_id() or "None" | ||
return f"Output ID: {id}" | ||
|
||
@render.text | ||
def info(): | ||
bg_color = session.clientdata.output_bg_color() | ||
return f"BG color: {bg_color}" | ||
|
||
|
||
app = App(app_ui, server) |
29 changes: 29 additions & 0 deletions
29
tests/playwright/shiny/session/current_output_info/test_current_output_info.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from playwright.sync_api import Page | ||
|
||
from shiny.playwright import controller | ||
from shiny.run import ShinyAppProc | ||
|
||
|
||
def test_current_output_info(page: Page, local_app: ShinyAppProc) -> None: | ||
|
||
page.goto(local_app.url) | ||
|
||
# Check that the output ID is displayed correctly in the UI | ||
text1 = controller.OutputText(page, "text1") | ||
text2 = controller.OutputText(page, "text2") | ||
|
||
text1.expect_value("Output ID: text1") | ||
text2.expect_value("Output ID: text2") | ||
|
||
# Check that we can get background color from clientdata | ||
info = controller.OutputText(page, "info") | ||
info.expect_value("BG color: rgb(255, 255, 255)") | ||
|
||
# Click the dark mode button to change the background color | ||
dark_mode = controller.InputDarkMode(page, "dark_mode") | ||
dark_mode.expect_mode("light") | ||
dark_mode.click() | ||
dark_mode.expect_mode("dark") | ||
|
||
# Check that the background color has changed | ||
info.expect_value("BG color: rgb(29, 31, 33)") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.