import json from pathlib import Path from typing import Dict, List, Any, Tuple from collections import defaultdict class HTMLGenerator: """Generate beautiful HTML pages from UI element JSON data.""" # Confidence threshold for visual indication LOW_CONFIDENCE_THRESHOLD = 0.6 # Column thresholds for positioning (24-column grid, 0-indexed) LEFT_RIGHT_THRESHOLD = 12 # Center < 12 is left, >= 12 is right FULL_WIDTH_SPAN = 16 # Elements spanning > 16 columns are full-width def __init__(self, json_path: str): """Initialize with path to JSON file.""" self.json_path = Path(json_path) self.data = self._load_json() self.metadata = self.data.get('metadata', {}) self.grid_system = self.metadata.get('grid_system', {}) self.elements = self._parse_elements() def _load_json(self) -> Dict: """Load and parse JSON file.""" with open(self.json_path, 'r') as f: return json.load(f) def _parse_elements(self) -> List[Dict]: """Parse and sort elements by vertical position (top to bottom), then horizontal.""" elements = self.data.get('elements', []) # Sort by row position (top to bottom), then column (left to right) return sorted(elements, key=lambda x: ( x['grid_position']['start_row'], x['grid_position']['start_col'] )) def _is_low_confidence(self, element: Dict) -> bool: """Check if element has low confidence.""" return element.get('confidence', 1.0) < self.LOW_CONFIDENCE_THRESHOLD def _calculate_element_center_col(self, element: Dict) -> float: """Calculate the center column position of an element.""" grid_pos = element['grid_position'] return (grid_pos['start_col'] + grid_pos['end_col']) / 2 def _get_element_colspan(self, element: Dict) -> int: """Get the column span of an element.""" return element['grid_position']['colspan'] def _classify_element_position(self, element: Dict) -> str: """ Classify element as 'left', 'right', or 'center' based on position. Rules: - CENTER: spans > 16 columns OR spans across both halves significantly - LEFT: center column < 12 - RIGHT: center column >= 12 """ center_col = self._calculate_element_center_col(element) colspan = self._get_element_colspan(element) grid_pos = element['grid_position'] # Check if it's a full-width/center element if colspan > self.FULL_WIDTH_SPAN: return 'center' # Check if it spans across both halves (crosses the 11-12 boundary significantly) start_col = grid_pos['start_col'] end_col = grid_pos['end_col'] # If it starts in left half and ends in right half with significant overlap if start_col < self.LEFT_RIGHT_THRESHOLD and end_col >= self.LEFT_RIGHT_THRESHOLD: # If it spans at least 4 columns on each side, consider it center left_span = self.LEFT_RIGHT_THRESHOLD - start_col right_span = end_col - self.LEFT_RIGHT_THRESHOLD if left_span >= 4 and right_span >= 4: return 'center' # Otherwise, classify by center position if center_col < self.LEFT_RIGHT_THRESHOLD: return 'left' else: return 'right' def _group_elements_by_rows(self) -> List[List[Dict]]: """ Group elements into row groups based on vertical proximity. Elements in similar row ranges are grouped together. """ if not self.elements: return [] row_groups = [] current_group = [] last_end_row = -1 for element in self.elements: start_row = element['grid_position']['start_row'] end_row = element['grid_position']['end_row'] # If this element starts within or close to the last element's range, group it # Gap threshold: if there's more than 2 rows gap, start new group if current_group and start_row > last_end_row + 2: row_groups.append(current_group) current_group = [] current_group.append(element) last_end_row = max(last_end_row, end_row) # Add the last group if current_group: row_groups.append(current_group) return row_groups def _get_gradient_colors(self) -> tuple: """Return primary gradient colors.""" return ('#667eea', '#764ba2') def _generate_css(self) -> str: """Generate comprehensive CSS styling.""" color1, color2 = self._get_gradient_colors() return f"""* {{ margin: 0; padding: 0; box-sizing: border-box; }} body {{ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: linear-gradient(135deg, {color1} 0%, {color2} 100%); min-height: 100vh; padding: 20px; line-height: 1.6; }} .container {{ max-width: 1400px; margin: 0 auto; background: white; border-radius: 20px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); overflow: hidden; }} /* Low Confidence Indicator */ .low-confidence {{ position: relative; border: 2px dashed #ff9800 !important; background: repeating-linear-gradient( 45deg, transparent, transparent 10px, rgba(255, 152, 0, 0.05) 10px, rgba(255, 152, 0, 0.05) 20px ) !important; }} .low-confidence::before {{ content: "⚠ Low Confidence Detection"; position: absolute; top: -12px; left: 10px; background: #ff9800; color: white; padding: 2px 10px; font-size: 11px; border-radius: 10px; font-weight: 600; z-index: 10; }} .low-confidence-inline {{ border: 2px dashed #ff9800 !important; background-color: rgba(255, 152, 0, 0.08) !important; box-shadow: 0 0 10px rgba(255, 152, 0, 0.2) !important; }} /* Navbar Styling */ .navbar {{ background: linear-gradient(90deg, {color1} 0%, {color2} 100%); padding: 30px 40px; display: flex; justify-content: space-between; align-items: center; }} .navbar.low-confidence {{ background: linear-gradient(90deg, #ff9800 0%, #f57c00 100%); }} .navbar h1 {{ color: white; font-size: 28px; font-weight: 600; }} .nav-links {{ display: flex; gap: 30px; }} .nav-links a {{ color: white; text-decoration: none; font-size: 16px; transition: opacity 0.3s; }} .nav-links a:hover {{ opacity: 0.8; }} /* Row Section - wraps each row group */ .row-section {{ padding: 40px; }} .row-section:nth-child(even) {{ background: #f8f9fa; }} /* Two Column Layout */ .two-column-layout {{ display: grid; grid-template-columns: 1fr 1fr; gap: 40px; align-items: start; }} .column-left {{ display: flex; flex-direction: column; gap: 25px; }} .column-right {{ display: flex; flex-direction: column; gap: 25px; }} /* Full Width Layout */ .full-width-layout {{ display: flex; flex-direction: column; gap: 25px; }} /* Button Styling */ .button {{ background: linear-gradient(135deg, {color1} 0%, {color2} 100%); color: white; border: none; padding: 18px 36px; font-size: 16px; font-weight: 600; border-radius: 12px; cursor: pointer; transition: all 0.3s; box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4); align-self: flex-start; }} .button:hover {{ transform: translateY(-2px); box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6); }} .button:active {{ transform: translateY(0); }} /* Checkbox Styling */ .checkbox-group {{ background: #f8f9fa; padding: 25px; border-radius: 12px; border: 2px solid #e0e0e0; }} .checkbox-item {{ display: flex; align-items: center; gap: 15px; margin-bottom: 15px; }} .checkbox-item:last-child {{ margin-bottom: 0; }} input[type="checkbox"] {{ width: 24px; height: 24px; cursor: pointer; accent-color: {color1}; }} .checkbox-item label {{ font-size: 16px; color: #333; cursor: pointer; }} /* Image Styling */ .image-container {{ background: linear-gradient(135deg, {color1}22 0%, {color2}33 100%); border-radius: 12px; padding: 20px; display: flex; align-items: center; justify-content: center; min-height: 250px; border: 2px dashed {color1}; }} .image-placeholder {{ text-align: center; color: {color1}; }} .image-placeholder svg {{ width: 100px; height: 100px; margin-bottom: 20px; opacity: 0.6; }} .image-placeholder p {{ font-size: 18px; font-weight: 500; }} /* Text Styling */ .text-block {{ background: white; padding: 25px; border-radius: 12px; border-left: 4px solid {color1}; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); }} .text-block h3 {{ color: #333; margin-bottom: 10px; font-size: 20px; }} .text-block p {{ color: #666; line-height: 1.6; font-size: 15px; }} /* Textfield Styling */ .textfield {{ width: 100%; padding: 18px 24px; font-size: 16px; border: 2px solid #e0e0e0; border-radius: 12px; transition: all 0.3s; }} .textfield:focus {{ outline: none; border-color: {color1}; box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1); }} /* Paragraph Styling */ .paragraph {{ color: #555; line-height: 1.8; font-size: 16px; padding: 25px; background: white; border-radius: 12px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); }} .paragraph h2 {{ color: #333; margin-bottom: 20px; font-size: 26px; border-bottom: 3px solid {color1}; padding-bottom: 15px; }} .paragraph p {{ margin-bottom: 15px; }} /* Responsive Design */ @media (max-width: 1024px) {{ .two-column-layout {{ grid-template-columns: 1fr; }} }} @media (max-width: 768px) {{ .navbar {{ flex-direction: column; gap: 20px; text-align: center; }} .nav-links {{ flex-direction: column; gap: 15px; }} .row-section {{ padding: 20px; }} .two-column-layout {{ gap: 20px; }} }}""" def _generate_navbar_html(self, element: Dict) -> str: """Generate navbar HTML.""" confidence_class = ' low-confidence' if self._is_low_confidence(element) else '' confidence = element.get('confidence', 1.0) return f""" """ def _generate_button_html(self, element: Dict) -> str: """Generate button HTML.""" confidence_class = ' low-confidence-inline' if self._is_low_confidence(element) else '' return f'' def _generate_checkbox_html(self, element: Dict) -> str: """Generate checkbox group HTML.""" confidence_class = ' low-confidence' if self._is_low_confidence(element) else '' return f"""
Image Placeholder
Use the search bar above to quickly find what you're looking for. All features are just a click away!
This modern interface brings together all the tools you need in one beautiful, cohesive design. Our goal is to make your workflow as smooth and efficient as possible.
With intuitive navigation and powerful features at your fingertips, you can focus on what matters most. Whether you're managing projects, analyzing data, or collaborating with your team, everything is designed to work seamlessly together.
The responsive layout adapts to any screen size, ensuring you have a consistent experience across all your devices. Customize your settings using the options on the left to make this space truly yours.
We've carefully crafted every element to provide both beauty and functionality. The clean design reduces clutter while maintaining all the power you need to be productive.
Start exploring the features available to you, and discover how this platform can transform the way you work. If you need any help, our comprehensive documentation and support team are always available.