From bf8dcbe213ca1e5ea5abff8671746e260955a28a Mon Sep 17 00:00:00 2001 From: Max Novelli Date: Tue, 11 Mar 2025 10:36:07 +0100 Subject: [PATCH 1/3] Added alive endpoint and renamed templates one --- src/sciwyrm/main.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/sciwyrm/main.py b/src/sciwyrm/main.py index fc03735..6393ad9 100644 --- a/src/sciwyrm/main.py +++ b/src/sciwyrm/main.py @@ -5,6 +5,7 @@ import json from typing import Annotated +import uvicorn from fastapi import Depends, FastAPI, Request, Response from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse @@ -19,13 +20,28 @@ notebook_template_path, ) +import logging + app = FastAPI() +logger = logging.getLogger(__name__) +logger.setLevel(logging.INFO) +logger.addHandler(logging.StreamHandler()) + + +@app.get("/alive", response_description="The service is alive") +async def list_templates( + config: Annotated[AppConfig, Depends(app_config)] +) -> bool: + """Return a list of available notebook templates.""" + return True + -@app.get("/notebook/templates", response_description="Available templates") +@app.get("/templates", response_description="Available templates") async def list_templates( config: Annotated[AppConfig, Depends(app_config)] ) -> list[notebook.TemplateSummary]: + logger.info("Templates") """Return a list of available notebook templates.""" return notebook.available_templates(config) @@ -56,11 +72,12 @@ async def template_schema( @app.post("/notebook", response_model=dict, response_description="Rendered notebook") -async def format_notebook( +async def format_notebook_from_json( request: Request, templates: Annotated[Jinja2Templates, Depends(get_templates)], spec: notebook.NotebookSpecWithConfig = Depends(_inject_app_config), # noqa: B008 ) -> Response: + """Format and return a notebook.""" formatted = templates.TemplateResponse( name=notebook_template_path(spec.template_id), @@ -70,3 +87,7 @@ async def format_notebook( nb = json.loads(formatted.body) notebook.insert_notebook_metadata(nb, spec) return JSONResponse(nb) + + +if __name__ == "__main__": + uvicorn.run(app, host="0.0.0.0", port=8000) From 8e9c6f2a7c68959a929591003310779940142a25 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Wynen Date: Fri, 23 May 2025 13:31:18 +0200 Subject: [PATCH 2/3] Rename alive to livez --- src/sciwyrm/main.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/sciwyrm/main.py b/src/sciwyrm/main.py index 6393ad9..7976b8d 100644 --- a/src/sciwyrm/main.py +++ b/src/sciwyrm/main.py @@ -29,12 +29,10 @@ logger.addHandler(logging.StreamHandler()) -@app.get("/alive", response_description="The service is alive") -async def list_templates( - config: Annotated[AppConfig, Depends(app_config)] -) -> bool: - """Return a list of available notebook templates.""" - return True +@app.get("/livez", response_description="The service is alive") +async def livez() -> str: + """Return 200 if the serve is alive.""" + return "ok" @app.get("/templates", response_description="Available templates") From 45223314080aced5b82e61cfd6bb767fc673f799 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Wynen Date: Fri, 23 May 2025 13:35:44 +0200 Subject: [PATCH 3/3] Remove debug logging --- src/sciwyrm/main.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/sciwyrm/main.py b/src/sciwyrm/main.py index 7976b8d..f0e46a0 100644 --- a/src/sciwyrm/main.py +++ b/src/sciwyrm/main.py @@ -20,14 +20,8 @@ notebook_template_path, ) -import logging - app = FastAPI() -logger = logging.getLogger(__name__) -logger.setLevel(logging.INFO) -logger.addHandler(logging.StreamHandler()) - @app.get("/livez", response_description="The service is alive") async def livez() -> str: @@ -39,7 +33,7 @@ async def livez() -> str: async def list_templates( config: Annotated[AppConfig, Depends(app_config)] ) -> list[notebook.TemplateSummary]: - logger.info("Templates") + """Return a list of all available templates.""" """Return a list of available notebook templates.""" return notebook.available_templates(config) @@ -75,7 +69,6 @@ async def format_notebook_from_json( templates: Annotated[Jinja2Templates, Depends(get_templates)], spec: notebook.NotebookSpecWithConfig = Depends(_inject_app_config), # noqa: B008 ) -> Response: - """Format and return a notebook.""" formatted = templates.TemplateResponse( name=notebook_template_path(spec.template_id),