Skip to content

Commit 8fbd8f2

Browse files
committed
Update warning message for when a user doesn't supply a function and wants bookmarking
1 parent df9f9bd commit 8fbd8f2

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

shiny/_app.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def __init__(
149149
"`server` must have 1 (Inputs) or 3 parameters (Inputs, Outputs, Session)"
150150
)
151151

152-
self._init_bookmarking(bookmark_store=bookmark_store)
152+
self._init_bookmarking(bookmark_store=bookmark_store, ui=ui)
153153

154154
self._debug: bool = debug
155155

@@ -378,19 +378,14 @@ async def _on_root_request_cb(self, request: Request) -> Response:
378378
request.url.query, app=self
379379
)
380380

381-
with restore_context(restore_ctx):
382-
if callable(self.ui):
381+
if callable(self.ui):
382+
# At this point, if `app.bookmark_store != "disable"`, then we've already
383+
# checked that `ui` is a function (in `App._init_bookmarking()`). No need to throw warning if `ui` is _not_ a function.
384+
with restore_context(restore_ctx):
383385
ui = self._render_page(self.ui(request), self.lib_prefix)
384-
else:
385-
if restore_ctx.active:
386-
# TODO: Barret - Docs: See ?enableBookmarking
387-
warnings.warn(
388-
"Trying to restore saved app state, but UI code must be a function for this to work!",
389-
stacklevel=1,
390-
)
391-
392-
ui = self.ui
393-
return HTMLResponse(content=ui["html"])
386+
else:
387+
ui = self.ui
388+
return HTMLResponse(content=ui["html"])
394389

395390
async def _on_connect_cb(self, ws: starlette.websockets.WebSocket) -> None:
396391
"""
@@ -503,11 +498,16 @@ def _render_page_from_file(self, file: Path, lib_prefix: str) -> RenderedHTML:
503498
# Bookmarking
504499
# ==========================================================================
505500

506-
def _init_bookmarking(self, *, bookmark_store: BookmarkStore) -> None:
501+
def _init_bookmarking(self, *, bookmark_store: BookmarkStore, ui: Any) -> None:
507502
self._bookmark_save_dir_fn = bookmark_global_state.bookmark_save_dir
508503
self._bookmark_restore_dir_fn = bookmark_global_state.bookmark_restore_dir
509504
self._bookmark_store = bookmark_store
510505

506+
if bookmark_store != "disable" and not callable(ui):
507+
raise TypeError(
508+
"App(ui=) must be a function that accepts a request object to allow the UI to be properly reconstructed from a bookmarked state."
509+
)
510+
511511
@property
512512
def bookmark_store(self) -> BookmarkStore:
513513
return self._bookmark_store

0 commit comments

Comments
 (0)