Skip to content

Commit

Permalink
add hint about new API
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Oct 19, 2023
1 parent 075f824 commit 10d4b85
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 12 additions & 1 deletion nicegui/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,23 @@ async def disconnected(self, check_interval: float = 0.1) -> None:
await asyncio.sleep(check_interval)
self.is_waiting_for_disconnect = False

def run_javascript(self, code: str, *, timeout: float = 1.0, check_interval: float = 0.01) -> AwaitableResponse:
def run_javascript(self, code: str, *,
respond: Optional[bool] = None, # DEPRECATED
timeout: float = 1.0, check_interval: float = 0.01) -> AwaitableResponse:
"""Execute JavaScript on the client.
The client connection must be established before this method is called.
You can do this by `await client.connected()` or register a callback with `client.on_connect(...)`.
"""
if respond is True:
globals.log.warning('The "respond" argument of run_javascript() has been removed. '
'Now the method always returns an AwaitableResponse that can be awaited. '
'Please remove the "respond=True" argument.')
if respond is False:
raise ValueError('The "respond" argument of run_javascript() has been removed. '
'Now the method always returns an AwaitableResponse that can be awaited. '
'Please remove the "respond=False" argument and call the method without awaiting.')

request_id = str(uuid.uuid4())
target_id = globals._socket_id or self.id # pylint: disable=protected-access

Expand Down
15 changes: 14 additions & 1 deletion nicegui/functions/javascript.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from typing import Optional

from .. import globals # pylint: disable=redefined-builtin
from ..awaitable_response import AwaitableResponse


def run_javascript(code: str, *, timeout: float = 1.0, check_interval: float = 0.01) -> AwaitableResponse:
def run_javascript(code: str, *,
respond: Optional[bool] = None, # DEPRECATED
timeout: float = 1.0, check_interval: float = 0.01) -> AwaitableResponse:
"""Run JavaScript
This function runs arbitrary JavaScript code on a page that is executed in the browser.
Expand All @@ -16,6 +20,15 @@ def run_javascript(code: str, *, timeout: float = 1.0, check_interval: float = 0
:return: response from the browser, or `None` if `respond` is `False`
"""
if respond is True:
globals.log.warning('The "respond" argument of run_javascript() has been removed. '
'Now the function always returns an AwaitableResponse that can be awaited. '
'Please remove the "respond=True" argument.')
if respond is False:
raise ValueError('The "respond" argument of run_javascript() has been removed. '
'Now the function always returns an AwaitableResponse that can be awaited. '
'Please remove the "respond=False" argument and call the function without awaiting.')

client = globals.get_client()
if not client.has_socket_connection:
raise RuntimeError('Cannot run JavaScript before client is connected; '
Expand Down

0 comments on commit 10d4b85

Please sign in to comment.