Spaces:
Paused

yaz / src /apps /shared /ui /renderer.py
anaselamr's picture
fix: HF Spaces access + compact branding + client guide
5b36da5
Raw History Blame Contribute Delete
7.39 kB
"""
Card Template Renderer - Renders sliced cards to HTML
Converts card data structures to HTML using card slicing methodology
"""
from typing import Dict, Any, List
from jinja2 import Environment, FileSystemLoader, Template
import os
from pathlib import Path
class CardRenderer:
"""
Renders sliced card components to HTML using Jinja2 templates
"""
def __init__(self):
# Setup Jinja2 environment
template_dir = Path(__file__).parent / "templates"
self.env = Environment(loader=FileSystemLoader(str(template_dir)), autoescape=True)
# Add custom functions to Jinja2 environment
self.env.globals["render_card"] = self._render_card_recursive
self.template_mapping = {
"metric_card": "metric_card.html",
"delta_card": "metric_card.html", # Use same template
"status_card": "status_card.html",
"chart_card": "metric_card.html", # Fallback for now
"info_card": "metric_card.html", # Fallback for now
"action_card": "metric_card.html", # Fallback for now
"kpi_card": "metric_card.html", # Fallback for now
"progress_card": "progress_card.html",
"case_card": "metric_card.html", # Fallback for now
"series_card": "metric_card.html", # Fallback for now
"article_card": "metric_card.html", # Fallback for now
"hero_section": "dashboard_layout.html",
"indexer_section": "dashboard_layout.html",
"interactor_section": "dashboard_layout.html",
"card_grid": "dashboard_layout.html",
"dashboard_layout": "dashboard_layout.html",
"dashboard": "dashboard_layout.html",
}
def render_card(self, card_data: Dict[str, Any]) -> str:
"""
Render a single card to HTML
"""
card_type = card_data.get("type", "default_card")
template_file = self.template_mapping.get(card_type, "metric_card.html")
try:
template = self.env.get_template(template_file)
return template.render(card=card_data)
except Exception as e:
# Fallback to basic template
return f'<div class="yaz-card p-4 border border-red-300 bg-red-50"><p class="text-red-700">Error rendering card: {str(e)}</p></div>'
def _render_card_recursive(self, card_data: Dict[str, Any]) -> str:
"""
Helper function for recursive card rendering in templates
"""
return self.render_card(card_data)
def render_layout(self, layout_data: Dict[str, Any]) -> str:
"""
Render a complete layout to HTML
"""
layout_type = layout_data.get("type", "card_grid")
template_file = self.template_mapping.get(layout_type, "dashboard_layout.html")
try:
template = self.env.get_template(template_file)
return template.render(dashboard=layout_data)
except Exception as e:
# Fallback rendering
return f'<div class="yaz-dashboard p-4 border border-red-300 bg-red-50"><p class="text-red-700">Error rendering layout: {str(e)}</p></div>'
def render_page(self, page_data: Dict[str, Any]) -> str:
"""
Render a complete page with base layout
"""
try:
template = self.env.get_template("base.html")
return template.render(page=page_data)
except Exception as e:
# Fallback page rendering
return f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YAZ Care Platform</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-50">
<div class="min-h-screen flex items-center justify-center">
<div class="bg-white p-8 rounded-lg shadow-md max-w-md w-full">
<h1 class="text-2xl font-bold text-gray-900 mb-4">YAZ Care Platform</h1>
<p class="text-red-600">Error rendering page: {str(e)}</p>
</div>
</div>
</body>
</html>
"""
def render_grid(self, cards: List[Dict[str, Any]], columns: int = 3) -> str:
"""
Render a grid of cards
"""
cards_html = ""
for card in cards:
cards_html += self.render_card(card)
return f"""
<div class="grid grid-cols-1 md:grid-cols-{columns} gap-6">
{cards_html}
</div>
"""
def get_available_templates(self) -> List[str]:
"""
Get list of available template names
"""
return list(self.template_mapping.keys())
def render_dashboard_section(self, section_data: Dict[str, Any]) -> str:
"""
Render a dashboard section with title and cards
"""
cards_html = ""
for card in section_data.get("cards", []):
cards_html += self.render_card(card)
columns = section_data.get("columns", 3)
title = section_data.get("title", "")
section_html = f"""
<div class="space-y-4">
{f'<h2 class="text-lg font-medium text-gray-900">{title}</h2>' if title else ''}
<div class="grid grid-cols-1 md:grid-cols-{columns} gap-6">
{cards_html}
</div>
</div>
"""
return section_html
def render_hero_section(self, hero_data: Dict[str, Any]) -> str:
"""
Render a hero section for landing pages
"""
stats_html = ""
if "stats" in hero_data:
for stat in hero_data["stats"]:
stat_card = {"type": "metric", **stat, "size": "small"}
stats_html += self.render_card(stat_card)
return f"""
<div class="bg-white shadow rounded-lg">
<div class="px-6 py-8">
<div class="text-center">
<h1 class="text-4xl font-bold text-gray-900">{hero_data.get("title", "YAZ Care Platform")}</h1>
<p class="mt-2 text-xl text-gray-600">{hero_data.get("subtitle", "")}</p>
{f'<p class="mt-4 text-gray-500">{hero_data.get("description", "")}</p>' if hero_data.get("description") else ''}
</div>
{f'<div class="mt-8 grid grid-cols-1 md:grid-cols-3 gap-4">{stats_html}</div>' if stats_html else ''}
<div class="mt-8 flex justify-center space-x-4">
{f'<a href="{hero_data["cta_primary"]["action"]}" class="inline-flex items-center px-6 py-3 border border-transparent text-base font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700">{hero_data["cta_primary"]["text"]}</a>' if hero_data.get("cta_primary") else ''}
{f'<a href="{hero_data["cta_secondary"]["action"]}" class="inline-flex items-center px-6 py-3 border border-gray-300 text-base font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50">{hero_data["cta_secondary"]["text"]}</a>' if hero_data.get("cta_secondary") else ''}
</div>
</div>
</div>
"""