mirror of
https://github.com/django-components/django-components.git
synced 2025-11-07 01:56:42 +00:00
39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
# 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 = """
|
|
<div class="bg-white rounded-lg shadow-md p-6">
|
|
<h3 class="text-xl font-semibold text-gray-800 mb-2">
|
|
Weather in {{ location }}
|
|
</h3>
|
|
<p class="text-gray-600">
|
|
<strong class="font-medium text-gray-700">Temperature:</strong>
|
|
{{ temperature }}
|
|
</p>
|
|
<p class="text-gray-600">
|
|
<strong class="font-medium text-gray-700">Condition:</strong>
|
|
{{ condition }}
|
|
</p>
|
|
</div>
|
|
"""
|