1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
from textual.app import App, ComposeResult
from textual.widgets import Input
from textual_autocomplete import AutoComplete
from textual_autocomplete._autocomplete import DropdownItem, TargetState
class DynamicDataApp(App[None]):
CSS = """
Input {
margin: 2 4;
}
"""
def compose(self) -> ComposeResult:
input_widget = Input()
yield input_widget
yield AutoComplete(input_widget, candidates=self.get_candidates)
def get_candidates(self, state: TargetState) -> list[DropdownItem]:
left = len(state.text)
return [
DropdownItem(item, prefix=f"{left:>2} ")
for item in [
"Apple",
"Banana",
"Cherry",
"Orange",
"Pineapple",
"Strawberry",
"Watermelon",
]
]
if __name__ == "__main__":
app = DynamicDataApp()
app.run()
|