Last active
May 2, 2023 20:06
-
-
Save mharris717/8febe8d7719c0d47e2cc99e47aaecdbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from textual.app import App | |
from textual.reactive import reactive | |
from textual.widget import Widget | |
from textual.widgets import Footer, Header, Input, Label, ListItem, ListView, Static | |
""" | |
My naive expectation was that, when I update Numbers#term, the ListView would | |
re-render. But it doesn't. | |
I am almost positive I understand why. If term was used in a render method, it would smart refresh. | |
And I know how to make it work like I want manually, or with a watch method. | |
But I'm wondering if there's a more idiomatic way to do this. | |
""" | |
class NumbersApp(App): | |
CSS_PATH = "main.css" | |
def compose(self): | |
yield Header() | |
yield SearchTerm() | |
yield Numbers() | |
yield Footer() | |
def on_input_submitted(self, event): | |
val = self.query_one(Input).value | |
self.query_one(Numbers).search(int(val)) | |
class Numbers(Widget): | |
term = reactive(1) | |
def compose(self): | |
yield Static(f"Term: {self.term}") | |
list_items = [ListItem(Label(x)) for x in self.get_number_rows()] | |
yield ListView(*list_items) | |
def search(self, val): | |
self.term = val | |
def get_number_rows(self): | |
res = [f"Num: {x}" for x in range(100) if x % self.term == 0] | |
return res[:20] | |
class SearchTerm(Widget): | |
def compose(self): | |
yield Static("search") | |
yield Input() | |
NumbersApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment