Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
bootstrap_converter.py · 9.9 KB · 269 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | DBBasic Bootstrap Converter - Convert data structures to Bootstrap HTML |
| 4 | This follows DBBasic's config-driven philosophy |
| 5 | """ |
| 6 | |
| 7 | from typing import Dict, List, Any, Union |
| 8 | |
| 9 | class BootstrapConverter: |
| 10 | """Convert Python/YAML data structures to Bootstrap 5.3 HTML""" |
| 11 | |
| 12 | @staticmethod |
| 13 | def convert(data: Union[Dict, List, str]) -> str: |
| 14 | """Main entry point - converts any data structure to HTML""" |
| 15 | |
| 16 | if isinstance(data, str): |
| 17 | return data |
| 18 | |
| 19 | if isinstance(data, list): |
| 20 | return ''.join(BootstrapConverter.convert(item) for item in data) |
| 21 | |
| 22 | if isinstance(data, dict): |
| 23 | # Check for component type |
| 24 | component_type = data.get('type', '') |
| 25 | |
| 26 | if component_type == 'page': |
| 27 | return BootstrapConverter._page(data) |
| 28 | elif component_type == 'navbar': |
| 29 | return BootstrapConverter._navbar(data) |
| 30 | elif component_type == 'hero': |
| 31 | return BootstrapConverter._hero(data) |
| 32 | elif component_type == 'grid': |
| 33 | return BootstrapConverter._grid(data) |
| 34 | elif component_type == 'card': |
| 35 | return BootstrapConverter._card(data) |
| 36 | elif component_type == 'button': |
| 37 | return BootstrapConverter._button(data) |
| 38 | elif component_type == 'container': |
| 39 | return BootstrapConverter._container(data) |
| 40 | elif component_type == 'alert': |
| 41 | return BootstrapConverter._alert(data) |
| 42 | else: |
| 43 | # Try to infer from structure |
| 44 | if 'components' in data: |
| 45 | return BootstrapConverter.convert(data['components']) |
| 46 | elif 'items' in data: |
| 47 | return BootstrapConverter.convert(data['items']) |
| 48 | else: |
| 49 | return str(data) |
| 50 | |
| 51 | return str(data) |
| 52 | |
| 53 | @staticmethod |
| 54 | def _page(data: Dict) -> str: |
| 55 | """Generate complete HTML page""" |
| 56 | title = data.get('title', 'DBBasic') |
| 57 | components = BootstrapConverter.convert(data.get('components', [])) |
| 58 | |
| 59 | return f"""<!DOCTYPE html> |
| 60 | <html lang="en"> |
| 61 | <head> |
| 62 | <meta charset="UTF-8"> |
| 63 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 64 | <title>{title}</title> |
| 65 | <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"> |
| 66 | <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css"> |
| 67 | <style> |
| 68 | .card {{ transition: transform 0.3s; }} |
| 69 | .card:hover {{ transform: translateY(-5px); }} |
| 70 | .hero {{ padding: 3rem 0; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; }} |
| 71 | </style> |
| 72 | </head> |
| 73 | <body> |
| 74 | {components} |
| 75 | <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script> |
| 76 | <script> |
| 77 | function deployTemplate(id) {{ |
| 78 | alert('Deploying template: ' + id); |
| 79 | }} |
| 80 | function previewTemplate(id) {{ |
| 81 | alert('Preview template: ' + id); |
| 82 | }} |
| 83 | </script> |
| 84 | </body> |
| 85 | </html>""" |
| 86 | |
| 87 | @staticmethod |
| 88 | def _navbar(data: Dict) -> str: |
| 89 | """Generate Bootstrap navbar""" |
| 90 | brand = data.get('brand', 'DBBasic') |
| 91 | links = data.get('links', []) |
| 92 | |
| 93 | nav_links = [] |
| 94 | for link in links: |
| 95 | if isinstance(link, str): |
| 96 | # Simple string becomes link with same name |
| 97 | nav_links.append(f'<a class="nav-link" href="/{link.lower()}">{link}</a>') |
| 98 | elif isinstance(link, dict): |
| 99 | nav_links.append(f'<a class="nav-link" href="{link["url"]}">{link["text"]}</a>') |
| 100 | |
| 101 | return f""" |
| 102 | <nav class="navbar navbar-expand-lg navbar-light bg-light"> |
| 103 | <div class="container-fluid"> |
| 104 | <a class="navbar-brand" href="#">{brand}</a> |
| 105 | <div class="navbar-nav ms-auto"> |
| 106 | {' '.join(nav_links)} |
| 107 | </div> |
| 108 | </div> |
| 109 | </nav>""" |
| 110 | |
| 111 | @staticmethod |
| 112 | def _hero(data: Dict) -> str: |
| 113 | """Generate hero section""" |
| 114 | return f""" |
| 115 | <div class="hero text-center"> |
| 116 | <div class="container"> |
| 117 | <h1 class="display-4">{data.get('title', '')}</h1> |
| 118 | <p class="lead">{data.get('subtitle', '')}</p> |
| 119 | </div> |
| 120 | </div>""" |
| 121 | |
| 122 | @staticmethod |
| 123 | def _grid(data: Dict) -> str: |
| 124 | """Generate Bootstrap grid""" |
| 125 | columns = data.get('columns', 3) |
| 126 | items = data.get('items', []) |
| 127 | |
| 128 | cols_html = [] |
| 129 | for item in items: |
| 130 | cols_html.append(f'<div class="col">{BootstrapConverter.convert(item)}</div>') |
| 131 | |
| 132 | return f""" |
| 133 | <div class="container my-5"> |
| 134 | <div class="row row-cols-1 row-cols-md-{columns} g-4"> |
| 135 | {' '.join(cols_html)} |
| 136 | </div> |
| 137 | </div>""" |
| 138 | |
| 139 | @staticmethod |
| 140 | def _card(data: Dict) -> str: |
| 141 | """Generate Bootstrap card""" |
| 142 | title = data.get('title', '') |
| 143 | category = data.get('category', '') |
| 144 | description = data.get('description', '') |
| 145 | actions = data.get('actions', []) |
| 146 | |
| 147 | # Build action buttons |
| 148 | action_html = [] |
| 149 | for action in actions: |
| 150 | if isinstance(action, str): |
| 151 | if action.lower() == 'deploy': |
| 152 | action_html.append(f'<button class="btn btn-success btn-sm" onclick="deployTemplate(\'{title}\')">Deploy</button>') |
| 153 | elif action.lower() == 'preview': |
| 154 | action_html.append(f'<button class="btn btn-secondary btn-sm" onclick="previewTemplate(\'{title}\')">Preview</button>') |
| 155 | elif isinstance(action, dict): |
| 156 | action_html.append(BootstrapConverter._button(action)) |
| 157 | |
| 158 | return f""" |
| 159 | <div class="card h-100"> |
| 160 | <div class="card-body"> |
| 161 | <div class="d-flex justify-content-between align-items-start mb-2"> |
| 162 | <h5 class="card-title">{title}</h5> |
| 163 | {f'<span class="badge bg-primary">{category}</span>' if category else ''} |
| 164 | </div> |
| 165 | <p class="card-text">{description}</p> |
| 166 | </div> |
| 167 | {f'<div class="card-footer bg-transparent"><div class="d-grid gap-2 d-flex justify-content-end">{" ".join(action_html)}</div></div>' if action_html else ''} |
| 168 | </div>""" |
| 169 | |
| 170 | @staticmethod |
| 171 | def _button(data: Dict) -> str: |
| 172 | """Generate Bootstrap button""" |
| 173 | text = data.get('text', 'Button') |
| 174 | variant = data.get('variant', 'primary') |
| 175 | onclick = data.get('onclick', '') |
| 176 | |
| 177 | return f'<button class="btn btn-{variant}" onclick="{onclick}">{text}</button>' |
| 178 | |
| 179 | @staticmethod |
| 180 | def _container(data: Dict) -> str: |
| 181 | """Generate container""" |
| 182 | content = BootstrapConverter.convert(data.get('content', [])) |
| 183 | return f'<div class="container">{content}</div>' |
| 184 | |
| 185 | @staticmethod |
| 186 | def _alert(data: Dict) -> str: |
| 187 | """Generate Bootstrap alert""" |
| 188 | message = data.get('message', '') |
| 189 | alert_type = data.get('variant', 'info') |
| 190 | return f'<div class="alert alert-{alert_type}" role="alert">{message}</div>' |
| 191 | |
| 192 | |
| 193 | # Test it with a template marketplace |
| 194 | if __name__ == "__main__": |
| 195 | # Define the page structure as data |
| 196 | marketplace_page = { |
| 197 | 'type': 'page', |
| 198 | 'title': 'DBBasic - Template Marketplace', |
| 199 | 'components': [ |
| 200 | { |
| 201 | 'type': 'navbar', |
| 202 | 'brand': 'DBBasic', |
| 203 | 'links': ['Monitor', 'Data', 'AI Services', 'Templates'] |
| 204 | }, |
| 205 | { |
| 206 | 'type': 'hero', |
| 207 | 'title': '🛍️ Template Marketplace', |
| 208 | 'subtitle': 'Deploy production-ready applications instantly' |
| 209 | }, |
| 210 | { |
| 211 | 'type': 'grid', |
| 212 | 'columns': 3, |
| 213 | 'items': [ |
| 214 | { |
| 215 | 'type': 'card', |
| 216 | 'title': 'Blog Platform', |
| 217 | 'category': 'CMS', |
| 218 | 'description': 'Complete blog with posts, categories, SEO optimization', |
| 219 | 'actions': ['deploy', 'preview'] |
| 220 | }, |
| 221 | { |
| 222 | 'type': 'card', |
| 223 | 'title': 'E-Commerce Store', |
| 224 | 'category': 'Shop', |
| 225 | 'description': 'Products, orders, inventory, payment processing', |
| 226 | 'actions': ['deploy', 'preview'] |
| 227 | }, |
| 228 | { |
| 229 | 'type': 'card', |
| 230 | 'title': 'CRM System', |
| 231 | 'category': 'Business', |
| 232 | 'description': 'Contacts, leads, opportunities, sales pipeline', |
| 233 | 'actions': ['deploy', 'preview'] |
| 234 | }, |
| 235 | { |
| 236 | 'type': 'card', |
| 237 | 'title': 'Project Manager', |
| 238 | 'category': 'Productivity', |
| 239 | 'description': 'Tasks, kanban boards, team collaboration', |
| 240 | 'actions': ['deploy', 'preview'] |
| 241 | }, |
| 242 | { |
| 243 | 'type': 'card', |
| 244 | 'title': 'Social Platform', |
| 245 | 'category': 'Social', |
| 246 | 'description': 'User posts, comments, likes, shares', |
| 247 | 'actions': ['deploy', 'preview'] |
| 248 | }, |
| 249 | { |
| 250 | 'type': 'card', |
| 251 | 'title': 'Help Desk', |
| 252 | 'category': 'Support', |
| 253 | 'description': 'Tickets, agents, knowledge base, SLA tracking', |
| 254 | 'actions': ['deploy', 'preview'] |
| 255 | } |
| 256 | ] |
| 257 | } |
| 258 | ] |
| 259 | } |
| 260 | |
| 261 | # Convert to HTML |
| 262 | html = BootstrapConverter.convert(marketplace_page) |
| 263 | |
| 264 | # Save to file |
| 265 | with open('test_marketplace.html', 'w') as f: |
| 266 | f.write(html) |
| 267 | |
| 268 | print("Generated test_marketplace.html") |
| 269 | print("Data structure converted to Bootstrap HTML successfully!") |