panel icon indicating copy to clipboard operation
panel copied to clipboard

Handle notifications triggered in async callbacks

Open maximlt opened this issue 3 years ago • 0 comments

The small app below emits a notification after waiting for a little while part of an async callback that is triggered on a button click. To reproduce the traceback I've obtained, launch the app and open two sessions. Click on the debug button on the first session and then on the second session. You should see only one notification being rendered, on the second session.

import asyncio
import panel as pn

pn.config.notifications = True

b = pn.widgets.Button(name='debug')

async def cb(e):
    await asyncio.sleep(5)
    pn.state.notifications.error('Notif')

b.on_click(cb)

tpl = pn.template.MaterialTemplate()

tpl.main.append(b)

tpl.servable()

Traceback:

2022-06-13 12:31:51,261 Exception in callback functools.partial(<bound method IOLoop._discard_future_result of <tornado.platform.asyncio.AsyncIOMainLoop object at 0x7fe6310c2a60>>, <Task finished name='Task-176' coro=<async_execute.<locals>.wrapper() done, defined at /Users/mliquet/work/dev/panel/panel/io/server.py:141> exception=AttributeError("'NoneType' object has no attribute 'error'")>)
Traceback (most recent call last):
  File "/Users/mliquet/miniconda3/envs/panel-dev38/lib/python3.8/site-packages/tornado/ioloop.py", line 741, in _run_callback
    ret = callback()
  File "/Users/mliquet/miniconda3/envs/panel-dev38/lib/python3.8/site-packages/tornado/ioloop.py", line 765, in _discard_future_result
    future.result()
  File "/Users/mliquet/work/dev/panel/panel/io/server.py", line 144, in wrapper
    return await func(*args, **kw)
  File "/Users/mliquet/work/dev/panel/mltmess/issue_notifs_shared.py", line 10, in cb
    pn.state.notifications.error('This is me!')
AttributeError: 'NoneType' object has no attribute 'error'

maximlt avatar Jun 13 '22 10:06 maximlt