Best practices for app to widget actions / messages / events #1648
-
Hi, thanks for a such great project! I'm trying to figure out what is the best practice to propagate actions from app to widgets. For example, if I had a key binding in App class which launches some actions on its all child widgets. Eventually, I wrote this simple code which works just fine: from textual.app import App, ComposeResult
import asyncio
from textual.widgets import Header, Footer
from textual.containers import Container
from textual.widgets import DataTable, ListView, ListItem, Label, Button
from textual.widget import Widget
class RunnerListItem(ListItem):
def compose(self) -> ComposeResult:
yield Button('Run')
async def action_run(self):
self.log('Runner run')
class TableTestApp(App):
BINDINGS = [("r", "run", "Run all")]
def compose(self) -> ComposeResult:
"""Create child widgets for the app."""
yield ListView(
RunnerListItem(),
RunnerListItem(),
RunnerListItem(),
id='list'
)
yield Footer()
async def action_run(self):
self.log('App action run')
await asyncio.gather(*[r.action_run()
for r in self.query(RunnerListItem)])
if __name__ == "__main__":
app = TableTestApp()
app.run() However, I tried some weird paths which didn't work, and I'm wondering if I didn't get the idea correctly, or I found some bugs. Could you confirm my thinking and answer some questions?
Can you confirm if my understanding of design principles is correct, according to the docs and examples projects I saw?
Hopefully you'll find time to finish #1532 documentation for compound widgets anytime soon. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hey there, thanks for opening this issue! (Consider taking a look at this blog post with some suggestions on how to best get help to use Textual :) ) Regarding your questions:
Regarding your understanding of design principles:
Issue 1532 is at the top of my TODO list, don't worry :) In the meantime, because this isn't a feature request/bug report and because we already have issues that track the lack of documentation that should address your questions and concerns, I will ask you to consider closing this issue. Either way, thanks for taking the time to reach out to us! |
Beta Was this translation helpful? Give feedback.
-
Thank you, for clarification. |
Beta Was this translation helpful? Give feedback.
Hey there, thanks for opening this issue!
(Consider taking a look at this blog post with some suggestions on how to best get help to use Textual :) )
Regarding your questions:
action_
methods are screen/app methods and Textual will only triggeraction_
methods in screens/apps. Of course, you can have a method with a nameaction_bla_bla
in another widget, but that is likely to create confusion because that methodaction_bla_bla
will never be called automatically by Textual.ap…