Dear Community Members,
I am new to FastAPI and I am trying to display the pandas dataframe as FastAPI output but I am not able to get the desired result. Here is something I am have done:
from fastapi import FastAPI, Body, Request, Form
from pydantic import BaseModel
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
import pandas as pd
app = FastAPI()
templates = Jinja2Templates(directory="htmlDirectory")
class NameValues(BaseModel):
Check : str = None
@app.get("/home", response_class = HTMLResponse)
def home(request:Request):
return templates.TemplateResponse("introduction.html", {"request": request})
@app.post("/displayDF")
async def handle_df(Check: str = Form(...)):
print("User input : ", Check)
test_list = [["Joe", 34, "Accounts", 10000], ["Jack", 34, "Chemistry", 20000]]
data = pd.DataFrame(data=test_list, columns=["Name", "Age", "Dept.", "Salary"])
return data.to_html()
The output displays like below screenshot:
Is there a way to actually get the output in pandas dataframe format? Something like below :
Also, below is the "htmlDirectory" code, if some changes has to be done here.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h2>Please enter a string in the space provided</h2>
<form action="/displayDF" method="post">
<input type="text" name="Check">
<input type="submit">
</form>
</body>
</html>

