mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
added textual.getters
This commit is contained in:
@@ -6,7 +6,7 @@ except ImportError:
|
||||
raise ImportError("Please install httpx with 'pip install httpx' ")
|
||||
|
||||
|
||||
from textual import work
|
||||
from textual import getters, work
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.containers import VerticalScroll
|
||||
from textual.widgets import Input, Markdown
|
||||
@@ -17,6 +17,9 @@ class DictionaryApp(App):
|
||||
|
||||
CSS_PATH = "dictionary.tcss"
|
||||
|
||||
results = getters.query_one("#results", Markdown)
|
||||
input = getters.query_one(Input)
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Input(placeholder="Search for a word", id="dictionary-search")
|
||||
with VerticalScroll(id="results-container"):
|
||||
@@ -28,7 +31,7 @@ class DictionaryApp(App):
|
||||
self.lookup_word(message.value)
|
||||
else:
|
||||
# Clear the results
|
||||
await self.query_one("#results", Markdown).update("")
|
||||
await self.results.update("")
|
||||
|
||||
@work(exclusive=True)
|
||||
async def lookup_word(self, word: str) -> None:
|
||||
@@ -40,12 +43,12 @@ class DictionaryApp(App):
|
||||
try:
|
||||
results = response.json()
|
||||
except Exception:
|
||||
self.query_one("#results", Markdown).update(response.text)
|
||||
self.results.update(response.text)
|
||||
return
|
||||
|
||||
if word == self.query_one(Input).value:
|
||||
if word == self.input.value:
|
||||
markdown = self.make_word_markdown(results)
|
||||
self.query_one("#results", Markdown).update(markdown)
|
||||
self.results.update(markdown)
|
||||
|
||||
def make_word_markdown(self, results: object) -> str:
|
||||
"""Convert the results into markdown."""
|
||||
|
||||
Reference in New Issue
Block a user