-
I'm sorry if this is answered or explained elsewhere. I'd like to use a dict as a reactive in a widget. If I pass in a dict with keys already existing, the reactivity works fine. However, if I add a new key to the dict it does not update as I might expect. Any pointers on what one might do? Thanks for a great tool! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You may find #2193 helpful here. A fairly useless but hopefully illustrative example could be: from textual.app import App, ComposeResult
from textual.reactive import reactive
from textual.widgets import Header, Footer, Label
class DictReactiveApp( App[ None ] ):
my_dict = reactive(dict(), always_update=True, init=False)
BINDINGS = [
( "space", "change", "" ),
]
def compose( self ) -> ComposeResult:
yield Header()
yield Label(str(self.my_dict))
yield Footer()
def watch_my_dict(self) -> None:
self.query_one(Label).update(str(self.my_dict))
def action_change( self ) -> None:
self.my_dict[len(self.my_dict)] = len(self.my_dict)
self.my_dict = self.my_dict
if __name__ == "__main__":
DictReactiveApp().run() Note that making the |
Beta Was this translation helpful? Give feedback.
-
Thank you! I'll try this out. |
Beta Was this translation helpful? Give feedback.
You may find #2193 helpful here.
A fairly useless but hopefully illustrative example could be: