Skip to content

Commit

Permalink
Add an endpoint to initialize the GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhelba committed May 16, 2023
1 parent 3a1ca15 commit a85abe7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
21 changes: 16 additions & 5 deletions imagedephi/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path

from fastapi import BackgroundTasks, FastAPI, Form, HTTPException, Request
from fastapi.responses import HTMLResponse, PlainTextResponse
from fastapi.responses import HTMLResponse, PlainTextResponse, RedirectResponse
from fastapi.templating import Jinja2Templates
from jinja2 import FunctionLoader
from starlette.background import BackgroundTask
Expand Down Expand Up @@ -63,11 +63,22 @@ def reset_shutdown_event() -> None:
shutdown_event.clear()


@app.get("/", response_class=HTMLResponse)
@app.get("/", response_class=RedirectResponse)
def home(request: Request):
# On Windows, there may be multiple roots, so pick the one that's an ancestor of the CWD
# On Linux, this should typically resolve to "/"
root_directory = Path.cwd().root

return request.url_for("select_directory").include_query_params(
input_directory=str(root_directory), output_directory=str(root_directory)
)


@app.get("/select-directory/", response_class=HTMLResponse)
def select_directory(
request: Request,
input_directory: Path = Path("/"), # noqa: B008
output_directory: Path = Path("/"), # noqa: B008
input_directory: Path,
output_directory: Path,
):
# TODO: if input_directory is specified but an empty string, it gets instantiated as the CWD
if not input_directory.is_dir():
Expand All @@ -85,7 +96,7 @@ def select_directory(
)


@app.post("/redact/")
@app.post("/redact/", response_class=HTMLResponse)
def redact(
background_tasks: BackgroundTasks,
input_directory: Path = Form(), # noqa: B008
Expand Down
1 change: 1 addition & 0 deletions imagedephi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ async def announce_ready() -> None:
# To avoid race conditions, ensure that the webserver is
# actually running before launching the browser
await wait_for_port(port)
app.url_path_for("home")
url = f"http://{host}:{port}/"
click.echo(f"Server is running at {url} .")
webbrowser.open(url)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def client() -> TestClient:
return TestClient(app)


def test_gui_select_directory(client: TestClient) -> None:
response = client.get("/")
def test_gui_home(client: TestClient) -> None:
response = client.get(app.url_path_for("home"), follow_redirects=True)

assert response.status_code == 200
assert "Select Directory" in response.text
Expand Down

0 comments on commit a85abe7

Please sign in to comment.