|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -__all__ = ("input_handlers",) |
4 | | - |
5 | 3 | from datetime import date, datetime, timezone |
6 | 4 | from typing import TYPE_CHECKING, Any, Callable, Dict |
7 | 5 |
|
| 6 | +from .bookmark import serializer_unserializable |
| 7 | + |
8 | 8 | if TYPE_CHECKING: |
9 | 9 | from .session import Session |
10 | 10 |
|
11 | 11 | from .module import ResolvedId |
12 | 12 | from .types import ActionButtonValue |
13 | 13 |
|
| 14 | +__all__ = ("input_handlers",) |
| 15 | + |
14 | 16 | InputHandlerType = Callable[[Any, ResolvedId, "Session"], Any] |
15 | 17 |
|
16 | 18 |
|
@@ -91,6 +93,10 @@ def _(value, name, session): |
91 | 93 | return "mypackage.intify"; |
92 | 94 | } |
93 | 95 | ``` |
| 96 | +
|
| 97 | +See Also |
| 98 | +-------- |
| 99 | +* :class:`~shiny.session.Inputs`'s `.set_serializer(info: InputSerializerInfo)` method for determining how an object can be serialized for bookmarking. |
94 | 100 | """ |
95 | 101 |
|
96 | 102 |
|
@@ -150,15 +156,50 @@ def _(value: str, name: ResolvedId, session: Session) -> str: |
150 | 156 | return value |
151 | 157 |
|
152 | 158 |
|
153 | | -# TODO: implement when we have bookmarking |
154 | | -# TODO: Barret: Input handler for passwords |
155 | 159 | @input_handlers.add("shiny.password") |
156 | 160 | def _(value: str, name: ResolvedId, session: Session) -> str: |
| 161 | + # Never bookmark passwords |
| 162 | + session.input.set_serializer(name, serializer_unserializable) |
| 163 | + |
157 | 164 | return value |
158 | 165 |
|
159 | 166 |
|
160 | | -# TODO: implement when we have bookmarking |
161 | | -# TODO: Barret: Input handler for file inputs |
162 | 167 | @input_handlers.add("shiny.file") |
163 | 168 | def _(value: Any, name: ResolvedId, session: Session) -> Any: |
| 169 | + |
| 170 | + # This function is only used when restoring a Shiny ui.input_file. |
| 171 | + # When a file is uploaded the usual way, it takes a different code path and won't |
| 172 | + # hit this function. |
| 173 | + if value is None: |
| 174 | + return None |
| 175 | + |
| 176 | + # TODO: Barret: Input handler for file inputs |
| 177 | + |
| 178 | + # # The data will be a named list of lists; convert to a data frame. |
| 179 | + # val <- as.data.frame(lapply(val, unlist), stringsAsFactors = FALSE) |
| 180 | + |
| 181 | + # # `val$datapath` should be a filename without a path, for security reasons. |
| 182 | + # if (basename(val$datapath) != val$datapath) { |
| 183 | + # stop("Invalid '/' found in file input path.") |
| 184 | + # } |
| 185 | + |
| 186 | + # # Prepend the persistent dir |
| 187 | + # oldfile <- file.path(getCurrentRestoreContext()$dir, val$datapath) |
| 188 | + |
| 189 | + # # Copy the original file to a new temp dir, so that a restored session can't |
| 190 | + # # modify the original. |
| 191 | + # newdir <- file.path(tempdir(), createUniqueId(12)) |
| 192 | + # dir.create(newdir) |
| 193 | + # val$datapath <- file.path(newdir, val$datapath) |
| 194 | + # file.copy(oldfile, val$datapath) |
| 195 | + |
| 196 | + # # Need to mark this input value with the correct serializer. When a file is |
| 197 | + # # uploaded the usual way (instead of being restored), this occurs in |
| 198 | + # # session$`@uploadEnd`. |
| 199 | + # setSerializer(name, serializerFileInput) |
| 200 | + |
| 201 | + # snapshotPreprocessInput(name, snapshotPreprocessorFileInput) |
| 202 | + |
| 203 | + # val |
| 204 | + |
164 | 205 | return value |
0 commit comments