Files available:
+-
+{% for file in files_metadata %}
+
- {{ file['name'] }} +{% endfor %} +
File root: {{ files_root }}
+From b4404dec9c8198611b8491b7069024ce25059608 Mon Sep 17 00:00:00 2001 From: Joaquin Matres <4514346+joamatab@users.noreply.github.com> Date: Mon, 21 Aug 2023 07:41:01 -0700 Subject: [PATCH 1/2] add welcome page file browser --- src/kweb/main.py | 22 ++++++++++++++++++++++ src/kweb/templates/file_browser.html.j2 | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/kweb/templates/file_browser.html.j2 diff --git a/src/kweb/main.py b/src/kweb/main.py index 7387517..0f1b38d 100644 --- a/src/kweb/main.py +++ b/src/kweb/main.py @@ -1,6 +1,8 @@ import os from pathlib import Path from typing import Any +from glob import glob +import pathlib from fastapi import APIRouter, FastAPI, Request from fastapi.exceptions import HTTPException @@ -23,6 +25,26 @@ edafiles: Path | None = None +@router.get("/", response_class=HTMLResponse) +async def gds_list(request: Request): + """List all saved GDS files.""" + files_root = pathlib.Path(os.getenv("KWEB_FILESLOCATION")) + paths_list = glob(str(files_root / "*.gds")) + files_list = sorted(Path(gdsfile).stem for gdsfile in paths_list) + files_metadata = [ + {"name": file_name, "url": f"gds/{file_name}"} for file_name in files_list + ] + return templates.TemplateResponse( + "file_browser.html.j2", + { + "request": request, + "message": f"GDS files in {str(files_root)!r}", + "files_root": files_root, + "files_metadata": files_metadata, + }, + ) + + def get_app(files_location: str | Path | None = None) -> FastAPI: if files_location is None: envedafiles = os.getenv("KWEB_FILESLOCATION") diff --git a/src/kweb/templates/file_browser.html.j2 b/src/kweb/templates/file_browser.html.j2 new file mode 100644 index 0000000..e6c557c --- /dev/null +++ b/src/kweb/templates/file_browser.html.j2 @@ -0,0 +1,24 @@ + +
++{{ message }} +
+Files available:
+File root: {{ files_root }}
+