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 @@ + + + KWeb GDS File Browser + + + +
+

+{{ message }} +

+
+ +
+

Files available:

+ +

File root: {{ files_root }}

+
+ + + From d4e12a3aa548d821625d4cc7f661103988bedbf0 Mon Sep 17 00:00:00 2001 From: Joaquin Matres <4514346+joamatab@users.noreply.github.com> Date: Mon, 21 Aug 2023 08:05:08 -0700 Subject: [PATCH 2/2] fix typo --- src/kweb/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kweb/main.py b/src/kweb/main.py index 0f1b38d..7e9b32c 100644 --- a/src/kweb/main.py +++ b/src/kweb/main.py @@ -50,7 +50,7 @@ def get_app(files_location: str | Path | None = None) -> FastAPI: envedafiles = os.getenv("KWEB_FILESLOCATION") if envedafiles is None: raise RuntimeError( - "A filels location must be set, either via " + "A files location must be set, either via " "kweb.main.get_app(path) as a string or Path object. " 'Alternatively the env variable "KWEB_FILESLOCATION"' " can be set with the path"