The below shows four different ways to return the data from a csv file.
Option 1 is to convert the file data into JSON and then parse it into a dict. You can optionally change the orientation of the data using the orient parameter in the to_json() method.
- Update 1: Using
to_dict() method might be a better option, as there is no need for parsing the JSON string.
- Update 2: When using
to_dict() method and returning the dict, FastAPI, behind the scenes, automatically converts that return value into JSON, using the jsonable_encoder. Thus, to avoid that extra work, you could still use to_json() method, but instead of parsing the JSON string, put it in a Response and return it directly, as shown in the example below.
Option 2 is to return the data in string format, using to_string() method.
Option 3 is to return the data as an HTML table, using to_html() method.
Option 4 is to return the file as is using FastAPI's FileResponse.
from fastapi import FastAPI, Response
from fastapi.responses import FileResponse
from fastapi.responses import HTMLResponse
import pandas as pd
import json
df = pd.read_csv("file.csv")
app = FastAPI()
def parse_csv(df):
res = df.to_json(orient="records")
parsed = json.loads(res)
return parsed
@app.get("/questions")
def load_questions():
return Response(df.to_json(orient="records"), media_type="application/json") # Option 1 (Updated 2): Return as JSON directly
#return df.to_dict(orient="records") # Option 1 (Updated 1): Return as dict (encoded to JSON behind the scenes)
#return parse_csv(df) # Option 1: Parse the JSON string and return as dict (encoded to JSON behind the scenes)
#return df.to_string() # Option 2: Return as string
#return HTMLResponse(content=df.to_html(), status_code=200) # Option 3: Return as HTML Table
#return FileResponse(path="file.csv", filename="file.csv") # Option 4: Return as File