posit-dev / posit-dev/py-shiny

saving and restoring state

Open
#323 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.8k
Forks
135
Avg merge
2d 18h
Merged PRs (30d)
21

Description

Are there plans to add tooling to save and restore state in py-shiny? I'm particularly interested in the option to save state to files (pickle format?) and to databases (e.g., sqlite). For apps that use large-ish datasets allowing the combination of storing to file + database would be ideal.

@jcheng5 got me started in an approach (re)store state in shiny-for-R with https://github.com/jcheng5/shiny-resume/blob/master/session.R. This required functionality like parseQueryString(session$clientData$url_search) which I don't see in py-shiny as yet.

The approach that I have used relies on functions that control how state is stored and restored for each input. See https://github.com/vnijs/quizr/blob/master/inst/example/init.R for an example. r_state is a list that is populated on session$onSessionEnded as shown below.

saveStateOnRefresh <- function(session = session) {
  session$onSessionEnded(function() {
    isolate({
      r_sessions[[r_ssuid]] <- list(
        r_state   = reactiveValuesToList(input),
        timestamp = Sys.time()
      )
      ## saving session information to file but could also be store to a database
      ## storing state could also be initialized using a button
      saveRDS(r_sessions[[r_ssuid]], file = paste0("~/r_sessions/r_", r_ssuid, ".rds"))
    })
  })
}

If a value is available in the r_state state list it will be used to initialize inputs on restore.

state_init <- function(inputvar, init = "") {
  if (is.null(r_state[[inputvar]])) init else r_state[[inputvar]]
}
radioButtons("radio1", NULL, answers, selected = state_init("radio1"))

I'd be very interested to hear your thoughts on (re)storing app state in py-shiny

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the shiny-resume session.R example and the quizr init.R example, then review the py-shiny session lifecycle around session$onSessionEnded and the missing parseQueryString equivalent. Define the persistence scope, supported file or database backends, and restore behavior before implementation; done should include a documented state-saving and state-restoring workflow.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlite
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.