The official docs list param.watch and pn.bind as the official ways to create asynchronous widgets. However, ideally one would not have to add extra params/decorators for each async function I want to trigger, and could use the native asyncio.create_task .
Let’s say we have this class and display two instances. We call update_layout
a through a param trigger and through asyncio.
import asyncio
import panel as pn
import param
class Counter(param.Parameterized):
counts = param.Integer(default=0)
action = param.Action()
def __init__(self, **params):
super().__init__(**params)
self.layout = pn.Card(objects=[self.param.counts])
async def check_conditions(self):
while self.counts < 5:
await asyncio.sleep(1)
self.counts += 1
@param.depends("action", watch=True)
async def update_layout(self):
self.layout.append(pn.widgets.LoadingSpinner(value=True))
await self.check_conditions()
self.layout.objects = ["<h1>done</h1"]
def __panel__(self):
return self.layout
objs = []
for i in range(2):
objs.append(Counter())
for obj in objs:
# obj.param.trigger("action") <--- works, as expected
asyncio.create_task(obj.update_layout()) # <-- AttributeError: 'str' object has no attribute 'select'
col = pn.Column()
col.extend(objs)
col.servable()
When only one object is created in the loop, there is no error, but with two it seems some reference gets confused. Any idea what could be going on here?
Traceback (most recent call last):
File "/Users/myuser/.pyenv/versions/3.9.6/lib/python3.9/site-packages/panel/viewable.py", line 591, in _preprocess
hook(self, root, changed, old_models)
TypeError: link_param_method() takes 2 positional arguments but 4 were given
During handling of the above exception, another exception occurred:
....
objects += obj.select(selector)
AttributeError: 'str' object has no attribute 'select' # THIS str refers to the "<h1>done</h1>" string!