-
Notifications
You must be signed in to change notification settings - Fork 99
Save and restore state, incl dataframes #1980
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
Comments
Related: #1958 (comment) |
There are two approaches on how to fix this:
|
"store state for a specific user in one file". Shiny does not know of the user and the demo app does not expose it either. For example purposes, let propose you have access to Let imagine then we're deployed on connect and (UNTESTED. Please let me know your final solution!) # (UNTESTED. Please let me know your final solution!)
app = App(app_ui, server, bookmark_store="server")
bookmark_dir = Path("bookmarks")
# Docs on the headers:
# https://docs.posit.co/connect/admin/appendix/advanced-user-group/#content-credentials
# Ex: session.http_conn.headers["shiny-server-credentials"]
async def user_restore_dir(_id_ignored: str) -> Path:
from shiny.session import require_active_session
from shiny import reactive
user = require_active_session(None).user
# Return your own bookmark dir location!
user_bookmark_dir = bookmark_dir / user
return user_bookmark_dir
async def user_save_dir(_id_ignored: str) -> Path:
user_bookmark_dir = await user_restore_dir(_id_ignored)
user_bookmark_dir.mkdir(parents=True, exist_ok=True)
return user_bookmark_dir
app.set_bookmark_save_dir_fn(user_save_dir)
app.set_bookmark_restore_dir_fn(user_restore_dir) If this approach is used, the ID being displayed is meaningless. Therefore, we can ignore the query string update within the app. # (UNTESTED)
# Do not auto-update the query string
chat.enable_bookmarking(chat_client, bookmark_on=None)
# Also update the bookmark on message change
@reactive.Effect
@reactive.event(
input.select_data,
input.select_letter,
input.add_data,
input.remove_data,
# Reactively react to chat messages
# This is a workaround to ensure the bookmark is updated when the chat messages change
# This approach may change in the future
lambda: chat.messages(format=MISSING),
)
async def _():
await session.bookmark()
# Add placeholder method (to do nothing) which hides the modal that shows up by default
@session.bookmark.on_bookmarked
async def _(_url_ignored: str):
pass |
UNTESTED. def app_ui(request: Request):
data_choices = ["A", "B"]
from shiny.bookmark._restore_state import get_current_restore_context
restore_ctx = get_current_restore_context()
if restore_ctx:
# Only need to restore the choices as the selected value is restored by the
# bookmark
data_choices = state.values["available_datasets"]
return ui.page_sidebar(
ui.sidebar(
ui.h4("Test"),
ui.input_select("select_letter", "Select letter", choices=["X", "Y", "Z"]),
ui.input_select("select_data", "Select data", choices=data_choices),
# ui.output_ui("ui_select_data"), # Use different ID for output
ui.input_action_button("add_data", "Add Data"),
ui.input_action_button("remove_data", "Remove Data"),
ui.output_ui("data"),
width=400,
),
ui.chat_ui(
id="chat",
messages=["Hello! How can I help you today?"],
),
width=400,
) I believe a |
Note... during |
@vnijs I hope this helps you get moving! Please let me know of any rough edges you come across. I'd be happy to fix them / update docs. |
Thank for the detailed replies @schloerke! Much appreciated and I will try each of your suggestions. We would have a user_name if using shiny-server pro and/or we could use a session identifier in the url. Joe Cheng suggested the below for me years ago and that has worked really well in R-Shiny Below the sequence of call I use in https://github.com/radiant-rstats/radiant.data/blob/b42fc2a29c1ddbf1e98584d0164d8c22b174bb8b/inst/app/server.R#L27 I will try to implement something similar for shiny for python very soon. On a related note, I'd love to hear your thoughts on the feature request linked below. Feasible? Of interest? I wouldn't know where to start in the shiny code base, tbh, but with some pointers I'd be happy to give this a shot as a PR if you would consider it. |
Commenting in the Issue. |
Hi @schloerke: @cpsievert suggested connecting with you about the new state save and restore features. Hopefully this path is appropriate.
I cobbled together a working solution to save and restore data, incl dataframes, but it seems a bit clunky (i.e., state is being updated many times to avoid losing information on browser refresh). See example below. Would love to hear your suggestions.
A few additional questions:
The text was updated successfully, but these errors were encountered: