Spaces:
Sleeping
Sleeping
Mallari-Merick commited on
Commit ·
be8f1e2
1
Parent(s): 44a2a72
Included the Coding Module and updated the app.py
Browse files- CodingModule.py +500 -0
- app.py +56 -17
- requirements.txt +1 -0
CodingModule.py
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from typing import Dict, List, Any
|
| 4 |
+
|
| 5 |
+
class HTMLGenerator:
|
| 6 |
+
"""Generate beautiful HTML pages from UI element JSON data."""
|
| 7 |
+
|
| 8 |
+
def __init__(self, json_path: str):
|
| 9 |
+
"""Initialize with path to JSON file."""
|
| 10 |
+
self.json_path = Path(json_path)
|
| 11 |
+
self.data = self._load_json()
|
| 12 |
+
self.elements = self._parse_elements()
|
| 13 |
+
|
| 14 |
+
def _load_json(self) -> Dict:
|
| 15 |
+
"""Load and parse JSON file."""
|
| 16 |
+
with open(self.json_path, 'r') as f:
|
| 17 |
+
return json.load(f)
|
| 18 |
+
|
| 19 |
+
def _parse_elements(self) -> List[Dict]:
|
| 20 |
+
"""Parse and sort elements by position."""
|
| 21 |
+
elements = self.data.get('elements', [])
|
| 22 |
+
# Sort by row position, then column
|
| 23 |
+
return sorted(elements, key=lambda x: (
|
| 24 |
+
x['grid_position']['start_row'],
|
| 25 |
+
x['grid_position']['start_col']
|
| 26 |
+
))
|
| 27 |
+
|
| 28 |
+
def _get_gradient_colors(self) -> tuple:
|
| 29 |
+
"""Return primary gradient colors."""
|
| 30 |
+
return ('#667eea', '#764ba2')
|
| 31 |
+
|
| 32 |
+
def _generate_css(self) -> str:
|
| 33 |
+
"""Generate comprehensive CSS styling."""
|
| 34 |
+
color1, color2 = self._get_gradient_colors()
|
| 35 |
+
|
| 36 |
+
return f"""* {{
|
| 37 |
+
margin: 0;
|
| 38 |
+
padding: 0;
|
| 39 |
+
box-sizing: border-box;
|
| 40 |
+
}}
|
| 41 |
+
|
| 42 |
+
body {{
|
| 43 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 44 |
+
background: linear-gradient(135deg, {color1} 0%, {color2} 100%);
|
| 45 |
+
min-height: 100vh;
|
| 46 |
+
padding: 20px;
|
| 47 |
+
line-height: 1.6;
|
| 48 |
+
}}
|
| 49 |
+
|
| 50 |
+
.container {{
|
| 51 |
+
max-width: 1400px;
|
| 52 |
+
margin: 0 auto;
|
| 53 |
+
background: white;
|
| 54 |
+
border-radius: 20px;
|
| 55 |
+
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
| 56 |
+
overflow: hidden;
|
| 57 |
+
}}
|
| 58 |
+
|
| 59 |
+
/* Navbar Styling */
|
| 60 |
+
.navbar {{
|
| 61 |
+
background: linear-gradient(90deg, {color1} 0%, {color2} 100%);
|
| 62 |
+
padding: 30px 40px;
|
| 63 |
+
display: flex;
|
| 64 |
+
justify-content: space-between;
|
| 65 |
+
align-items: center;
|
| 66 |
+
}}
|
| 67 |
+
|
| 68 |
+
.navbar h1 {{
|
| 69 |
+
color: white;
|
| 70 |
+
font-size: 28px;
|
| 71 |
+
font-weight: 600;
|
| 72 |
+
}}
|
| 73 |
+
|
| 74 |
+
.nav-links {{
|
| 75 |
+
display: flex;
|
| 76 |
+
gap: 30px;
|
| 77 |
+
}}
|
| 78 |
+
|
| 79 |
+
.nav-links a {{
|
| 80 |
+
color: white;
|
| 81 |
+
text-decoration: none;
|
| 82 |
+
font-size: 16px;
|
| 83 |
+
transition: opacity 0.3s;
|
| 84 |
+
}}
|
| 85 |
+
|
| 86 |
+
.nav-links a:hover {{
|
| 87 |
+
opacity: 0.8;
|
| 88 |
+
}}
|
| 89 |
+
|
| 90 |
+
/* Section Styling */
|
| 91 |
+
.section {{
|
| 92 |
+
padding: 30px 40px;
|
| 93 |
+
}}
|
| 94 |
+
|
| 95 |
+
.section.light {{
|
| 96 |
+
background: #f8f9fa;
|
| 97 |
+
}}
|
| 98 |
+
|
| 99 |
+
/* Main Content Grid */
|
| 100 |
+
.main-content {{
|
| 101 |
+
display: grid;
|
| 102 |
+
grid-template-columns: 1fr 1fr;
|
| 103 |
+
gap: 40px;
|
| 104 |
+
padding: 40px;
|
| 105 |
+
}}
|
| 106 |
+
|
| 107 |
+
.left-section {{
|
| 108 |
+
display: flex;
|
| 109 |
+
flex-direction: column;
|
| 110 |
+
gap: 30px;
|
| 111 |
+
}}
|
| 112 |
+
|
| 113 |
+
.right-section {{
|
| 114 |
+
background: #f8f9fa;
|
| 115 |
+
padding: 35px;
|
| 116 |
+
border-radius: 12px;
|
| 117 |
+
border: 2px solid #e0e0e0;
|
| 118 |
+
}}
|
| 119 |
+
|
| 120 |
+
/* Button Styling */
|
| 121 |
+
.button {{
|
| 122 |
+
background: linear-gradient(135deg, {color1} 0%, {color2} 100%);
|
| 123 |
+
color: white;
|
| 124 |
+
border: none;
|
| 125 |
+
padding: 18px 36px;
|
| 126 |
+
font-size: 16px;
|
| 127 |
+
font-weight: 600;
|
| 128 |
+
border-radius: 12px;
|
| 129 |
+
cursor: pointer;
|
| 130 |
+
transition: all 0.3s;
|
| 131 |
+
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
|
| 132 |
+
align-self: flex-start;
|
| 133 |
+
}}
|
| 134 |
+
|
| 135 |
+
.button:hover {{
|
| 136 |
+
transform: translateY(-2px);
|
| 137 |
+
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
|
| 138 |
+
}}
|
| 139 |
+
|
| 140 |
+
.button:active {{
|
| 141 |
+
transform: translateY(0);
|
| 142 |
+
}}
|
| 143 |
+
|
| 144 |
+
/* Checkbox Styling */
|
| 145 |
+
.checkbox-group {{
|
| 146 |
+
background: #f8f9fa;
|
| 147 |
+
padding: 25px;
|
| 148 |
+
border-radius: 12px;
|
| 149 |
+
border: 2px solid #e0e0e0;
|
| 150 |
+
}}
|
| 151 |
+
|
| 152 |
+
.checkbox-item {{
|
| 153 |
+
display: flex;
|
| 154 |
+
align-items: center;
|
| 155 |
+
gap: 15px;
|
| 156 |
+
margin-bottom: 15px;
|
| 157 |
+
}}
|
| 158 |
+
|
| 159 |
+
.checkbox-item:last-child {{
|
| 160 |
+
margin-bottom: 0;
|
| 161 |
+
}}
|
| 162 |
+
|
| 163 |
+
input[type="checkbox"] {{
|
| 164 |
+
width: 24px;
|
| 165 |
+
height: 24px;
|
| 166 |
+
cursor: pointer;
|
| 167 |
+
accent-color: {color1};
|
| 168 |
+
}}
|
| 169 |
+
|
| 170 |
+
.checkbox-item label {{
|
| 171 |
+
font-size: 16px;
|
| 172 |
+
color: #333;
|
| 173 |
+
cursor: pointer;
|
| 174 |
+
}}
|
| 175 |
+
|
| 176 |
+
/* Image Styling */
|
| 177 |
+
.image-container {{
|
| 178 |
+
background: linear-gradient(135deg, {color1}22 0%, {color2}33 100%);
|
| 179 |
+
border-radius: 12px;
|
| 180 |
+
padding: 20px;
|
| 181 |
+
display: flex;
|
| 182 |
+
align-items: center;
|
| 183 |
+
justify-content: center;
|
| 184 |
+
min-height: 300px;
|
| 185 |
+
border: 2px dashed {color1};
|
| 186 |
+
}}
|
| 187 |
+
|
| 188 |
+
.image-placeholder {{
|
| 189 |
+
text-align: center;
|
| 190 |
+
color: {color1};
|
| 191 |
+
}}
|
| 192 |
+
|
| 193 |
+
.image-placeholder svg {{
|
| 194 |
+
width: 100px;
|
| 195 |
+
height: 100px;
|
| 196 |
+
margin-bottom: 20px;
|
| 197 |
+
opacity: 0.6;
|
| 198 |
+
}}
|
| 199 |
+
|
| 200 |
+
.image-placeholder p {{
|
| 201 |
+
font-size: 18px;
|
| 202 |
+
font-weight: 500;
|
| 203 |
+
}}
|
| 204 |
+
|
| 205 |
+
/* Text Styling */
|
| 206 |
+
.text-block {{
|
| 207 |
+
background: white;
|
| 208 |
+
padding: 25px;
|
| 209 |
+
border-radius: 12px;
|
| 210 |
+
border-left: 4px solid {color1};
|
| 211 |
+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
| 212 |
+
}}
|
| 213 |
+
|
| 214 |
+
.text-block h3 {{
|
| 215 |
+
color: #333;
|
| 216 |
+
margin-bottom: 10px;
|
| 217 |
+
font-size: 20px;
|
| 218 |
+
}}
|
| 219 |
+
|
| 220 |
+
.text-block p {{
|
| 221 |
+
color: #666;
|
| 222 |
+
line-height: 1.6;
|
| 223 |
+
font-size: 15px;
|
| 224 |
+
}}
|
| 225 |
+
|
| 226 |
+
/* Textfield Styling */
|
| 227 |
+
.textfield {{
|
| 228 |
+
width: 100%;
|
| 229 |
+
padding: 18px 24px;
|
| 230 |
+
font-size: 16px;
|
| 231 |
+
border: 2px solid #e0e0e0;
|
| 232 |
+
border-radius: 12px;
|
| 233 |
+
transition: all 0.3s;
|
| 234 |
+
}}
|
| 235 |
+
|
| 236 |
+
.textfield:focus {{
|
| 237 |
+
outline: none;
|
| 238 |
+
border-color: {color1};
|
| 239 |
+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
|
| 240 |
+
}}
|
| 241 |
+
|
| 242 |
+
/* Paragraph Styling */
|
| 243 |
+
.paragraph {{
|
| 244 |
+
color: #555;
|
| 245 |
+
line-height: 1.8;
|
| 246 |
+
font-size: 16px;
|
| 247 |
+
margin-bottom: 15px;
|
| 248 |
+
}}
|
| 249 |
+
|
| 250 |
+
.paragraph h2 {{
|
| 251 |
+
color: #333;
|
| 252 |
+
margin-bottom: 20px;
|
| 253 |
+
font-size: 26px;
|
| 254 |
+
border-bottom: 3px solid {color1};
|
| 255 |
+
padding-bottom: 15px;
|
| 256 |
+
}}
|
| 257 |
+
|
| 258 |
+
/* Responsive Design */
|
| 259 |
+
@media (max-width: 1024px) {{
|
| 260 |
+
.main-content {{
|
| 261 |
+
grid-template-columns: 1fr;
|
| 262 |
+
}}
|
| 263 |
+
}}
|
| 264 |
+
|
| 265 |
+
@media (max-width: 768px) {{
|
| 266 |
+
.navbar {{
|
| 267 |
+
flex-direction: column;
|
| 268 |
+
gap: 20px;
|
| 269 |
+
text-align: center;
|
| 270 |
+
}}
|
| 271 |
+
|
| 272 |
+
.nav-links {{
|
| 273 |
+
flex-direction: column;
|
| 274 |
+
gap: 15px;
|
| 275 |
+
}}
|
| 276 |
+
|
| 277 |
+
.section {{
|
| 278 |
+
padding: 20px;
|
| 279 |
+
}}
|
| 280 |
+
|
| 281 |
+
.main-content {{
|
| 282 |
+
padding: 20px;
|
| 283 |
+
gap: 20px;
|
| 284 |
+
}}
|
| 285 |
+
}}"""
|
| 286 |
+
|
| 287 |
+
def _generate_navbar_html(self, element: Dict) -> str:
|
| 288 |
+
"""Generate navbar HTML."""
|
| 289 |
+
return """<nav class="navbar">
|
| 290 |
+
<h1>Modern Dashboard</h1>
|
| 291 |
+
<div class="nav-links">
|
| 292 |
+
<a href="#home">Home</a>
|
| 293 |
+
<a href="#features">Features</a>
|
| 294 |
+
<a href="#about">About</a>
|
| 295 |
+
<a href="#contact">Contact</a>
|
| 296 |
+
</div>
|
| 297 |
+
</nav>"""
|
| 298 |
+
|
| 299 |
+
def _generate_button_html(self, element: Dict) -> str:
|
| 300 |
+
"""Generate button HTML."""
|
| 301 |
+
return f'<button class="button">Get Started</button>'
|
| 302 |
+
|
| 303 |
+
def _generate_checkbox_html(self, element: Dict) -> str:
|
| 304 |
+
"""Generate checkbox group HTML."""
|
| 305 |
+
return """<div class="checkbox-group">
|
| 306 |
+
<div class="checkbox-item">
|
| 307 |
+
<input type="checkbox" id="option1" checked>
|
| 308 |
+
<label for="option1">Enable notifications</label>
|
| 309 |
+
</div>
|
| 310 |
+
<div class="checkbox-item">
|
| 311 |
+
<input type="checkbox" id="option2">
|
| 312 |
+
<label for="option2">Auto-save changes</label>
|
| 313 |
+
</div>
|
| 314 |
+
<div class="checkbox-item">
|
| 315 |
+
<input type="checkbox" id="option3" checked>
|
| 316 |
+
<label for="option3">Dark mode</label>
|
| 317 |
+
</div>
|
| 318 |
+
</div>"""
|
| 319 |
+
|
| 320 |
+
def _generate_image_html(self, element: Dict) -> str:
|
| 321 |
+
"""Generate image placeholder HTML."""
|
| 322 |
+
return """<div class="image-container">
|
| 323 |
+
<div class="image-placeholder">
|
| 324 |
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
| 325 |
+
<rect x="3" y="3" width="18" height="18" rx="2" ry="2"/>
|
| 326 |
+
<circle cx="8.5" cy="8.5" r="1.5"/>
|
| 327 |
+
<polyline points="21 15 16 10 5 21"/>
|
| 328 |
+
</svg>
|
| 329 |
+
<p>Image Placeholder</p>
|
| 330 |
+
</div>
|
| 331 |
+
</div>"""
|
| 332 |
+
|
| 333 |
+
def _generate_text_html(self, element: Dict) -> str:
|
| 334 |
+
"""Generate text block HTML."""
|
| 335 |
+
return """<div class="text-block">
|
| 336 |
+
<h3>Quick Tip</h3>
|
| 337 |
+
<p>Use the search bar above to quickly find what you're looking for. All features are just a click away!</p>
|
| 338 |
+
</div>"""
|
| 339 |
+
|
| 340 |
+
def _generate_textfield_html(self, element: Dict) -> str:
|
| 341 |
+
"""Generate textfield HTML."""
|
| 342 |
+
return '<input type="text" class="textfield" placeholder="Search for anything...">'
|
| 343 |
+
|
| 344 |
+
def _generate_paragraph_html(self, element: Dict) -> str:
|
| 345 |
+
"""Generate paragraph HTML."""
|
| 346 |
+
return """<div class="paragraph">
|
| 347 |
+
<h2>Welcome to Your Dashboard</h2>
|
| 348 |
+
<p>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.</p>
|
| 349 |
+
<p>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.</p>
|
| 350 |
+
<p>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.</p>
|
| 351 |
+
<p>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.</p>
|
| 352 |
+
<p>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.</p>
|
| 353 |
+
</div>"""
|
| 354 |
+
|
| 355 |
+
def _generate_element_html(self, element: Dict) -> str:
|
| 356 |
+
"""Generate HTML for a single element based on its type."""
|
| 357 |
+
element_type = element['type'].lower()
|
| 358 |
+
|
| 359 |
+
generators = {
|
| 360 |
+
'navbar': self._generate_navbar_html,
|
| 361 |
+
'button': self._generate_button_html,
|
| 362 |
+
'checkbox': self._generate_checkbox_html,
|
| 363 |
+
'image': self._generate_image_html,
|
| 364 |
+
'text': self._generate_text_html,
|
| 365 |
+
'textfield': self._generate_textfield_html,
|
| 366 |
+
'paragraph': self._generate_paragraph_html
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
generator = generators.get(element_type)
|
| 370 |
+
if generator:
|
| 371 |
+
return generator(element)
|
| 372 |
+
|
| 373 |
+
# Default fallback
|
| 374 |
+
return f'<div class="{element_type}">Element: {element_type}</div>'
|
| 375 |
+
|
| 376 |
+
def _organize_layout(self) -> Dict[str, List[Dict]]:
|
| 377 |
+
"""Organize elements into layout sections."""
|
| 378 |
+
layout = {
|
| 379 |
+
'header': [],
|
| 380 |
+
'search': [],
|
| 381 |
+
'left': [],
|
| 382 |
+
'right': []
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
for element in self.elements:
|
| 386 |
+
elem_type = element['type'].lower()
|
| 387 |
+
row_start = element['grid_position']['start_row']
|
| 388 |
+
|
| 389 |
+
if elem_type == 'navbar':
|
| 390 |
+
layout['header'].append(element)
|
| 391 |
+
elif elem_type == 'textfield' and row_start < 7:
|
| 392 |
+
layout['search'].append(element)
|
| 393 |
+
elif elem_type == 'paragraph':
|
| 394 |
+
layout['right'].append(element)
|
| 395 |
+
else:
|
| 396 |
+
layout['left'].append(element)
|
| 397 |
+
|
| 398 |
+
return layout
|
| 399 |
+
|
| 400 |
+
def _generate_body_html(self) -> str:
|
| 401 |
+
"""Generate the body HTML content."""
|
| 402 |
+
layout = self._organize_layout()
|
| 403 |
+
|
| 404 |
+
html_parts = ['<div class="container">']
|
| 405 |
+
|
| 406 |
+
# Header section
|
| 407 |
+
for element in layout['header']:
|
| 408 |
+
html_parts.append(self._generate_element_html(element))
|
| 409 |
+
|
| 410 |
+
# Search section
|
| 411 |
+
if layout['search']:
|
| 412 |
+
html_parts.append('<div class="section light">')
|
| 413 |
+
for element in layout['search']:
|
| 414 |
+
html_parts.append(self._generate_element_html(element))
|
| 415 |
+
html_parts.append('</div>')
|
| 416 |
+
|
| 417 |
+
# Main content with left and right sections
|
| 418 |
+
html_parts.append('<div class="main-content">')
|
| 419 |
+
|
| 420 |
+
# Left section
|
| 421 |
+
html_parts.append('<div class="left-section">')
|
| 422 |
+
for element in layout['left']:
|
| 423 |
+
html_parts.append(self._generate_element_html(element))
|
| 424 |
+
html_parts.append('</div>')
|
| 425 |
+
|
| 426 |
+
# Right section
|
| 427 |
+
html_parts.append('<div class="right-section">')
|
| 428 |
+
for element in layout['right']:
|
| 429 |
+
html_parts.append(self._generate_element_html(element))
|
| 430 |
+
html_parts.append('</div>')
|
| 431 |
+
|
| 432 |
+
html_parts.append('</div>') # Close main-content
|
| 433 |
+
html_parts.append('</div>') # Close container
|
| 434 |
+
|
| 435 |
+
return '\n'.join(html_parts)
|
| 436 |
+
|
| 437 |
+
def generate_html(self, output_path: str = 'output.html'):
|
| 438 |
+
"""Generate complete HTML file."""
|
| 439 |
+
css = self._generate_css()
|
| 440 |
+
body = self._generate_body_html()
|
| 441 |
+
|
| 442 |
+
html_template = f"""<!DOCTYPE html>
|
| 443 |
+
<html lang="en">
|
| 444 |
+
<head>
|
| 445 |
+
<meta charset="UTF-8">
|
| 446 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 447 |
+
<title>Generated UI Layout</title>
|
| 448 |
+
<style>
|
| 449 |
+
{css}
|
| 450 |
+
</style>
|
| 451 |
+
</head>
|
| 452 |
+
<body>
|
| 453 |
+
{body}
|
| 454 |
+
</body>
|
| 455 |
+
</html>"""
|
| 456 |
+
|
| 457 |
+
# Write to file
|
| 458 |
+
output_file = Path(output_path)
|
| 459 |
+
output_file.write_text(html_template, encoding='utf-8')
|
| 460 |
+
|
| 461 |
+
print(f"✓ HTML file generated successfully: {output_file.absolute()}")
|
| 462 |
+
print(f"✓ Total elements processed: {len(self.elements)}")
|
| 463 |
+
print(f"✓ Element types: {', '.join(set(e['type'] for e in self.elements))}")
|
| 464 |
+
|
| 465 |
+
return html_template
|
| 466 |
+
|
| 467 |
+
|
| 468 |
+
def main():
|
| 469 |
+
"""Main function to run the generator."""
|
| 470 |
+
import sys
|
| 471 |
+
|
| 472 |
+
# Check if JSON file path is provided
|
| 473 |
+
if len(sys.argv) < 2:
|
| 474 |
+
print("Usage: python script.py <json_file_path> [output_html_path]")
|
| 475 |
+
print("Example: python script.py sample_output.json output.html")
|
| 476 |
+
sys.exit(1)
|
| 477 |
+
|
| 478 |
+
json_path = sys.argv[1]
|
| 479 |
+
output_path = sys.argv[2] if len(sys.argv) > 2 else 'output.html'
|
| 480 |
+
|
| 481 |
+
# Validate JSON file exists
|
| 482 |
+
if not Path(json_path).exists():
|
| 483 |
+
print(f"Error: JSON file not found: {json_path}")
|
| 484 |
+
sys.exit(1)
|
| 485 |
+
|
| 486 |
+
try:
|
| 487 |
+
# Generate HTML
|
| 488 |
+
generator = HTMLGenerator(json_path)
|
| 489 |
+
generator.generate_html(output_path)
|
| 490 |
+
|
| 491 |
+
except json.JSONDecodeError as e:
|
| 492 |
+
print(f"Error: Invalid JSON file - {e}")
|
| 493 |
+
sys.exit(1)
|
| 494 |
+
except Exception as e:
|
| 495 |
+
print(f"Error: {e}")
|
| 496 |
+
sys.exit(1)
|
| 497 |
+
|
| 498 |
+
|
| 499 |
+
if __name__ == "__main__":
|
| 500 |
+
main()
|
app.py
CHANGED
|
@@ -66,6 +66,13 @@ async def process_wireframe_api(image: UploadFile = File(...)):
|
|
| 66 |
if os.path.exists(temp_path):
|
| 67 |
os.remove(temp_path)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# -----------------------------------------------------------------------------
|
| 71 |
# GRADIO (for Hugging Face UI)
|
|
@@ -75,8 +82,14 @@ def gradio_process(image):
|
|
| 75 |
Gradio passes a PIL Image.
|
| 76 |
We save it temporarily and reuse the SAME pipeline.
|
| 77 |
"""
|
|
|
|
|
|
|
| 78 |
temp_path = f"{TEMP_DIR}/{uuid.uuid4()}.png"
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
try:
|
| 82 |
results = process_wireframe(
|
|
@@ -87,35 +100,61 @@ def gradio_process(image):
|
|
| 87 |
)
|
| 88 |
|
| 89 |
if not results:
|
| 90 |
-
return "No elements detected", None
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
traceback.print_exc()
|
| 99 |
-
return f"Error: {str(e)}", None
|
| 100 |
|
| 101 |
finally:
|
| 102 |
if os.path.exists(temp_path):
|
| 103 |
os.remove(temp_path)
|
| 104 |
|
| 105 |
|
| 106 |
-
demo = gr.Interface(
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# -----------------------------------------------------------------------------
|
| 118 |
# ENTRY POINT (THIS IS IMPORTANT)
|
| 119 |
# -----------------------------------------------------------------------------
|
| 120 |
app = gr.mount_gradio_app(api, demo, path="/")
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if os.path.exists(temp_path):
|
| 67 |
os.remove(temp_path)
|
| 68 |
|
| 69 |
+
#Endpoint to serve generated HTML files
|
| 70 |
+
@api.get("/view-html/{file_id}")
|
| 71 |
+
async def view_html(file_id: str):
|
| 72 |
+
html_path = os.path.join(OUTPUT_DIR, f"{file_id}.html")
|
| 73 |
+
if os.path.exists(html_path):
|
| 74 |
+
return FileResponse(html_path, media_type="text/html")
|
| 75 |
+
return JSONResponse(status_code=404, content={"error": "File not found"})
|
| 76 |
|
| 77 |
# -----------------------------------------------------------------------------
|
| 78 |
# GRADIO (for Hugging Face UI)
|
|
|
|
| 82 |
Gradio passes a PIL Image.
|
| 83 |
We save it temporarily and reuse the SAME pipeline.
|
| 84 |
"""
|
| 85 |
+
if image is none:
|
| 86 |
+
return "Please upload an image", None, None
|
| 87 |
temp_path = f"{TEMP_DIR}/{uuid.uuid4()}.png"
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
image.save(temp_path)
|
| 91 |
+
except Exception as e:
|
| 92 |
+
return f"Error saving image: {str(e)}", None, None
|
| 93 |
|
| 94 |
try:
|
| 95 |
results = process_wireframe(
|
|
|
|
| 100 |
)
|
| 101 |
|
| 102 |
if not results:
|
| 103 |
+
return "No elements detected", None, None
|
| 104 |
|
| 105 |
+
status_msg = f"Successfully detected {len(results[['normalized_elements']])} elements"
|
| 106 |
+
json_path = results.get("json_path")
|
| 107 |
+
html_path = results.get("html_path")
|
| 108 |
+
|
| 109 |
+
return status_msg, json_path, html_path
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
traceback.print_exc()
|
| 113 |
+
return f"Error: {str(e)}", None, None
|
| 114 |
|
| 115 |
finally:
|
| 116 |
if os.path.exists(temp_path):
|
| 117 |
os.remove(temp_path)
|
| 118 |
|
| 119 |
|
| 120 |
+
# demo = gr.Interface(
|
| 121 |
+
# fn=gradio_process,
|
| 122 |
+
# inputs=gr.Image(type="pil", label="Upload Wireframe"),
|
| 123 |
+
# outputs=[
|
| 124 |
+
# gr.Textbox(label="Status"),
|
| 125 |
+
# gr.File(label="Normalized JSON Output")
|
| 126 |
+
# ],
|
| 127 |
+
# title="Wireframe Layout Normalizer",
|
| 128 |
+
# description="Upload a wireframe image to extract and normalize UI layout"
|
| 129 |
+
# )
|
| 130 |
+
|
| 131 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 132 |
+
gr.Markdown("# Wireframe Layout Normalizer")
|
| 133 |
+
gr.Markdown("Upload a wireframe image to extract and normalize UI layout elements")
|
| 134 |
+
|
| 135 |
+
with gr.Row():
|
| 136 |
+
with gr.Row():
|
| 137 |
+
image_input = gr.Image(type="pil", label="Upload Wireframe Image")
|
| 138 |
+
process_btn = gr.Button("Process Wireframe", variant="primary")
|
| 139 |
+
|
| 140 |
+
with gr.Column():
|
| 141 |
+
status_output = gr.Textbox(label="Status", lines=2)
|
| 142 |
+
json_output = gr.File(label="Normalized JSON Output")
|
| 143 |
+
html_output = gr.File(label="Generated HTML Output")
|
| 144 |
+
|
| 145 |
+
process_btn.click(
|
| 146 |
+
fn=gradio_process,
|
| 147 |
+
inputs=image_input,
|
| 148 |
+
outputs=[status_output, json_output, html_output]
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
|
| 152 |
# -----------------------------------------------------------------------------
|
| 153 |
# ENTRY POINT (THIS IS IMPORTANT)
|
| 154 |
# -----------------------------------------------------------------------------
|
| 155 |
app = gr.mount_gradio_app(api, demo, path="/")
|
| 156 |
|
| 157 |
+
if __name__ == "__main__":
|
| 158 |
+
import uvicorn
|
| 159 |
+
# For local testing
|
| 160 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -6,3 +6,4 @@ numpy
|
|
| 6 |
pillow
|
| 7 |
matplotlib
|
| 8 |
opencv-python-headless
|
|
|
|
|
|
| 6 |
pillow
|
| 7 |
matplotlib
|
| 8 |
opencv-python-headless
|
| 9 |
+
python-multipart
|