Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
test_html_approaches.py · 6.7 KB · 246 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Test different approaches to see what feels most natural for DBBasic |
| 4 | """ |
| 5 | |
| 6 | # ============================================ |
| 7 | # APPROACH 1: Pure Python data structures |
| 8 | # ============================================ |
| 9 | |
| 10 | def approach1_pure_dicts(): |
| 11 | """Everything is just dicts and lists""" |
| 12 | |
| 13 | page = { |
| 14 | "navbar": { |
| 15 | "brand": "DBBasic", |
| 16 | "links": [ |
| 17 | {"text": "Monitor", "url": "http://localhost:8004"}, |
| 18 | {"text": "Data", "url": "http://localhost:8005"} |
| 19 | ] |
| 20 | }, |
| 21 | "content": { |
| 22 | "title": "Template Marketplace", |
| 23 | "cards": [ |
| 24 | { |
| 25 | "title": "Blog Posts", |
| 26 | "badge": "CMS", |
| 27 | "description": "Manage blog posts with SEO", |
| 28 | "actions": [ |
| 29 | {"type": "button", "text": "Deploy", "variant": "success"}, |
| 30 | {"type": "button", "text": "Preview", "variant": "secondary"} |
| 31 | ] |
| 32 | } |
| 33 | ] |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | return page |
| 38 | |
| 39 | |
| 40 | # ============================================ |
| 41 | # APPROACH 2: Minimal markup helpers |
| 42 | # ============================================ |
| 43 | |
| 44 | def approach2_minimal_helpers(): |
| 45 | """Simple helper functions that return dicts""" |
| 46 | |
| 47 | def nav(brand, *links): |
| 48 | return {"nav": {"brand": brand, "links": links}} |
| 49 | |
| 50 | def card(title, desc, badge=None): |
| 51 | return { |
| 52 | "card": { |
| 53 | "title": title, |
| 54 | "desc": desc, |
| 55 | "badge": badge |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | def btn(text, action=None, style="primary"): |
| 60 | return {"btn": text, "action": action, "style": style} |
| 61 | |
| 62 | # Usage |
| 63 | page = { |
| 64 | "nav": nav("DBBasic", |
| 65 | {"text": "Monitor", "url": "/monitor"}, |
| 66 | {"text": "Data", "url": "/data"}), |
| 67 | "cards": [ |
| 68 | card("Blog Posts", "Manage content", badge="CMS"), |
| 69 | card("Products", "E-commerce catalog", badge="Shop") |
| 70 | ] |
| 71 | } |
| 72 | |
| 73 | return page |
| 74 | |
| 75 | |
| 76 | # ============================================ |
| 77 | # APPROACH 3: Component classes |
| 78 | # ============================================ |
| 79 | |
| 80 | class Component: |
| 81 | def to_dict(self): |
| 82 | return self.__dict__ |
| 83 | |
| 84 | class Card(Component): |
| 85 | def __init__(self, title, description, badge=None): |
| 86 | self.type = "card" |
| 87 | self.title = title |
| 88 | self.description = description |
| 89 | self.badge = badge |
| 90 | self.actions = [] |
| 91 | |
| 92 | def add_action(self, text, action): |
| 93 | self.actions.append({"text": text, "action": action}) |
| 94 | return self |
| 95 | |
| 96 | class Page(Component): |
| 97 | def __init__(self, title): |
| 98 | self.type = "page" |
| 99 | self.title = title |
| 100 | self.components = [] |
| 101 | |
| 102 | def add(self, component): |
| 103 | self.components.append(component.to_dict() if hasattr(component, 'to_dict') else component) |
| 104 | return self |
| 105 | |
| 106 | |
| 107 | def approach3_component_classes(): |
| 108 | """OOP-style component building""" |
| 109 | |
| 110 | page = Page("Template Marketplace") |
| 111 | |
| 112 | card = Card("Blog Posts", "Content management", "CMS") |
| 113 | card.add_action("Deploy", "deploy('blog')") |
| 114 | card.add_action("Preview", "preview('blog')") |
| 115 | |
| 116 | page.add(card) |
| 117 | |
| 118 | return page.to_dict() |
| 119 | |
| 120 | |
| 121 | # ============================================ |
| 122 | # APPROACH 4: Bootstrap-aware converter |
| 123 | # ============================================ |
| 124 | |
| 125 | def to_bootstrap(data): |
| 126 | """Convert Python data structures to Bootstrap HTML |
| 127 | |
| 128 | Rules: |
| 129 | - Dict with 'card' key -> Bootstrap card |
| 130 | - Dict with 'nav' key -> Bootstrap navbar |
| 131 | - List -> Bootstrap grid |
| 132 | - String -> Raw HTML |
| 133 | """ |
| 134 | |
| 135 | if isinstance(data, dict): |
| 136 | if 'card' in data: |
| 137 | c = data['card'] |
| 138 | return f''' |
| 139 | <div class="card"> |
| 140 | <div class="card-body"> |
| 141 | <h5 class="card-title">{c.get('title', '')}</h5> |
| 142 | <p class="card-text">{c.get('desc', '')}</p> |
| 143 | </div> |
| 144 | </div>''' |
| 145 | |
| 146 | elif 'nav' in data: |
| 147 | n = data['nav'] |
| 148 | links = ''.join([f'<a class="nav-link" href="{l["url"]}">{l["text"]}</a>' |
| 149 | for l in n.get('links', [])]) |
| 150 | return f''' |
| 151 | <nav class="navbar navbar-expand-lg navbar-light bg-light"> |
| 152 | <a class="navbar-brand" href="#">{n.get('brand', '')}</a> |
| 153 | <div class="navbar-nav ms-auto">{links}</div> |
| 154 | </nav>''' |
| 155 | |
| 156 | elif 'grid' in data: |
| 157 | cols = ''.join([f'<div class="col">{to_bootstrap(item)}</div>' |
| 158 | for item in data['grid']]) |
| 159 | return f'<div class="row">{cols}</div>' |
| 160 | |
| 161 | elif isinstance(data, list): |
| 162 | # Lists become grids by default |
| 163 | return to_bootstrap({'grid': data}) |
| 164 | |
| 165 | return str(data) |
| 166 | |
| 167 | |
| 168 | # ============================================ |
| 169 | # APPROACH 5: YAML-like structure |
| 170 | # ============================================ |
| 171 | |
| 172 | def approach5_yaml_like(): |
| 173 | """Structure that would work well in YAML""" |
| 174 | |
| 175 | page = { |
| 176 | "layout": "dashboard", |
| 177 | "components": [ |
| 178 | { |
| 179 | "type": "navbar", |
| 180 | "brand": "DBBasic", |
| 181 | "links": ["Monitor", "Data", "Templates"] |
| 182 | }, |
| 183 | { |
| 184 | "type": "hero", |
| 185 | "title": "Template Marketplace", |
| 186 | "subtitle": "Deploy apps instantly" |
| 187 | }, |
| 188 | { |
| 189 | "type": "grid", |
| 190 | "columns": 3, |
| 191 | "items": [ |
| 192 | { |
| 193 | "type": "card", |
| 194 | "title": "Blog", |
| 195 | "category": "CMS", |
| 196 | "actions": ["deploy", "preview"] |
| 197 | } |
| 198 | ] |
| 199 | } |
| 200 | ] |
| 201 | } |
| 202 | |
| 203 | return page |
| 204 | |
| 205 | |
| 206 | # ============================================ |
| 207 | # Test all approaches |
| 208 | # ============================================ |
| 209 | |
| 210 | print("=" * 60) |
| 211 | print("APPROACH 1: Pure Python dicts") |
| 212 | print("=" * 60) |
| 213 | result1 = approach1_pure_dicts() |
| 214 | print(result1) |
| 215 | |
| 216 | print("\n" + "=" * 60) |
| 217 | print("APPROACH 2: Minimal helpers") |
| 218 | print("=" * 60) |
| 219 | result2 = approach2_minimal_helpers() |
| 220 | print(result2) |
| 221 | |
| 222 | print("\n" + "=" * 60) |
| 223 | print("APPROACH 3: Component classes") |
| 224 | print("=" * 60) |
| 225 | result3 = approach3_component_classes() |
| 226 | print(result3) |
| 227 | |
| 228 | print("\n" + "=" * 60) |
| 229 | print("APPROACH 4: Bootstrap converter example") |
| 230 | print("=" * 60) |
| 231 | sample = {"card": {"title": "Test Card", "desc": "This is a test"}} |
| 232 | print(to_bootstrap(sample)) |
| 233 | |
| 234 | print("\n" + "=" * 60) |
| 235 | print("APPROACH 5: YAML-friendly structure") |
| 236 | print("=" * 60) |
| 237 | result5 = approach5_yaml_like() |
| 238 | print(result5) |
| 239 | |
| 240 | print("\n" + "=" * 60) |
| 241 | print("Which approach feels most natural for DBBasic?") |
| 242 | print("1. Pure dicts - Simple, no magic") |
| 243 | print("2. Minimal helpers - Slight abstraction") |
| 244 | print("3. Component classes - OOP style") |
| 245 | print("4. Smart converter - Infers HTML from structure") |
| 246 | print("5. YAML-like - Config-driven") |