Skip to content

Commit 8be292d

Browse files
committed
Add docs; Rename ShinySaveState -> BookmarkState
1 parent 79dd52b commit 8be292d

File tree

9 files changed

+93
-107
lines changed

9 files changed

+93
-107
lines changed

docs/_quartodoc-core.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ quartodoc:
9898
- ui.input_file
9999
- ui.download_button
100100
- ui.download_link
101+
- title: Bookmarking
102+
desc: Saving and restoring app state
103+
contents:
104+
- ui.input_bookmark_button
105+
- bookmark.restore_input
106+
- bookmark.Bookmark
107+
- session.Session.bookmark.exclude
108+
- bookmark.BookmarkState
109+
- bookmark.RestoreState
110+
- kind: page
111+
path: bookmark_integration
112+
summary:
113+
name: "Integration"
114+
desc: "Decorators to set save and restore directories."
115+
flatten: true
116+
contents:
117+
- bookmark.set_save_dir
118+
- bookmark.set_restore_dir
101119
- title: Chat interface
102120
desc: Build a chatbot interface
103121
contents:

shiny/_app.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@
3131
from ._error import ErrorMiddleware
3232
from ._shinyenv import is_pyodide
3333
from ._utils import guess_mime_type, is_async_callable, sort_keys_length
34-
from .bookmark._restore_state import (
35-
RestoreContext,
36-
get_current_restore_context,
37-
restore_context,
38-
)
34+
from .bookmark._restore_state import RestoreContext, restore_context
3935
from .bookmark._types import BookmarkStore
4036
from .html_dependencies import jquery_deps, require_deps, shiny_deps
4137
from .http_staticfiles import FileResponse, StaticFiles

shiny/bookmark/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
BookmarkApp,
44
BookmarkExpressStub,
55
BookmarkProxy,
6-
ShinySaveState,
76
)
87
from ._button import input_bookmark_button
98
from ._external import set_restore_dir, set_save_dir
10-
from ._restore_state import RestoreContext, RestoreContextState, restore_input
9+
from ._restore_state import RestoreContext, RestoreState, restore_input
10+
from ._save_state import BookmarkState
1111

1212
__all__ = (
1313
# _bookmark
14-
"ShinySaveState",
1514
"Bookmark",
1615
"BookmarkApp",
1716
"BookmarkProxy",
@@ -23,6 +22,8 @@
2322
"set_restore_dir",
2423
# _restore_state
2524
"RestoreContext",
26-
"RestoreContextState",
25+
"RestoreState",
2726
"restore_input",
27+
# _save_state
28+
"BookmarkState",
2829
)

shiny/bookmark/_bookmark.py

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from .._utils import AsyncCallbacks, CancelCallback, wrap_async
99
from ..types import MISSING, MISSING_TYPE
1010
from ._button import BOOKMARK_ID
11-
from ._restore_state import RestoreContextState
12-
from ._save_state import ShinySaveState
11+
from ._restore_state import RestoreState
12+
from ._save_state import BookmarkState
1313
from ._types import BookmarkStore
1414

1515
# TODO: Barret - Bookmark state
@@ -192,8 +192,7 @@ def _get_bookmark_exclude(self) -> list[str]:
192192
def on_bookmark(
193193
self,
194194
callback: (
195-
Callable[[ShinySaveState], None]
196-
| Callable[[ShinySaveState], Awaitable[None]]
195+
Callable[[BookmarkState], None] | Callable[[BookmarkState], Awaitable[None]]
197196
),
198197
/,
199198
) -> CancelCallback:
@@ -266,8 +265,7 @@ async def do_bookmark(self) -> None:
266265
def on_restore(
267266
self,
268267
callback: (
269-
Callable[[RestoreContextState], None]
270-
| Callable[[RestoreContextState], Awaitable[None]]
268+
Callable[[RestoreState], None] | Callable[[RestoreState], Awaitable[None]]
271269
),
272270
) -> CancelCallback:
273271
"""
@@ -281,8 +279,7 @@ def on_restore(
281279
def on_restored(
282280
self,
283281
callback: (
284-
Callable[[RestoreContextState], None]
285-
| Callable[[RestoreContextState], Awaitable[None]]
282+
Callable[[RestoreState], None] | Callable[[RestoreState], Awaitable[None]]
286283
),
287284
) -> CancelCallback:
288285
"""
@@ -402,8 +399,7 @@ def _get_bookmark_exclude(self) -> list[str]:
402399
def on_bookmark(
403400
self,
404401
callback: (
405-
Callable[[ShinySaveState], None]
406-
| Callable[[ShinySaveState], Awaitable[None]]
402+
Callable[[BookmarkState], None] | Callable[[BookmarkState], Awaitable[None]]
407403
),
408404
/,
409405
) -> CancelCallback:
@@ -419,17 +415,15 @@ def on_bookmarked(
419415
def on_restore(
420416
self,
421417
callback: (
422-
Callable[[RestoreContextState], None]
423-
| Callable[[RestoreContextState], Awaitable[None]]
418+
Callable[[RestoreState], None] | Callable[[RestoreState], Awaitable[None]]
424419
),
425420
) -> CancelCallback:
426421
return self._on_restore_callbacks.register(wrap_async(callback))
427422

428423
def on_restored(
429424
self,
430425
callback: (
431-
Callable[[RestoreContextState], None]
432-
| Callable[[RestoreContextState], Awaitable[None]]
426+
Callable[[RestoreState], None] | Callable[[RestoreState], Awaitable[None]]
433427
),
434428
) -> CancelCallback:
435429
return self._on_restored_callbacks.register(wrap_async(callback))
@@ -457,14 +451,14 @@ async def do_bookmark(self) -> None:
457451

458452
try:
459453
# ?withLogErrors
460-
from ..bookmark._bookmark import ShinySaveState
454+
from ..bookmark._bookmark import BookmarkState
461455
from ..session import session_context
462456

463-
async def root_state_on_save(state: ShinySaveState) -> None:
457+
async def root_state_on_save(state: BookmarkState) -> None:
464458
with session_context(self._session_root):
465459
await self._on_bookmark_callbacks.invoke(state)
466460

467-
root_state = ShinySaveState(
461+
root_state = BookmarkState(
468462
input=self._session_root.input,
469463
exclude=self._get_bookmark_exclude(),
470464
on_save=root_state_on_save,
@@ -541,7 +535,7 @@ def __init__(self, session_proxy: SessionProxy):
541535
# The goal of this method is to save the scope's values. All namespaced inputs
542536
# will already exist within the `root_state`.
543537
@self._root_bookmark.on_bookmark
544-
async def scoped_on_bookmark(root_state: ShinySaveState) -> None:
538+
async def scoped_on_bookmark(root_state: BookmarkState) -> None:
545539
return await self._scoped_on_bookmark(root_state)
546540

547541
from ..session import session_context
@@ -557,7 +551,7 @@ async def scoped_on_bookmarked(url: str) -> None:
557551
ns_prefix = str(self._ns + self._ns._sep)
558552

559553
@self._root_bookmark.on_restore
560-
async def scoped_on_restore(restore_state: RestoreContextState) -> None:
554+
async def scoped_on_restore(restore_state: RestoreState) -> None:
561555
if self._on_restore_callbacks.count() == 0:
562556
return
563557

@@ -567,22 +561,22 @@ async def scoped_on_restore(restore_state: RestoreContextState) -> None:
567561
await self._on_restore_callbacks.invoke(scoped_restore_state)
568562

569563
@self._root_bookmark.on_restored
570-
async def scoped_on_restored(restore_state: RestoreContextState) -> None:
564+
async def scoped_on_restored(restore_state: RestoreState) -> None:
571565
if self._on_restored_callbacks.count() == 0:
572566
return
573567

574568
scoped_restore_state = restore_state._state_within_namespace(ns_prefix)
575569
with session_context(self._session_proxy):
576570
await self._on_restored_callbacks.invoke(scoped_restore_state)
577571

578-
async def _scoped_on_bookmark(self, root_state: ShinySaveState) -> None:
572+
async def _scoped_on_bookmark(self, root_state: BookmarkState) -> None:
579573
# Exit if no user-defined callbacks.
580574
if self._on_bookmark_callbacks.count() == 0:
581575
return
582576

583-
from ..bookmark._bookmark import ShinySaveState
577+
from ..bookmark._bookmark import BookmarkState
584578

585-
scoped_state = ShinySaveState(
579+
scoped_state = BookmarkState(
586580
input=self._session_root.input,
587581
exclude=self._root_bookmark.exclude,
588582
on_save=None,
@@ -621,8 +615,7 @@ def _create_effects(self) -> NoReturn:
621615
def on_bookmark(
622616
self,
623617
callback: (
624-
Callable[[ShinySaveState], None]
625-
| Callable[[ShinySaveState], Awaitable[None]]
618+
Callable[[BookmarkState], None] | Callable[[BookmarkState], Awaitable[None]]
626619
),
627620
/,
628621
) -> CancelCallback:
@@ -658,17 +651,15 @@ def store(self) -> BookmarkStore:
658651
def on_restore(
659652
self,
660653
callback: (
661-
Callable[[RestoreContextState], None]
662-
| Callable[[RestoreContextState], Awaitable[None]]
654+
Callable[[RestoreState], None] | Callable[[RestoreState], Awaitable[None]]
663655
),
664656
) -> CancelCallback:
665657
return self._on_restore_callbacks.register(wrap_async(callback))
666658

667659
def on_restored(
668660
self,
669661
callback: (
670-
Callable[[RestoreContextState], None]
671-
| Callable[[RestoreContextState], Awaitable[None]]
662+
Callable[[RestoreState], None] | Callable[[RestoreState], Awaitable[None]]
672663
),
673664
) -> CancelCallback:
674665
return self._on_restored_callbacks.register(wrap_async(callback))
@@ -697,8 +688,7 @@ def _get_bookmark_exclude(self) -> NoReturn:
697688
def on_bookmark(
698689
self,
699690
callback: (
700-
Callable[[ShinySaveState], None]
701-
| Callable[[ShinySaveState], Awaitable[None]]
691+
Callable[[BookmarkState], None] | Callable[[BookmarkState], Awaitable[None]]
702692
),
703693
) -> NoReturn:
704694
raise NotImplementedError(
@@ -728,8 +718,7 @@ async def do_bookmark(self) -> NoReturn:
728718
def on_restore(
729719
self,
730720
callback: (
731-
Callable[[RestoreContextState], None]
732-
| Callable[[RestoreContextState], Awaitable[None]]
721+
Callable[[RestoreState], None] | Callable[[RestoreState], Awaitable[None]]
733722
),
734723
) -> NoReturn:
735724
raise NotImplementedError(
@@ -739,8 +728,7 @@ def on_restore(
739728
def on_restored(
740729
self,
741730
callback: (
742-
Callable[[RestoreContextState], None]
743-
| Callable[[RestoreContextState], Awaitable[None]]
731+
Callable[[RestoreState], None] | Callable[[RestoreState], Awaitable[None]]
744732
),
745733
) -> NoReturn:
746734
raise NotImplementedError(

shiny/bookmark/_button.py

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,50 @@
99
BOOKMARK_ID = "._bookmark_"
1010

1111

12-
# ' Create a button for bookmarking/sharing
13-
# '
14-
# ' A `bookmarkButton` is a [actionButton()] with a default label
15-
# ' that consists of a link icon and the text "Bookmark...". It is meant to be
16-
# ' used for bookmarking state.
17-
# '
18-
# ' @inheritParams actionButton
19-
# ' @param title A tooltip that is shown when the mouse cursor hovers over the
20-
# ' button.
21-
# ' @param id An ID for the bookmark button. The only time it is necessary to set
22-
# ' the ID unless you have more than one bookmark button in your application.
23-
# ' If you specify an input ID, it should be excluded from bookmarking with
24-
# ' [setBookmarkExclude()], and you must create an observer that
25-
# ' does the bookmarking when the button is pressed. See the examples below.
26-
# '
27-
# ' @seealso [enableBookmarking()] for more examples.
28-
# '
29-
# ' @examples
30-
# ' ## Only run these examples in interactive sessions
31-
# ' if (interactive()) {
32-
# '
33-
# ' # This example shows how to use multiple bookmark buttons. If you only need
34-
# ' # a single bookmark button, see examples in ?enableBookmarking.
35-
# ' ui <- function(request) {
36-
# ' fluidPage(
37-
# ' tabsetPanel(id = "tabs",
38-
# ' tabPanel("One",
39-
# ' checkboxInput("chk1", "Checkbox 1"),
40-
# ' bookmarkButton(id = "bookmark1")
41-
# ' ),
42-
# ' tabPanel("Two",
43-
# ' checkboxInput("chk2", "Checkbox 2"),
44-
# ' bookmarkButton(id = "bookmark2")
45-
# ' )
46-
# ' )
47-
# ' )
48-
# ' }
49-
# ' server <- function(input, output, session) {
50-
# ' # Need to exclude the buttons from themselves being bookmarked
51-
# ' setBookmarkExclude(c("bookmark1", "bookmark2"))
52-
# '
53-
# ' # Trigger bookmarking with either button
54-
# ' observeEvent(input$bookmark1, {
55-
# ' session$doBookmark()
56-
# ' })
57-
# ' observeEvent(input$bookmark2, {
58-
# ' session$doBookmark()
59-
# ' })
60-
# ' }
61-
# ' enableBookmarking(store = "url")
62-
# ' shinyApp(ui, server)
63-
# ' }
64-
# ' @export
6512
def input_bookmark_button(
6613
label: TagChild = "Bookmark...",
6714
*,
6815
icon: TagChild | MISSING_TYPE = MISSING,
6916
width: Optional[str] = None,
7017
disabled: bool = False,
71-
# id: str = "._bookmark_",
18+
id: str = BOOKMARK_ID,
7219
title: str = "Bookmark this application's state and get a URL for sharing.",
7320
**kwargs: TagAttrValue,
7421
) -> Tag:
75-
resolved_id = resolve_id(BOOKMARK_ID)
22+
"""
23+
Button for bookmarking/sharing.
24+
25+
A `bookmarkButton` is a [input_action_button()] with a default label that consists of a link icon and the text "Bookmark...". It is meant to be used for bookmarking state.
26+
27+
Parameters
28+
----------
29+
label
30+
The button label.
31+
icon
32+
The icon to display on the button.
33+
width
34+
The CSS width, e.g. '400px', or '100%'.
35+
disabled
36+
Whether the button is disabled.
37+
id
38+
An ID for the bookmark button. The only time it is necessary to set the ID unless you have more than one bookmark button in your application. If you specify an input ID, it should be excluded from bookmarking with `session.bookmark.exclude.append(ID)`, and you must create a reactive effect that performs the bookmarking (`session.bookmark()`) when the button is pressed.
39+
title
40+
A tooltip that is shown when the mouse cursor hovers over the button.
41+
kwargs
42+
Additional attributes for the button.
43+
44+
Returns
45+
-------
46+
:
47+
A UI element.
48+
49+
See Also
50+
--------
51+
* :func:`~shiny.ui.input_action_button`
52+
* :func:`~shiny.ui.input_action_link`
53+
* :func:`~shiny.reactive.event`
54+
"""
55+
resolved_id = resolve_id(id)
7656
if isinstance(icon, MISSING_TYPE):
7757
icon = HTML("&#x1F517;")
7858

0 commit comments

Comments
 (0)