mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
import httpx
|
|
from rich.text import Text
|
|
|
|
from textual import work
|
|
from textual.app import App, ComposeResult
|
|
from textual.containers import VerticalScroll
|
|
from textual.widgets import Input, Static
|
|
|
|
|
|
class WeatherApp(App):
|
|
"""App to display the current weather."""
|
|
|
|
CSS_PATH = "weather.tcss"
|
|
|
|
def compose(self) -> ComposeResult:
|
|
yield Input(placeholder="Enter a City")
|
|
with VerticalScroll(id="weather-container"):
|
|
yield Static(id="weather")
|
|
|
|
async def on_input_changed(self, message: Input.Changed) -> None:
|
|
"""Called when the input changes"""
|
|
self.update_weather(message.value)
|
|
|
|
@work(exclusive=True)
|
|
async def update_weather(self, city: str) -> None:
|
|
"""Update the weather for the given city."""
|
|
weather_widget = self.query_one("#weather", Static)
|
|
if city:
|
|
# Query the network API
|
|
url = f"https://wttr.in/{city}"
|
|
async with httpx.AsyncClient() as client:
|
|
response = await client.get(url)
|
|
weather = Text.from_ansi(response.text)
|
|
weather_widget.update(weather)
|
|
else:
|
|
# No city, so just blank out the weather
|
|
weather_widget.update("")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app = WeatherApp()
|
|
app.run()
|