Add custom clientdata to shiny

Hi,

i was trying to extend the existing clientdata in shiny by adding the clientside timezone. I tried subclassing the python ClientData and just add a new method, that reads inputs the same way its already done. Now the issue is setting the reactive value in js. The existing has code to initialize the value, before even starting the app connection. If i set my value like this:

$(document).on('shiny:connected', function () {
    const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
    Shiny.setInputValue('.clientdata_timezone', tz);
});

The server will crash unless i explicitly catch the server accesses, where the value does not yet exist.

class ExtendedClientData(ClientData):
    def timezone(self) -> str | None:
        try:
            return cast(str, self._read_input("timezone"))
        except ValueError:
            return None

This is bad, as i then have to explicitly check when the value has arrived and cannot just take it for granted.

The existing clientdata values counteract this by setting values before connecting, thus making sure the values are also available on the server from the get go (as far as i have understood).

Have i overlooked something here? Is there a proper way to do this?

Thanks

1 Like