other placeholder not interactive while awaiting for data #557
-
Hi, my understanding is that since most calls are async, one can update a tile while still have access to other tiles. However, in the example below, when clicking on a menu item in the tree, everything hangs for 3 seconds. What am I not understanding correctly? Thanks async def get_sell_prices():
await asyncio.sleep(3)
return "foo"
class SimpleApp(App):
async def on_mount(self) -> None:
self.context_placeholder = ScrollView(gutter=1)
tree = TreeControl("Menu", {})
# await fill_tree(tree)
await tree.add(tree.root.id, "Menu1", data="1")
await tree.add(tree.root.id, "Menu2", data="2")
await tree.nodes[0].expand()
await self.view.dock(tree, edge="left", size=40)
await self.view.dock(self.context_placeholder, Placeholder(), edge="top")
async def handle_tree_click(self, message: TreeClick[dict]) -> None:
"""Called in response to a tree click."""
stdout = await get_sell_prices()
await self.context_placeholder.update(stdout)
SimpleApp.run(log="textual.log") |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
There is a queue of messages for each app / widget. Your sleep will prevent other messages being processed until it returns. If you wan't something to run in "the background", you will need to use asyncio Tasks. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your reply! Do you maybe have a working example somewhere? |
Beta Was this translation helpful? Give feedback.
-
This works, awesome! Thanks class SimpleApp(App):
context = Reactive("")
async def update_context(self):
await asyncio.sleep(3)
self.context = self.context + "foo"
async def watch_context(self, context):
await self.context_placeholder.update(context)
async def on_mount(self) -> None:
self.context_placeholder = ScrollView(gutter=1)
tree = TreeControl("Menu", {})
# await fill_tree(tree)
await tree.add(tree.root.id, "Menu1", data="1")
await tree.add(tree.root.id, "Menu2", data="2")
await tree.nodes[0].expand()
await self.view.dock(tree, edge="left", size=40)
await self.view.dock(self.context_placeholder, Placeholder(), edge="top")
async def handle_tree_click(self, message: TreeClick[dict]) -> None:
"""Called in response to a tree click."""
# stdout = await get_sell_prices()
# await self.context_placeholder.update("baz")
asyncio.create_task(self.update_context())
SimpleApp.run(log="textual.log") |
Beta Was this translation helpful? Give feedback.
There is a queue of messages for each app / widget. Your sleep will prevent other messages being processed until it returns.
If you wan't something to run in "the background", you will need to use asyncio Tasks.