run_method with return type #1471
Replies: 5 comments 4 replies
-
Thanks for the feature request, @WolfgangFahl! This would indeed be a useful extension to the existing API. It's probably a bit tricky, because |
Beta Was this translation helpful? Give feedback.
-
@falkoschindler - i think a new run_and_await_method is just fine and the original would be the "fire and forget" version. |
Beta Was this translation helpful? Give feedback.
-
I think |
Beta Was this translation helpful? Give feedback.
-
Here are ChatGPT's suggestions for alternative names:
|
Beta Was this translation helpful? Give feedback.
-
I just experimented with an implementation for import asyncio
from nicegui import background_tasks, ui
def run_method(command: str) -> asyncio.Task:
print(f'Send method call "{command}" to client...')
async def wait_for_response():
print('Wait for response...')
await asyncio.sleep(0.5)
return f'Response to "{command}"'
return background_tasks.create(wait_for_response())
def fire_and_forget():
run_method('fire_and_forget')
async def wait_for_response():
result = await run_method('wait_for_response')
ui.notify(result)
ui.button('Fire and forget', on_click=fire_and_forget)
ui.button('Wait for response', on_click=wait_for_response)
ui.run() The synchronous function returns a Task which can be ignored or awaited, which looks very clean to me. Maybe way we can also improve the However, there is a downside: At the moment of sending the method call to the client, we don't know whether the function will be awaited. Therefore we can't tell the client whether a response is expected and the client needs to always send the response back to the server. But in most cases this should be negligible. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
See #1446
For a start a string value return type would be helpful. Having more datatypes such as int/float/string/date and so on would be useful but i think it would then suffice to return a json string with the intended content that can be deserialized on the python side.
Beta Was this translation helpful? Give feedback.
All reactions