How to close the current page by button click? #1542
-
QuestionHi All, I am trying to create a button, which when pressed, will close the current page on the browser. Recently in my another question #1529 , I learnt how to use the async def close_page():
await ui.run_javascript('window.close()') So this works, the page does close. But I am getting some warning messages in the console. Is this normal to happen? Or is there a correct way to do this? I am sharing the console warning message below for reference: Task exception was never retrieved
future: <Task finished name='Task-50' coro=<AsyncServer._handle_event_internal() done, defined at C:\apps\test\hivespace\Lib\site-packages\socketio\asyncio_server.py:522> exception=KeyError('result')>
Traceback (most recent call last):
File "C:\apps\test\hivespace\Lib\site-packages\socketio\asyncio_server.py", line 524, in _handle_event_internal
r = await server._trigger_event(data[0], namespace, sid, *data[1:])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\apps\test\hivespace\Lib\site-packages\socketio\asyncio_server.py", line 562, in _trigger_event
ret = handler(*args)
^^^^^^^^^^^^^^
File "C:\apps\test\hivespace\Lib\site-packages\nicegui\nicegui.py", line 187, in on_javascript_response
handle_javascript_response(client, msg)
File "C:\apps\test\hivespace\Lib\site-packages\nicegui\nicegui.py", line 191, in handle_javascript_response
client.waiting_javascript_commands[msg['request_id']] = msg['result']
~~~^^^^^^^^^^
KeyError: 'result'
JavaScript did not respond in time
Traceback (most recent call last):
File "C:\apps\test\hivespace\Lib\site-packages\nicegui\events.py", line 388, in wait_for_result
await result
File "C:\apps\test\api_tests\code_tests.py", line 38, in close_page
await ui.run_javascript('window.close()')
File "C:\apps\test\hivespace\Lib\site-packages\nicegui\functions\javascript.py", line 27, in run_javascript
return await client.run_javascript(code, respond=respond, timeout=timeout, check_interval=check_interval)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\apps\test\hivespace\Lib\site-packages\nicegui\client.py", line 145, in run_javascript
raise TimeoutError('JavaScript did not respond in time')
TimeoutError: JavaScript did not respond in time |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
The from nicegui import ui
async def close_page():
await ui.run_javascript('window.close()', respond=False)
ui.button('Button', on_click=lambda: close_page())
ui.run() |
Beta Was this translation helpful? Give feedback.
-
To be honest I am not a Javascript expert. It does appear what you are trying to do isn't super straight forward in general. Maybe this link will help? |
Beta Was this translation helpful? Give feedback.
The
run_javascript
method not only executes the code, it also allows for data to be returned from the page. I am guessing the return hooks don't like that you closed the page before they could do their thing. This seems to keep it from trying to wait on data return: