4343from .._namespaces import Id , Root
4444from .._typing_extensions import NotRequired , TypedDict
4545from .._utils import wrap_async
46- from ..bookmark import BookmarkApp , BookmarkProxy
46+ from ..bookmark import BookmarkApp , BookmarkProxy , SerializeInputInfo
4747from ..bookmark ._button import BOOKMARK_ID
4848from ..bookmark ._restore_state import RestoreContext
4949from ..http_staticfiles import FileResponse
@@ -1354,7 +1354,7 @@ class Inputs:
13541354 _serializers : dict [
13551355 str ,
13561356 Callable [
1357- [Any , Path | None ],
1357+ [SerializeInputInfo ],
13581358 Awaitable [Any | Unserializable ],
13591359 ],
13601360 ]
@@ -1424,14 +1424,8 @@ def set_serializer(
14241424 self ,
14251425 id : str ,
14261426 fn : (
1427- Callable [
1428- [Any , Path | None ],
1429- Awaitable [Any | Unserializable ],
1430- ]
1431- | Callable [
1432- [Any , Path | None ],
1433- Any | Unserializable ,
1434- ]
1427+ Callable [[SerializeInputInfo ], Awaitable [Any | Unserializable ]]
1428+ | Callable [[SerializeInputInfo ], Any | Unserializable ]
14351429 ),
14361430 ) -> None :
14371431 """
@@ -1442,9 +1436,44 @@ def set_serializer(
14421436 id
14431437 The ID of the input value.
14441438 fn
1445- A function that takes the input value and returns a modified value. The
1446- returned value will be used for test snapshots and bookmarking.
1439+ A function that takes the input value information: an object containing
1440+ `value` and `state_dir`. The function should return a JSON serializable
1441+ value. During execution, the function may save information to disk using the
1442+ `.state_dir` attribute. The returned value will be used for bookmarking.
1443+
1444+ Examples
1445+ --------
1446+
1447+ When setting an input, you can add a suffix to the name: e.g. `"x:shiny.date"`
1448+ or `"y:shiny.password"`. This will cause the name to go through the appropriate
1449+ input handler, e.g. `"shiny.date"`.
1450+
1451+ A good use for this implementation is to set the bookmark serializer for this
1452+ object. An example for inputs handled by `"shiny.password"` is given below where
1453+ the bookmark serializer is set to always ignore this value.
1454+
1455+ ```python
1456+ @shiny.input_handlers.add("shiny.password")
1457+ def _(value: str, name: ResolvedId, session: Session) -> str:
1458+ # Never bookmark passwords
1459+ session.input.set_serializer(name, serializer_unserializable)
1460+
1461+ return value
1462+ ```
1463+
1464+ ```python
1465+ def serializer_capitalize(info: SerializeInputInfo) -> str:
1466+ return info.value.upper()
1467+
1468+ @shiny.input_handlers.add("capitalize")
1469+ def _(value: str, name: ResolvedId, session: Session) -> str:
1470+ # Capitalize the value before bookmarking
1471+ session.input.set_serializer(name, serializer_capitalize)
1472+
1473+ return value
1474+ ```
14471475 """
1476+ # test snapshots and
14481477 self ._serializers [id ] = wrap_async (fn )
14491478
14501479 async def _serialize (
@@ -1476,7 +1505,8 @@ async def _serialize(
14761505
14771506 # Possibly apply custom serialization given the input id
14781507 serializer = self ._serializers .get (key , serializer_default )
1479- serialized_value = await serializer (val , state_dir )
1508+ serializer_info = SerializeInputInfo (value = val , state_dir = state_dir )
1509+ serialized_value = await serializer (serializer_info )
14801510
14811511 # Filter out any values that were marked as unserializable.
14821512 if isinstance (serialized_value , Unserializable ):
0 commit comments