Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add welcome page file browser #30

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/kweb/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,12 +25,32 @@
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")
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"
Expand Down
24 changes: 24 additions & 0 deletions src/kweb/templates/file_browser.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<head>
<title>KWeb GDS File Browser</title>
</head>
<body>

<div id="message">
<p>
{{ message }}
</p>
</div>

<main>
<p>Files available:</p>
<ul>
{% for file in files_metadata %}
<li><a href="{{ file['url'] }}">{{ file['name'] }}</a></li>
{% endfor %}
</ul>
<p>File root: <i>{{ files_root }}</i></p>
</main>

</body>
</html>
Loading