Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computed methods aren't called from mutate_reactive #4807

Closed
Banbury opened this issue Jul 26, 2024 · 5 comments
Closed

Computed methods aren't called from mutate_reactive #4807

Banbury opened this issue Jul 26, 2024 · 5 comments

Comments

@Banbury
Copy link

Banbury commented Jul 26, 2024

Computed methods are not called if a reactive is updated with mutate_reactive.

Example:

from textual.app import App, ComposeResult
from textual.widgets import Static, Button
from textual.reactive import reactive

class TestWidget(Static):
    msg: reactive[list[str]] = reactive(list, recompose=True)
    count: reactive(0)

    def compute_count(self):
        return len(self.msg)

    def compose(self) -> ComposeResult:
        yield Static(" ".join(self.msg))


class TestApp(App):
    msg: reactive[list[str]] = reactive(list, recompose=True)
    def compose(self) -> ComposeResult:
        yield TestWidget().data_bind(TestApp.msg)
        yield Button("Click me", id="button")

    def on_button_pressed(self, event: Button.Pressed) -> None:
        self.msg.append("Hello")
        self.mutate_reactive(TestApp.msg)

if __name__ == "__main__":
    app = TestApp()
    app.run()

Place a breakpoint in compute_count and run the app. Press the button. The execution doesn't stop at the breakpoint.

Copy link

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@TomJGooding
Copy link
Contributor

From a quick glance, it looks like you're not actually creating a reactive here - try changing : to =?

count: reactive(0)

@willmcgugan
Copy link
Collaborator

@Banbury Tom is correct. If you make this change, then the compute method is called:

count = reactive(0)

Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@Banbury
Copy link
Author

Banbury commented Jul 26, 2024

Textual is a great framework. But reactives and data binding are confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants