From 095bb94212b2a53c30efbcf0e8d5071a49789712 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Wed, 19 Mar 2025 12:25:17 -0400 Subject: [PATCH] Allow for `session.bookmark.update_query_string(query_string=)` to be optional. Default to current bookmark url --- shiny/bookmark/_bookmark.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/shiny/bookmark/_bookmark.py b/shiny/bookmark/_bookmark.py index 2e8832733..f76ed9620 100644 --- a/shiny/bookmark/_bookmark.py +++ b/shiny/bookmark/_bookmark.py @@ -3,7 +3,7 @@ import warnings from abc import ABC, abstractmethod from pathlib import Path -from typing import TYPE_CHECKING, Awaitable, Callable, Literal +from typing import TYPE_CHECKING, Awaitable, Callable, Literal, Optional from .._docstring import add_example from .._utils import AsyncCallbacks, CancelCallback, wrap_async @@ -167,7 +167,7 @@ def on_restored( @abstractmethod async def update_query_string( self, - query_string: str, + query_string: Optional[str] = None, mode: Literal["replace", "push"] = "replace", ) -> None: """ @@ -176,7 +176,7 @@ async def update_query_string( Parameters ---------- query_string - The query string to set. + The query string to set. If `None`, the current bookmark state URL will be used. mode Whether to replace the current URL or push a new one. Pushing a new value will add to the user's browser history. @@ -448,9 +448,12 @@ async def invoke_on_restored_callbacks(): async def update_query_string( self, - query_string: str, + query_string: Optional[str] = None, mode: Literal["replace", "push"] = "replace", ) -> None: + if query_string is None: + query_string = await self.get_bookmark_url() + if mode not in {"replace", "push"}: raise ValueError(f"Invalid mode: {mode}") await self._root_session._send_message( @@ -723,7 +726,9 @@ def _restore_context(self) -> RestoreContext | None: return self._root_bookmark._restore_context async def update_query_string( - self, query_string: str, mode: Literal["replace", "push"] = "replace" + self, + query_string: Optional[str] = None, + mode: Literal["replace", "push"] = "replace", ) -> None: await self._root_bookmark.update_query_string(query_string, mode) @@ -769,7 +774,9 @@ def on_bookmarked( return lambda: None async def update_query_string( - self, query_string: str, mode: Literal["replace", "push"] = "replace" + self, + query_string: Optional[str] = None, + mode: Literal["replace", "push"] = "replace", ) -> None: # no-op within ExpressStub return None