File size: 833 Bytes
e1d45bd
 
 
 
 
 
c0b4976
e1d45bd
fd25f55
e1d45bd
 
07ed8fc
fd25f55
 
e1d45bd
 
 
07317c7
e1d45bd
 
 
03fa64f
fd25f55
e1d45bd
 
fd25f55
e1d45bd
 
 
 
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
from fastapi import FastAPI, Response
import papermill as pm
import io

app = FastAPI()

@app.get("/")
def run_notebook(fruits: str):
    
    fruit_list = fruits.split(",")

    with open("/tmp/input.txt", "w") as f:
        f.write("\n".join(fruit_list))

    # Execute the notebook with Papermill
    pm.execute_notebook(
        "fruitchecker.ipynb",
        output_path="/tmp/nulloutput.ipynb", #The notebook output isnt used, but a path is still required
        parameters={"fruits": fruit_list}
    )

    output_path = "/tmp/output.csv"
    
    # Extract the CSV result
    with open(output_path, "r") as f:
        csv_output = f.read()

    # Return as downloadable CSV
    return Response(content=csv_output, media_type="text/csv",
                    headers={"Content-Disposition": "attachment; filename=fruits.csv"})