-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathserver.py
More file actions
34 lines (27 loc) · 954 Bytes
/
server.py
File metadata and controls
34 lines (27 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from fastapi import FastAPI, Request
from pprint import pprint
app = FastAPI()
import inspect
import os
from .trigger import trigger
filename = inspect.getframeinfo(inspect.currentframe()).filename
BASE_DIR = os.path.dirname(os.path.abspath(filename))
NOTEBOOKS_DIR = BASE_DIR + '/notebooks'
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/notebook/{filepath:path}")
async def read_item(filepath, request: Request):
filepath = os.path.join(NOTEBOOKS_DIR, filepath)
ep = trigger(notebook_filename=filepath, params=request.query_params)
cells = ep[0]['cells']
all_outputs = []
for i, cell in enumerate(cells):
output = cell.get("outputs", [])
if len(output) > 0:
output_data = {
"cell": i,
"output": output
}
all_outputs.append(output_data)
return {"filepath": filepath, "all_outputs": all_outputs}