如何从 fastAPI 中的数据帧呈现 json
how to render a json from a dataframe in fastAPI
我有一个要在 fastAPI 应用程序中呈现的 csv 文件。我只设法以 json 格式呈现 te csv,如下所示:
def transform_question_format(csv_file_name):
json_file_name = f"{csv_file_name[:-4]}.json"
# transforms the csv file into json file
pd.read_csv(csv_file_name ,sep=",").to_json(json_file_name)
with open(json_file_name, "r") as f:
json_data = json.load(f)
return json_data
@app.get("/questions")
def load_questions():
question_json = transform_question_format(question_csv_filename)
return question_json
当我在 return 中直接尝试 pd.read_csv(csv_file_name ,sep=",").to_json(json_file_name)
时,它确实有效,因为它 return 是一个字符串。
我应该如何进行?我认为这不是一个好方法。
下面显示了从 csv
文件中 return 数据的四种不同方法。
方案1是将文件数据转换成JSON
,然后再解析成dict
。您可以选择使用 to_json()
方法中的 orient
参数更改数据的方向。
- 更新 1:使用
to_dict()
方法可能是更好的选择,因为不需要解析 JSON
字符串。
- 更新 2:当使用
to_dict()
方法和 returning dict
,FastAPI,在幕后, 使用 jsonable_encoder
自动将 return 值转换为 JSON
。因此,为了避免额外的工作,您仍然可以使用 to_json()
方法,但不是解析 JSON
字符串,而是将其放在 Response
中并直接 return 它,如下例所示。
方案2是return数据string
格式,使用to_string()
方法
选项 3 是 return 数据作为 HTML
table,使用 to_html()
方法。
选项 4 return file
就像使用 FastAPI 的 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
我有一个要在 fastAPI 应用程序中呈现的 csv 文件。我只设法以 json 格式呈现 te csv,如下所示:
def transform_question_format(csv_file_name):
json_file_name = f"{csv_file_name[:-4]}.json"
# transforms the csv file into json file
pd.read_csv(csv_file_name ,sep=",").to_json(json_file_name)
with open(json_file_name, "r") as f:
json_data = json.load(f)
return json_data
@app.get("/questions")
def load_questions():
question_json = transform_question_format(question_csv_filename)
return question_json
当我在 return 中直接尝试 pd.read_csv(csv_file_name ,sep=",").to_json(json_file_name)
时,它确实有效,因为它 return 是一个字符串。
我应该如何进行?我认为这不是一个好方法。
下面显示了从 csv
文件中 return 数据的四种不同方法。
方案1是将文件数据转换成JSON
,然后再解析成dict
。您可以选择使用 to_json()
方法中的 orient
参数更改数据的方向。
- 更新 1:使用
to_dict()
方法可能是更好的选择,因为不需要解析JSON
字符串。 - 更新 2:当使用
to_dict()
方法和 returningdict
,FastAPI,在幕后, 使用jsonable_encoder
自动将 return 值转换为JSON
。因此,为了避免额外的工作,您仍然可以使用to_json()
方法,但不是解析JSON
字符串,而是将其放在Response
中并直接 return 它,如下例所示。
方案2是return数据string
格式,使用to_string()
方法
选项 3 是 return 数据作为 HTML
table,使用 to_html()
方法。
选项 4 return file
就像使用 FastAPI 的 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