# ruff: noqa: S311 import random from django_components import Component, register, types DESCRIPTION = "A component that catches errors and displays fallback content, similar to React's ErrorBoundary." @register("weather_widget") class WeatherWidget(Component): class Kwargs: location: str simulate_error: bool = False def get_template_data(self, args, kwargs: Kwargs, slots, context): if kwargs.simulate_error: raise OSError(f"Failed to connect to weather service for '{kwargs.location}'.") return { "location": kwargs.location, "temperature": f"{random.randint(10, 30)}°C", "condition": random.choice(["Sunny", "Cloudy", "Rainy"]), } template: types.django_html = """

Weather in {{ location }}

Temperature: {{ temperature }}

Condition: {{ condition }}

"""