Conditionally showing widget which depends on data being loaded #2009
-
I want to show a widget only after some data have been loaded. I have I then have a ... which now makes sense to me. One solution would be to refactor my Any better ideas? 🤔 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
If I'm reading this correctly (keep a widget hidden until some value is set on a property), perhaps a reactive along with a watch and some CSS magic? from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, Label
from textual.reactive import reactive
class MyLabel( Label ):
DEFAULT_CSS = """
.hidden {
display: none;
}
"""
text: reactive[str | None] = reactive(None)
def __init__( self ) -> None:
super().__init__( classes="hidden" )
def watch_text( self ) -> None:
if self.text is not None:
self.update( self.text )
self.set_class( self.text is None, "hidden" )
class ReactShow( App[ None ] ):
CSS = """
Screen {
align: center middle;
}
"""
BINDINGS = [
( "s", "set_text", "Set some text" ),
]
def compose( self ) -> ComposeResult:
yield Header()
yield MyLabel()
yield Footer()
def action_set_text( self ) -> None:
self.query_one( MyLabel ).text = "S U R P R I S E!!"
if __name__ == "__main__":
ReactShow().run() |
Beta Was this translation helpful? Give feedback.
If I'm reading this correctly (keep a widget hidden until some value is set on a property), perhaps a reactive along with a watch and some CSS magic?