File size: 1,831 Bytes
90ad0a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Serves the built Svelte frontend alongside the Gradio API."""

import logging
import re
from pathlib import Path

from starlette.responses import HTMLResponse
from starlette.routing import Mount, Route
from starlette.staticfiles import StaticFiles

FRONTEND_DIR = Path(__file__).parent / "frontend" / "dist"
ASSETS_DIR = Path(__file__).parent / "assets"

_logger = logging.getLogger(__name__)

_SPA_SEGMENTS = (
    "metrics",
    "system",
    "media",
    "reports",
    "runs",
    "run",
    "files",
)


def mount_frontend(app):
    if not FRONTEND_DIR.exists():
        _logger.warning(
            "Trackio dashboard UI was not mounted: %s is missing. "
            "Build the frontend with `npm ci && npm run build` in trackio/frontend.",
            FRONTEND_DIR,
        )
        return

    index_html_path = FRONTEND_DIR / "index.html"
    if not index_html_path.exists():
        _logger.warning(
            "Trackio dashboard UI was not mounted: %s is missing.",
            index_html_path,
        )
        return

    index_html_content = index_html_path.read_text()
    patched_html = re.sub(
        r'/assets/(index-[^"]+)',
        r"/assets/app/\1",
        index_html_content,
    )

    async def serve_frontend(request):
        return HTMLResponse(patched_html)

    vite_assets = StaticFiles(directory=str(FRONTEND_DIR / "assets"))
    static_assets = StaticFiles(directory=str(ASSETS_DIR))

    app.routes.insert(0, Mount("/static/trackio", app=static_assets))
    app.routes.insert(0, Mount("/assets/app", app=vite_assets))

    for seg in reversed(_SPA_SEGMENTS):
        app.routes.insert(0, Route(f"/{seg}/", serve_frontend, methods=["GET"]))
        app.routes.insert(0, Route(f"/{seg}", serve_frontend, methods=["GET"]))
    app.routes.insert(0, Route("/", serve_frontend, methods=["GET"]))