mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
replace TextInput with Input
This commit is contained in:
@@ -11,8 +11,8 @@ except ImportError:
|
||||
from rich.markdown import Markdown
|
||||
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.containers import Vertical
|
||||
from textual.widgets import Static, TextInput
|
||||
from textual.containers import Content
|
||||
from textual.widgets import Static, Input
|
||||
|
||||
|
||||
class DictionaryApp(App):
|
||||
@@ -21,10 +21,10 @@ class DictionaryApp(App):
|
||||
CSS_PATH = "dictionary.css"
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield TextInput(placeholder="Search for a word")
|
||||
yield Vertical(Static(id="results"), id="results-container")
|
||||
yield Input(placeholder="Search for a word")
|
||||
yield Content(Static(id="results"), id="results-container")
|
||||
|
||||
async def on_text_input_changed(self, message: TextInput.Changed) -> None:
|
||||
async def on_input_changed(self, message: Input.Changed) -> None:
|
||||
"""A coroutine to handle a text changed message."""
|
||||
if message.value:
|
||||
# Look up the word in the background
|
||||
@@ -39,22 +39,26 @@ class DictionaryApp(App):
|
||||
async with httpx.AsyncClient() as client:
|
||||
results = (await client.get(url)).json()
|
||||
|
||||
if word == self.query_one(TextInput).value:
|
||||
if word == self.query_one(Input).value:
|
||||
markdown = self.make_word_markdown(results)
|
||||
self.query_one("#results", Static).update(Markdown(markdown))
|
||||
|
||||
def make_word_markdown(self, results: list[Any]) -> str:
|
||||
def make_word_markdown(self, results: object) -> str:
|
||||
"""Convert the results in to markdown."""
|
||||
lines = []
|
||||
for result in results:
|
||||
lines.append(f"# {result['word']}")
|
||||
lines.append("")
|
||||
for meaning in result.get("meanings", []):
|
||||
lines.append(f"_{meaning['partOfSpeech']}_")
|
||||
if isinstance(results, dict):
|
||||
lines.append(f"# {results['title']}")
|
||||
lines.append(results["message"])
|
||||
elif isinstance(results, list):
|
||||
for result in results:
|
||||
lines.append(f"# {result['word']}")
|
||||
lines.append("")
|
||||
for definition in meaning.get("definitions", []):
|
||||
lines.append(f" - {definition['definition']}")
|
||||
lines.append("---")
|
||||
for meaning in result.get("meanings", []):
|
||||
lines.append(f"_{meaning['partOfSpeech']}_")
|
||||
lines.append("")
|
||||
for definition in meaning.get("definitions", []):
|
||||
lines.append(f" - {definition['definition']}")
|
||||
lines.append("---")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user