how can I move data from screen to another in textual framework

44 views Asked by At

basically my app is rss feeder, the first step is to put all feeds in one screen and I called it ArticlesGrid with publish date, provider name and title, I finished the focus thing and my app is working fine, but it needs to be useful so, I tried to show other data if user press enter like the link and full content, but I can't find anything that I can use like routing or navigating, there is push_screen method but it is push a new screen without any data from previous screen, is there anything can push data for specific cell in grid? like for this card push title, this is my code and I don't know what to do and cant find any code from community.

class ArticlesGrid(Grid):
    def compose(self) -> ComposeResult:
        for article_info in self.content:
            article = ArticleCard(article_info.get("title"))
            article.border_title = article_info.get("provider")
            article.border_subtitle = article_info.get("pub_date")
            yield article

    def handle_key(self, event: events.Key) -> bool:
        if event.key == "enter":
            focused_card = self.focus()
            if focused_card is not None:
                self.app.push_screen(...)

    @property
    def content(self):
        feeds = get_content()
        data = []
        for provider, feed in feeds.items():
            for article in feed:
                data.append(
                    {
                        "provider": provider,
                        "title": article.title,
                        "description": article.description,
                        "content": article.content,
                        "link": article.link,
                        "pub_date": article.pub_date,
                        "author": article.author,
                        "authors": article.authors,
                        "tags": article.tags,
                    }
                )
        return data
1

There are 1 answers

0
Will McGugan On

When you create a custom screen, you can override the constructor to pass in any arguments you may need. Something like the following:

class MyScreen(Screen):
    def __init__(self, greet:str):
        self.greet = greet
        super().__init__()

    def compose(self):
        yield Label(f"Hello {self.greet}")

When you push the screen you can add your arguments:

self.push_screen(MyScreen("Will"))