|
3 | 3 | import copy |
4 | 4 | import os |
5 | 5 | import secrets |
| 6 | +import warnings |
6 | 7 | from contextlib import AsyncExitStack, asynccontextmanager |
7 | 8 | from inspect import signature |
8 | 9 | from pathlib import Path |
|
30 | 31 | from ._error import ErrorMiddleware |
31 | 32 | from ._shinyenv import is_pyodide |
32 | 33 | 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 | +) |
33 | 39 | from .html_dependencies import jquery_deps, require_deps, shiny_deps |
34 | 40 | from .http_staticfiles import FileResponse, StaticFiles |
35 | 41 | from .session._session import AppSession, Inputs, Outputs, Session, session_context |
@@ -167,7 +173,7 @@ def __init__( |
167 | 173 |
|
168 | 174 | self._sessions: dict[str, AppSession] = {} |
169 | 175 |
|
170 | | - self._sessions_needing_flush: dict[int, AppSession] = {} |
| 176 | + # self._sessions_needing_flush: dict[int, AppSession] = {} |
171 | 177 |
|
172 | 178 | self._registered_dependencies: dict[str, HTMLDependency] = {} |
173 | 179 | self._dependency_handler = starlette.routing.Router() |
@@ -353,11 +359,41 @@ async def _on_root_request_cb(self, request: Request) -> Response: |
353 | 359 | request for / occurs. |
354 | 360 | """ |
355 | 361 | ui: RenderedHTML |
356 | | - if callable(self.ui): |
357 | | - ui = self._render_page(self.ui(request), self.lib_prefix) |
| 362 | + # Create a restore context using query string |
| 363 | + # TODO: Barret implement how to get bookmark_store value |
| 364 | + # bookmarkStore <- getShinyOption("bookmarkStore", default = "disable") |
| 365 | + print("TODO: Figure this out") |
| 366 | + bookmark_store: str = "disable" |
| 367 | + bookmark_store: str = "query" |
| 368 | + |
| 369 | + if bookmark_store == "disable": |
| 370 | + restore_ctx = RestoreContext() |
358 | 371 | else: |
359 | | - ui = self.ui |
360 | | - return HTMLResponse(content=ui["html"]) |
| 372 | + restore_ctx = await RestoreContext.from_query_string(request.url.query) |
| 373 | + |
| 374 | + print( |
| 375 | + { |
| 376 | + "values": restore_ctx.as_state().values, |
| 377 | + "input": restore_ctx.as_state().input, |
| 378 | + } |
| 379 | + ) |
| 380 | + |
| 381 | + with restore_context(restore_ctx): |
| 382 | + if callable(self.ui): |
| 383 | + ui = self._render_page(self.ui(request), self.lib_prefix) |
| 384 | + else: |
| 385 | + # TODO: Why is this here as there's a with restore_context above? |
| 386 | + # TODO: Why not `if restore_ctx.active:`? |
| 387 | + cur_restore_ctx = get_current_restore_context() |
| 388 | + print("cur_restore_ctx", cur_restore_ctx) |
| 389 | + if cur_restore_ctx is not None and cur_restore_ctx.active: |
| 390 | + # TODO: See ?enableBookmarking |
| 391 | + warnings.warn( |
| 392 | + "Trying to restore saved app state, but UI code must be a function for this to work!" |
| 393 | + ) |
| 394 | + |
| 395 | + ui = self.ui |
| 396 | + return HTMLResponse(content=ui["html"]) |
361 | 397 |
|
362 | 398 | async def _on_connect_cb(self, ws: starlette.websockets.WebSocket) -> None: |
363 | 399 | """ |
|
0 commit comments