Question About Fetching Browser's Timezone #3096
-
QuestionApologize if this is not directly related to the framework, but I am facing some trouble getting and saving browser's timezone. Original Requirement: To get and save the timezone from the browser, and utilize it as input for components: async def get_or_create_user_timezone() -> str:
timezone = app.storage.user.get("timezone")
if not timezone:
timezone = await ui.run_javascript(
"Intl.DateTimeFormat().resolvedOptions().timeZone"
)
logger.info(f"User timezone: {timezone}")
app.storage.user["timezone"] = timezone
return timezone
app.on_connect(get_or_create_user_timezone) Unfortunately it failed (due to the absence of request?), an error was raised: File "/Users/henry/repo/private/beaver_habits/.venv/lib/python3.12/site-packages/nicegui/storage.py", line 143, in user
raise RuntimeError('app.storage.user needs a storage_secret passed in ui.run()')
RuntimeError: app.storage.user needs a storage_secret passed in ui.run() I also tried middleware but the javascript execution failed, is there any clever way to solve this problem? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @daya0576, This looks like issue #2520 which is still unresolved. As a workaround you can await the client connection in your page function(s) like this: async def get_or_create_user_timezone() -> None:
timezone = await ui.run_javascript("Intl.DateTimeFormat().resolvedOptions().timeZone")
print(f"User timezone: {timezone}")
app.storage.user["timezone"] = timezone
@ui.page('/')
async def page():
await ui.context.client.connected()
await get_or_create_user_timezone()
ui.label('Hello, world!') |
Beta Was this translation helpful? Give feedback.
Hi @daya0576,
This looks like issue #2520 which is still unresolved. As a workaround you can await the client connection in your page function(s) like this: