Skip to content

Commit 4d60af3

Browse files
andrevanzuydamclaudetina4stack
committed
feat(dev-admin): split preview, data-driven scaffold, page/base generators (Python)
Python side of the dev-dashboard overhaul (feature 142): - /__dev/split: a split view - dashboard/editor LEFT, live app preview RIGHT - served as a plain page so the container never reloads. Device frame (mobile/ tablet framed on a light backdrop, desktop fills), device toggle + live dimensions + pop-out, draggable gutter, and Files/Editor/Preview icon toggles (hiding the editor keeps the file nav). The Dashboard toolbar link navigates here instead of the old in-app iframe overlay. - Scaffold endpoints are data-driven from the CLI GENERATORS registry: the list endpoint reports every generator (+ needs_name/summary/usage); the run endpoint dispatches any kind through its handler. - New generators: "generate base" (Frond base.twig + index page + pages/ folder + / route) and "generate page NAME" (a page that extends base, auto-creating pages/ and bootstrapping base). "base" added to the no-name generators. - Bundles the shared tina4-dev-admin SPA that renders the scaffold form, the editor disk-resync and the eye-to-preview. Verified live on :7150. Depends on the reload-loop fix (#210) for a usable dashboard; both target v3. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Co-Authored-By: Tina4 <82961293+tina4stack@users.noreply.github.com> Signed-off-by: Andre van Zuydam <andrevanzuydam@gmail.com>
1 parent 0dd0787 commit 4d60af3

4 files changed

Lines changed: 500 additions & 162 deletions

File tree

‎tina4_python/cli/__init__.py‎

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,8 +1652,8 @@ def _generate(args):
16521652

16531653
what = args[0].lower()
16541654

1655-
# Auth doesn't require a name argument
1656-
no_name_generators = {"auth"}
1655+
# These generators take no name argument.
1656+
no_name_generators = {"auth", "base"}
16571657
if what not in no_name_generators and len(args) < 2:
16581658
print(f"Usage: tina4python generate {what} <name> [options]")
16591659
sys.exit(1)
@@ -3352,6 +3352,127 @@ def _gen_form(name: str, flags: dict = None):
33523352
print(f" ✓ Created {path}")
33533353

33543354

3355+
_BASE_TWIG = '''<!DOCTYPE html>
3356+
<html lang="en">
3357+
<head>
3358+
<meta charset="UTF-8">
3359+
<title>{% block title %}{{ APP_NAME }}{% endblock %}</title>
3360+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
3361+
<link rel="stylesheet" href="/css/tina4.min.css">
3362+
<link rel="stylesheet" href="/css/default.css">
3363+
{% block stylesheets %}{% endblock %}
3364+
</head>
3365+
<body>
3366+
{% block nav %}{% include "partials/nav.twig" ignore missing %}{% endblock %}
3367+
<main>{% block content %}{% endblock %}</main>
3368+
{% block javascripts %}
3369+
<script src="/js/tina4.min.js"></script>
3370+
<script src="/js/frond.min.js"></script>
3371+
{% endblock %}
3372+
</body>
3373+
</html>
3374+
'''
3375+
3376+
3377+
def _ensure_base_template():
3378+
"""Create src/templates/base.twig and the src/templates/pages/ folder if
3379+
missing. Returns the paths created (empty when everything already existed).
3380+
Shared by `generate base` and `generate page` so a page can be scaffolded
3381+
into a fresh project and still inherit from a real base."""
3382+
created = []
3383+
templates = Path("src/templates")
3384+
templates.mkdir(parents=True, exist_ok=True)
3385+
(templates / "pages").mkdir(parents=True, exist_ok=True)
3386+
base = templates / "base.twig"
3387+
if not base.exists():
3388+
base.write_text(_BASE_TWIG, encoding="utf-8")
3389+
created.append(str(base))
3390+
return created
3391+
3392+
3393+
def _page_twig(title: str) -> str:
3394+
"""A Frond page template that extends base.twig (Twig braces kept literal)."""
3395+
return (
3396+
'{% extends "base.twig" %}\n'
3397+
'{% block title %}' + title + '{% endblock %}\n'
3398+
'{% block content %}\n'
3399+
'<div class="container mt-4">\n'
3400+
' <h1>' + title + '</h1>\n'
3401+
' {# tina4:edit page content goes here #}\n'
3402+
'</div>\n'
3403+
'{% endblock %}\n'
3404+
)
3405+
3406+
3407+
def _page_route(url_path: str, template_name: str, fn_name: str, title: str) -> str:
3408+
"""A route that renders a page template through the @template decorator."""
3409+
return (
3410+
"from tina4_python.core.router import get, template\n\n\n"
3411+
'@get("' + url_path + '")\n'
3412+
'@template("' + template_name + '")\n'
3413+
"async def " + fn_name + "(request, response):\n"
3414+
' return {"title": "' + title + '"}\n'
3415+
)
3416+
3417+
3418+
def _gen_base(name: str = None, flags: dict = None):
3419+
"""Scaffold a Frond base template + an index page that extends it.
3420+
3421+
tina4python generate base
3422+
3423+
Creates src/templates/base.twig (if missing), the src/templates/pages/
3424+
folder, src/templates/pages/index.twig (extends base), and the `/` route
3425+
that renders it. Subsequent `generate page <name>` pages inherit from base.
3426+
"""
3427+
created = _ensure_base_template()
3428+
index_tpl = Path("src/templates/pages/index.twig")
3429+
if not index_tpl.exists():
3430+
index_tpl.write_text(_page_twig("Home"), encoding="utf-8")
3431+
created.append(str(index_tpl))
3432+
routes = Path("src/routes")
3433+
routes.mkdir(parents=True, exist_ok=True)
3434+
index_route = routes / "index.py"
3435+
if not index_route.exists():
3436+
index_route.write_text(_page_route("/", "pages/index.twig", "index_page", "Home"), encoding="utf-8")
3437+
created.append(str(index_route))
3438+
if created:
3439+
for p in created:
3440+
print(f" ✓ Created {p}")
3441+
else:
3442+
print(" ✓ base template + index page already present")
3443+
3444+
3445+
def _gen_page(name: str, flags: dict = None):
3446+
"""Scaffold a page that inherits from the Frond base template.
3447+
3448+
tina4python generate page about
3449+
3450+
Creates src/templates/pages/<name>.twig (extends base.twig) and a route that
3451+
renders it. Auto-creates the pages/ folder and bootstraps base.twig when they
3452+
do not exist yet, so a page always inherits from a real base.
3453+
"""
3454+
slug = _to_snake(name or "").strip("/")
3455+
if not slug:
3456+
print(" ✗ page needs a name (e.g. generate page about)")
3457+
return
3458+
title = slug.replace("_", " ").title()
3459+
created = _ensure_base_template()
3460+
tpl = Path("src/templates/pages") / f"{slug}.twig"
3461+
if tpl.exists():
3462+
print(f" ✗ File already exists: {tpl}")
3463+
return
3464+
tpl.write_text(_page_twig(title), encoding="utf-8")
3465+
created.append(str(tpl))
3466+
routes = Path("src/routes")
3467+
routes.mkdir(parents=True, exist_ok=True)
3468+
route = routes / f"{slug}.py"
3469+
if not route.exists():
3470+
route.write_text(_page_route(f"/{slug}", f"pages/{slug}.twig", f"{slug}_page", title), encoding="utf-8")
3471+
created.append(str(route))
3472+
for p in created:
3473+
print(f" ✓ Created {p}")
3474+
3475+
33553476
def _gen_view(name: str, flags: dict = None):
33563477
"""Generate list + detail view templates.
33573478
@@ -3912,6 +4033,8 @@ def _commands(args=None):
39124033
GENERATORS = {
39134034
"model": {"handler": _gen_model, "usage": '<Name> [--fields "name:string,price:float"] [--table-name <name>]', "summary": "ORM model + matching migration"},
39144035
"route": {"handler": _gen_route, "usage": "<name> [--model Name] [--public]", "summary": "CRUD route file, secure by default (--public opens writes)"},
4036+
"base": {"handler": _gen_base, "usage": "", "summary": "Frond base template + index page + pages/ folder"},
4037+
"page": {"handler": _gen_page, "usage": "<name>", "summary": "Page that extends base.twig (auto-creates pages/ + base)"},
39154038
"crud": {"handler": _gen_crud, "usage": '<Name> [--fields "..."] [--public] [--no-templates]', "summary": "Model + migration + AutoCrud-backed admin page (to_crud) + editable crud/ templates + gate test"},
39164039
"migration": {"handler": _gen_migration, "usage": "<description>", "summary": "Timestamped migration file (UP/DOWN)"},
39174040
"middleware": {"handler": _gen_middleware, "usage": "<Name>", "summary": "Middleware with before/after hooks"},

‎tina4_python/core/server.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,7 @@ def _handle_rate_limit(request: Request, response: Response) -> Response | None:
16541654

16551655
async def _handle_dev_admin(request: Request, response: Response) -> Response:
16561656
"""Serve the /__dev dashboard and API routes."""
1657-
from tina4_python.dev_admin import get_api_handlers, _dev_request_denial
1657+
from tina4_python.dev_admin import get_api_handlers, _dev_request_denial, split_view_html
16581658
# ADR-0082: ONE gate for every /__dev request, reads and writes alike -
16591659
# Host allow-list (DNS rebinding), same-origin (CSRF and cross-site reads)
16601660
# and loopback peer. Scoped to /__dev so the deliberately cross-origin
@@ -1665,7 +1665,15 @@ async def _handle_dev_admin(request: Request, response: Response) -> Response:
16651665
response.status(_denial[0]).json({"ok": False, "error": _denial[1]})
16661666
_cors.apply(request, response)
16671667
return response
1668-
if request.path in ("/__dev/", "/__dev", "/__dev/v2", "/__dev/v2/"):
1668+
if request.path in ("/__dev/split", "/__dev/split/"):
1669+
# Detached split view: dashboard/editor LEFT, live app preview RIGHT.
1670+
# A plain page (no toolbar, no reload) hosting two iframes that each
1671+
# keep their own reload behaviour - so the editor stays put while the
1672+
# preview updates on a change.
1673+
response.html(split_view_html())
1674+
response.header("cache-control", "no-store, must-revalidate")
1675+
response.header("pragma", "no-cache")
1676+
elif request.path in ("/__dev/", "/__dev", "/__dev/v2", "/__dev/v2/"):
16691677
# Unified SPA dev admin. The bundle derives its WS URL from
16701678
# `location.host` directly, so no environment shim is needed —
16711679
# the framework serves /__dev_reload on its own port and the

0 commit comments

Comments
 (0)