question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

watch_ method not working with dict

See original GitHub issue

Sorry if the title is off. I’m having a hard time getting watch_ methods working with dict attributes. Here’s an example:

from json import dumps
from textual.app import App, ComposeResult
from textual.reactive import reactive
from textual.widgets import Input, Static


class Example(App):
    display = reactive({"input": ""})

    def compose(self) -> ComposeResult:
        yield Input()
        yield Static()

    def on_input_changed(self, event: Input.Changed) -> None:
        self.display["input"] = event.value

    def watch_display(self, new_display: dict) -> None:
        self.query_one(Static).update(dumps(new_display))


Example().run()

As I type in the Input, the Static’s content doesn’t update. Interestingly, if I swap json.dumps() for rich.Pretty(), it does update, but only after I move the mouse over it. The only way I’ve been able to get what I want is by moving the update() into the on_input_changed() handler.

    def on_input_changed(self, event: Input.Changed) -> None:
        self.display["input"] = event.value
        self.query_one(Static).update(dumps(self.display))

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
davepcommented, Nov 5, 2022

Aye, it’s fine to close if you’re happy with that.

1reaction
aaronstcommented, Nov 3, 2022

Ahh, okay, really appreciate the writeup! I thought maybe that was the case with dict, but seeing the Pretty() update had me convinced that it was at least partially working. Thanks again, really been enjoying learning and working with all the new capability.

Read more comments on GitHub >

github_iconTop Results From Across the Web

xcode - Passing Dictionary to Watch - Stack Overflow
I'm trying to pass data from iPhone -> Watch via Watch Connectivity using background transfer via Application Context method. iPhone ...
Read more >
Dictionaries in Python - Real Python
In this Python dictionaries tutorial, you'll cover the basic characteristics and learn how to access and manage dictionary data. Once you have finished...
Read more >
Python 3.7: How To Use The Dict() Built-in Function - YouTube
In this Python 3.7 tutorial, we will show you how to use the dict () built-in function to create a dictionary. For more...
Read more >
How to pass a dict when a Python function expects **kwargs
Many Python functions have a **kwargs parameter — a dict whose keys and values are populated via keyword arguments. But what if you...
Read more >
Fix TypeError: ' ' not supported between instances of 'dict' and ...
5.2K views 1 year ago Troubleshooting ... not supported between instances of ' dict ' and ' dict ' when your trying to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found