Cannot understand ui.run_javascript() #3376
-
QuestionI want to store the return value of ui.run_javascript() into a variable, but something strange happened while using ui.run_javascript(). First case: from nicegui import ui
async def get_client_lang():
return await ui.run_javascript('navigator.language')
async def notify_lang():
ui.notify(await get_client_lang())
@ui.page('/')
async def index():
ui.button('get lang', on_click=notify_lang)
ui.run() That runs well. Next case: from nicegui import ui
async def get_client_lang():
return await ui.run_javascript('navigator.language')
async def notify_lang():
ui.notify(await get_client_lang())
@ui.page('/')
async def index():
lang = await get_client_lang()
ui.button('get lang', on_click=ui.notify(lang))
ui.run() This case, Are there special cautions when using that function? |
Beta Was this translation helpful? Give feedback.
Answered by
python-and-fiction
Jul 21, 2024
Replies: 1 comment 1 reply
-
you should wait client connected. async def get_client_lang():
return await ui.run_javascript('navigator.language')
async def notify_lang():
ui.notify(await get_client_lang())
@ui.page('/')
async def index():
await ui.context.client.connected()
lang = await get_client_lang()
ui.button('get lang', on_click=lambda :ui.notify(lang)) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
snoman88
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you should wait client connected.