Reactive doesn't seem to work at all #1683
Replies: 6 comments 2 replies
-
I don't think it's supposed to work like that? You initialize the label once and that's it. You would need a Label subclass that has watch_name. |
Beta Was this translation helpful? Give feedback.
-
Textual calls You can react to a changing attribute with a watch method, and update any children accordingly. Here's an example... import time
from textual.app import App
from textual.reactive import reactive
from textual.widgets import Header, Footer, Label
class MyApp(App):
BINDINGS = [("t", "test", "Test")]
name = reactive("default", init=False)
def compose(self):
yield Header()
yield Label(self.name)
yield Footer()
def action_test(self):
self.name = str(time.time())
def watch_name(self, name: str) -> None:
self.query_one(Label).update(name)
app = MyApp()
if __name__ == "__main__":
app.run() |
Beta Was this translation helpful? Give feedback.
-
Something like this:
|
Beta Was this translation helpful? Give feedback.
-
oh ok. I had a similar thing working to that. The documentation might be slightly misleading then. Right at the top of https://textual.textualize.io/guide/reactivity/#reactive-attributes it says: The watch method seems useful, I'll probably use that. But the "Smart Refresh" ability seems totally indistinguishable from just setting the value. Example:
This doesn't use reactive at all. In that case, it seems like the smart refresh doesn't actually exist. (The other "superpowers" seem cool though and will work, mostly just curious now) Thanks for the response, by the way |
Beta Was this translation helpful? Give feedback.
-
Also found this on that same page, this seems pretty close to what I was going for; however it does use the render method. Is the render method called multiple times (on update etc.)?
|
Beta Was this translation helpful? Give feedback.
-
Ok this works pretty well.
I sort of wish it worked with the compose method. But that's ok, I'm good to close the discussion knowing that render smartly follows a reactive value. I also like the prettiness of not having to query in this example. Thanks again folks! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using version
textual = {extras = ["dev"], version = "^0.10.1"}
When I run this, the text shows "default". If I hit t, it does not update the name value. If I query_one and update the label, that works, but bypasses the whole point of reactive.
I've also tried instantiating it with Reactive instead, same result.
Beta Was this translation helpful? Give feedback.
All reactions