Delayed initialization in reactivity #4007
Unanswered
jakubziebin
asked this question in
Q&A
Replies: 1 comment
-
|
Why not handle it pretty much like your final analogy, only the from textual import on
from textual.app import App, ComposeResult
from textual.reactive import var
from textual.widgets import Button
class DelayedReactiveApp(App[None]):
_username: var[str | None] = var(None)
@property
def username(self) -> str:
if self._username is None:
raise Exception("Nope")
return self._username
@username.setter
def username(self, new_username: str) -> None:
self._username = new_username
def compose(self) -> ComposeResult:
yield Button("Show the username (might crash)", id="show")
yield Button("Set the username", id="set")
@on(Button.Pressed, "#show")
def show_username(self) -> None:
self.notify(self.username)
@on(Button.Pressed, "#set")
def set_username(self) -> None:
self.username = "davep"
self.notify("Username has been set")
if __name__ == "__main__":
DelayedReactiveApp().run()This way the "public" interface for |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I have some problem with reactivity. I want to have a reactive data container like dict, dataclass, etc..
But then I can't instantiate it in right time, because it is already forced to be instantiated in the reactive() call.
Which means that all the properties in my dataclass must have default values.
But I don't want that. Because in code which uses these properties in some scenarios I need to have a unnecessary
if/else statement to check if the actual data is already there or not.
This is more or less how it looks in the code:
I hope the example above is clear enough. It is a simple example in which also removing | None should do the job:
but in a more complex case it is not possible and...
I wanted to ask is there a way to have that reactive later evaluated? I mean I don't want to instantiate
the DataHolder class right in the reactive(), but I want to set it later, when my data arrives. Is that possible?
Maybe should be with some syntax that is common in python (e.g. in Typer) I mean ... to mark it like that:
content: DataHolder = reactive(..., init=False)What I actually tried is a workaround like:
content: DataHolder = reactive(None, init=False)but this is not a good solution, because it won't throw an error if someone try to access content by accident before
it is set. Neither a good solution is to use | None in the dataclass, because then I need to have a if/else
statement in every place where I use it:
content: DataHolder | None = reactive(None, init=False)A good analogy is the way how to handle it via @Property:
I think same thing should be possible with reactive() as well but we cannot have one reactive depend on another one.
I hope I was clear enough. Thanks in advance for any help.
Beta Was this translation helpful? Give feedback.
All reactions