Weather App Determined by City and Country Input

  • Share this:

Code introduction


This function creates a weather application using the Textual library, which determines the weather based on a string input containing the city and country names.


Technology Stack : Textual library

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from textual import App, Input, Component

class WeatherComponent(Component):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.weather = "sunny"

    async def render(self):
        return f"The weather is {self.weather}."

def xxx(city, country):
    class WeatherApp(App):
        def create_components(self):
            return [WeatherComponent(weather="sunny")]

        async def on_load(self):
            city_country = f"{city}, {country}"
            # Simulate fetching weather data
            self.components[0].weather = "rainy" if "r" in city_country.lower() else "sunny"

    app = WeatherApp()
    app.run()