askrobots / dbbasic-admin
Public
An admin interface that builds itself from the dbbasic modules you install; its pages follow file paths, like CGI.
database.py · 3.4 KB · 72 lines
| 1 | """Database browser handler - /admin/database""" |
| 2 | from dbbasic_web.responses import html |
| 3 | from dbbasic_admin.admin import build_nav |
| 4 | from dbbasic_admin.discover import discover_tsv_tables, read_tsv_data, format_file_size |
| 5 | |
| 6 | |
| 7 | def handle(request): |
| 8 | """Render database browser""" |
| 9 | nav_items = build_nav() |
| 10 | tables = discover_tsv_tables() |
| 11 | |
| 12 | # Get selected table from query params |
| 13 | selected_table = request.get('query', {}).get('table') |
| 14 | table_data = [] |
| 15 | |
| 16 | if selected_table: |
| 17 | table_data = read_tsv_data(selected_table, limit=100) |
| 18 | |
| 19 | nav_html = "".join(f'<li><a href="{item["href"]}">{item.get("icon", "")} {item["label"]}</a></li>' for item in nav_items) |
| 20 | |
| 21 | # Build tables grid |
| 22 | tables_html = "" |
| 23 | for t in tables: |
| 24 | tables_html += f'''<div style="border: 1px solid #ddd; padding: 15px; border-radius: 5px;"> |
| 25 | <h3>{t["name"]}.tsv</h3> |
| 26 | <p>{t["rows"]} rows • {format_file_size(t["size"])}</p> |
| 27 | <p style="font-size: 12px; color: #666;">{", ".join(t["columns"][:5])}{" +"+str(len(t["columns"])-5)+" more" if len(t["columns"]) > 5 else ""}</p> |
| 28 | <a href="/admin/database?table={t["name"]}" style="color: #0066cc;">View Data</a> |
| 29 | </div>''' |
| 30 | |
| 31 | # Build data table if selected |
| 32 | data_html = "" |
| 33 | if selected_table and table_data: |
| 34 | columns = list(table_data[0].keys()) if table_data else [] |
| 35 | data_html = f'<h3>Table: {selected_table}</h3><table style="width: 100%; border-collapse: collapse;"><thead><tr>' |
| 36 | data_html += "".join(f'<th style="border: 1px solid #ddd; padding: 8px; background: #f5f5f5;">{col}</th>' for col in columns) |
| 37 | data_html += '</tr></thead><tbody>' |
| 38 | for row in table_data: |
| 39 | data_html += '<tr>' |
| 40 | data_html += "".join(f'<td style="border: 1px solid #ddd; padding: 8px;">{row.get(col, "")}</td>' for col in columns) |
| 41 | data_html += '</tr>' |
| 42 | data_html += '</tbody></table>' |
| 43 | |
| 44 | html_content = f"""<!DOCTYPE html> |
| 45 | <html> |
| 46 | <head> |
| 47 | <title>Database - Admin</title> |
| 48 | <style> |
| 49 | * {{ margin: 0; padding: 0; box-sizing: border-box; }} |
| 50 | body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; display: flex; height: 100vh; background: #f5f5f5; }} |
| 51 | .sidebar {{ width: 250px; background: #2c3e50; color: white; padding: 20px; overflow-y: auto; }} |
| 52 | .sidebar h1 {{ font-size: 24px; margin-bottom: 30px; color: #ecf0f1; }} |
| 53 | .sidebar ul {{ list-style: none; }} |
| 54 | .sidebar li {{ margin-bottom: 10px; }} |
| 55 | .sidebar a {{ color: #ecf0f1; text-decoration: none; display: block; padding: 10px; border-radius: 5px; }} |
| 56 | .content {{ flex: 1; padding: 40px; overflow-y: auto; }} |
| 57 | .header {{ background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; }} |
| 58 | .card {{ background: white; padding: 20px; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; }} |
| 59 | .grid {{ display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }} |
| 60 | </style> |
| 61 | </head> |
| 62 | <body> |
| 63 | <div class="sidebar"><h1>Admin</h1><ul>{nav_html}</ul></div> |
| 64 | <div class="content"> |
| 65 | <div class="header"><h2>🗄️ Database</h2><p>Browse TSV tables</p></div> |
| 66 | <div class="card"><h3>Tables</h3><div class="grid">{tables_html}</div></div> |
| 67 | {f'<div class="card">{data_html}</div>' if data_html else ''} |
| 68 | </div> |
| 69 | </body> |
| 70 | </html>""" |
| 71 | |
| 72 | return html(html_content) |