Skip to content

Instantly share code, notes, and snippets.

@sellisd
Last active January 9, 2025 17:45
Show Gist options
  • Save sellisd/cbc2e715ff7cea76910d7488fb0e8a7f to your computer and use it in GitHub Desktop.
Save sellisd/cbc2e715ff7cea76910d7488fb0e8a7f to your computer and use it in GitHub Desktop.
parallel textual lists
from textual.app import App, ComposeResult
from textual.widgets import ListView, ListItem, Label
from textual.containers import Horizontal, Container
from textual.widget import Widget
from textual.reactive import reactive
class Child(Widget):
def __init__(self, parent_widget,**kwargs):
super().__init__(**kwargs)
self.parent_widget = parent_widget
def compose(self) -> ComposeResult:
species = [ListItem(Label(item)) for item in ["cat","dog"]]
self.species_view = ListView(*species)
yield self.species_view
def update_species(self, kingdom):
if kingdom == "Animal":
species = ["cat", "dog"]
else:
species = ["palm", "olive tree"]
# Clear existing items
self.species_view.clear()
# Add new items to existing ListView
for item in species:
self.species_view.append(ListItem(Label(item)))
class TUI(App):
active_kingdom = reactive("Animal")
def __init__(self) -> None:
super().__init__()
self.kingdom_view = ListView()
def compose(self) -> ComposeResult:
"""Create children for layout"""
kingdoms = [ListItem(Label(item)) for item in ["Animal", "Plant"]]
self.kingdom_view = ListView(*kingdoms)
self.species_view = Child(self.active_kingdom)
yield Horizontal(
Container(self.kingdom_view),
Container(self.species_view)
)
def on_list_view_highlighted(self, message: ListView.Highlighted) -> None:
self.active_kingdom = message.item
self.species_view.update_species(self.active_kingdom)
if __name__ == "__main__":
app = TUI()
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment