5
5
import json
6
6
from typing import Annotated
7
7
8
+ import uvicorn
8
9
from fastapi import Depends , FastAPI , Request , Response
9
10
from fastapi .exceptions import RequestValidationError
10
11
from fastapi .responses import JSONResponse
19
20
notebook_template_path ,
20
21
)
21
22
23
+ import logging
24
+
22
25
app = FastAPI ()
23
26
27
+ logger = logging .getLogger (__name__ )
28
+ logger .setLevel (logging .INFO )
29
+ logger .addHandler (logging .StreamHandler ())
30
+
31
+
32
+ @app .get ("/alive" , response_description = "The service is alive" )
33
+ async def list_templates (
34
+ config : Annotated [AppConfig , Depends (app_config )]
35
+ ) -> bool :
36
+ """Return a list of available notebook templates."""
37
+ return True
38
+
24
39
25
- @app .get ("/notebook/ templates" , response_description = "Available templates" )
40
+ @app .get ("/templates" , response_description = "Available templates" )
26
41
async def list_templates (
27
42
config : Annotated [AppConfig , Depends (app_config )]
28
43
) -> list [notebook .TemplateSummary ]:
44
+ logger .info ("Templates" )
29
45
"""Return a list of available notebook templates."""
30
46
return notebook .available_templates (config )
31
47
@@ -44,12 +60,13 @@ def _inject_app_config(
44
60
raise RequestValidationError (errors ) from None
45
61
46
62
47
- @app .post ("/notebook" , response_model = dict , response_description = "Rendered notebook" )
48
- async def format_notebook (
63
+ @app .post ("/notebook" , response_model = dict , response_description = "Rendered notebook with json input " )
64
+ async def format_notebook_from_json (
49
65
request : Request ,
50
66
templates : Annotated [Jinja2Templates , Depends (get_templates )],
51
67
spec : notebook .NotebookSpecWithConfig = Depends (_inject_app_config ), # noqa: B008
52
68
) -> Response :
69
+
53
70
"""Format and return a notebook."""
54
71
formatted = templates .TemplateResponse (
55
72
name = notebook_template_path (spec .template_id ),
@@ -59,3 +76,7 @@ async def format_notebook(
59
76
nb = json .loads (formatted .body )
60
77
nb ["metadata" ]["sciwyrm" ] = notebook .notebook_metadata (spec )
61
78
return JSONResponse (nb )
79
+
80
+
81
+ if __name__ == "__main__" :
82
+ uvicorn .run (app , host = "0.0.0.0" , port = 8000 )
0 commit comments