Skip to content

async callback and ui element with Element.on method #280

Closed Answered by falkoschindler
SebastienDorgan asked this question in Q&A
Discussion options

You must be logged in to vote

It looks like there is a limitation in the generic event subscription using .on(). Here is a minimum reproduction:

import asyncio
from nicegui import ui

async def handle_click():
    await asyncio.sleep(0.1)
    ui.notify('clicked')

ui.button('Works', on_click=handle_click)
ui.button('Fails').on('click', lambda _: handle_click())

ui.run()

The event handler for on_click will automatically keep the current UI context open as long as some async function is awaited. .on() should do that as well. I'll open a new issue.


A workaround for you would be to explicitly set the context in on_drag_end somehow like this:

with card:
    absolute_app_card(app, x - rec.x, y - rec.y)

This way the ui.card()

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@SebastienDorgan
Comment options

@falkoschindler
Comment options

@SebastienDorgan
Comment options

@rodja
Comment options

@SebastienDorgan
Comment options

Answer selected by SebastienDorgan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants