askrobots / dbbasic Public

Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.

0 downloads

dbbasic/test_html_approaches.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add DBBasic crate engine logo and update README 0d628db 1 year, 2 months ago History
test_html_approaches.py · 6.7 KB · 246 lines
1#!/usr/bin/env python3
2"""
3Test different approaches to see what feels most natural for DBBasic
4"""
5
6# ============================================
7# APPROACH 1: Pure Python data structures
8# ============================================
9
10def 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
44def 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
80class Component:
81 def to_dict(self):
82 return self.__dict__
83
84class 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
96class 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
107def 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
125def 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
172def 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
210print("=" * 60)
211print("APPROACH 1: Pure Python dicts")
212print("=" * 60)
213result1 = approach1_pure_dicts()
214print(result1)
215
216print("\n" + "=" * 60)
217print("APPROACH 2: Minimal helpers")
218print("=" * 60)
219result2 = approach2_minimal_helpers()
220print(result2)
221
222print("\n" + "=" * 60)
223print("APPROACH 3: Component classes")
224print("=" * 60)
225result3 = approach3_component_classes()
226print(result3)
227
228print("\n" + "=" * 60)
229print("APPROACH 4: Bootstrap converter example")
230print("=" * 60)
231sample = {"card": {"title": "Test Card", "desc": "This is a test"}}
232print(to_bootstrap(sample))
233
234print("\n" + "=" * 60)
235print("APPROACH 5: YAML-friendly structure")
236print("=" * 60)
237result5 = approach5_yaml_like()
238print(result5)
239
240print("\n" + "=" * 60)
241print("Which approach feels most natural for DBBasic?")
242print("1. Pure dicts - Simple, no magic")
243print("2. Minimal helpers - Slight abstraction")
244print("3. Component classes - OOP style")
245print("4. Smart converter - Infers HTML from structure")
246print("5. YAML-like - Config-driven")