Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, Response | |
import papermill as pm | |
import io | |
app = FastAPI() | |
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"}) | |